add i18n embedding

master
Brad Rydzewski 7 years ago
parent c5dd598ac8
commit 9c1d6a5bbe

@ -0,0 +1,90 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli"
"github.com/bradrydzewski/togo/template"
)
type (
i18nParams struct {
Package string
Files []*i18nFile
}
i18nFile struct {
Base string
Name string
Path string
Ext string
Data string
}
)
var i18nCommand = cli.Command{
Name: "i18n",
Usage: "embed i18n files",
Action: i18nAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "package",
Value: "i18n",
},
cli.StringFlag{
Name: "input",
Value: "files/*.all.json",
},
cli.StringFlag{
Name: "output",
Value: "i18n_gen.go",
},
cli.BoolFlag{
Name: "encode",
},
},
}
func i18nAction(c *cli.Context) error {
pattern := c.Args().First()
if pattern == "" {
pattern = c.String("input")
}
matches, err := filepath.Glob(pattern)
if err != nil {
return err
}
params := i18nParams{
Package: c.String("package"),
}
for _, match := range matches {
raw, ioerr := ioutil.ReadFile(match)
if ioerr != nil {
return ioerr
}
params.Files = append(params.Files, &i18nFile{
Path: match,
Name: filepath.Base(match),
Base: strings.TrimSuffix(filepath.Base(match), filepath.Ext(match)),
Ext: filepath.Ext(match),
Data: string(raw),
})
}
wr := os.Stdout
if output := c.String("output"); output != "" {
wr, err = os.Create(output)
if err != nil {
return err
}
defer wr.Close()
}
return template.Execute(wr, "i18n.tmpl", params)
}

@ -18,6 +18,7 @@ func main() {
sqlCommand,
httpCommand,
tmplCommand,
i18nCommand,
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)

@ -0,0 +1,8 @@
{
"greeting": {
"other": "Hello"
},
"welcome": {
"other": "Welcome"
}
}

@ -0,0 +1,5 @@
- id: greeting
translation: Hello
- id: welcome
translation: Welcome

@ -0,0 +1,8 @@
{
"greeting": {
"other": ""
},
"welcome": {
"other": ""
}
}

@ -0,0 +1,8 @@
{
"greeting": {
"other": "Hello"
},
"welcome": {
"other": "Welcome"
}
}

@ -0,0 +1,5 @@
- id: greeting
translation: Hola
- id: welcome
translation: Bienvenidos

@ -0,0 +1,8 @@
{
"greeting": {
"other": "Bonjour"
},
"welcome": {
"other": "Bienvenue"
}
}

@ -0,0 +1,5 @@
- id: greeting
translation: Bonjour
- id: welcome
translation: Bienvenue

@ -0,0 +1,8 @@
{
"greeting": {
"other": "Hello"
},
"welcome": {
"other": "Welcome"
}
}

@ -0,0 +1,12 @@
package i18n
//go:generate goi18n -flat -outdir files files/en-us.yaml
//go:generate goi18n -flat -outdir files files/fr-fr.yaml files/en-us.yaml
//go:generate goi18n -flat -outdir files files/es-es.yaml files/en-us.yaml
//go:generate goi18n -flat -outdir files files/zh-cn.yaml files/en-us.yaml
//go:generate goi18n -flat -outdir files files/fr-fr.all.json files/fr-fr.untranslated.json files/en-us.all.json
//go:generate goi18n -flat -outdir files files/fr-fr.all.json files/es-es.untranslated.json files/en-us.all.json
//go:generate goi18n -flat -outdir files files/fr-fr.all.json files/zh-cn.untranslated.json files/en-us.all.json
//go:generate togo i18n

@ -0,0 +1,73 @@
package i18n
import "github.com/nicksnyder/go-i18n/i18n"
// list of embedded template files.
var files = []struct {
name string
data string
}{
{
name: "en-us.all.json",
data: enUsall,
}, {
name: "es-es.all.json",
data: esEsall,
}, {
name: "fr-fr.all.json",
data: frFrall,
}, {
name: "zh-cn.all.json",
data: zhCnall,
},
}
func init() {
for _, file := range files {
i18n.ParseTranslationFileBytes(file.Name, []byte(file.data))
}
}
//
// embedded template files.
//
// files/en-us.all.json
var enUsall = `{
"greeting": {
"other": "Hello"
},
"welcome": {
"other": "Welcome"
}
}`
// files/es-es.all.json
var esEsall = `{
"greeting": {
"other": ""
},
"welcome": {
"other": ""
}
}`
// files/fr-fr.all.json
var frFrall = `{
"greeting": {
"other": "Bonjour"
},
"welcome": {
"other": "Bienvenue"
}
}`
// files/zh-cn.all.json
var zhCnall = `{
"greeting": {
"other": "Hello"
},
"welcome": {
"other": "Welcome"
}
}`

@ -0,0 +1,31 @@
package {{ .Package }}
import "github.com/nicksnyder/go-i18n/i18n"
// list of embedded template files.
var files = []struct{
name string
data string
} {
{{ range .Files -}}
{
name: {{ printf "%q" .Name }},
data: {{ .Base | camelize }},
},
{{- end }}
}
func init() {
for _, file := range files {
i18n.ParseTranslationFileBytes(file.Name, []byte(file.data))
}
}
//
// embedded template files.
//
{{ range .Files }}
// {{ .Path }}
var {{ .Base | camelize }} = `{{ .Data }}`
{{ end }}

@ -13,6 +13,9 @@ var files = []struct {
}, {
name: "http.tmpl",
data: string(http),
}, {
name: "i18n.tmpl",
data: string(i18n),
}, {
name: "sql.tmpl",
data: string(sql),
@ -486,6 +489,52 @@ var http = []byte{
0x7d, 0x0a,
}
// files/i18n.tmpl
var i18n = []byte{
0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x7b, 0x7b, 0x20, 0x2e,
0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x7d, 0x7d, 0x0a, 0x0a,
0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x22, 0x67, 0x69, 0x74, 0x68,
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6e, 0x69, 0x63, 0x6b, 0x73,
0x6e, 0x79, 0x64, 0x65, 0x72, 0x2f, 0x67, 0x6f, 0x2d, 0x69, 0x31, 0x38,
0x6e, 0x2f, 0x69, 0x31, 0x38, 0x6e, 0x22, 0x0a, 0x0a, 0x2f, 0x2f, 0x20,
0x6c, 0x69, 0x73, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x65, 0x6d, 0x62, 0x65,
0x64, 0x64, 0x65, 0x64, 0x20, 0x74, 0x65, 0x6d, 0x70, 0x6c, 0x61, 0x74,
0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x0a, 0x76, 0x61, 0x72,
0x20, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x20, 0x3d, 0x20, 0x5b, 0x5d, 0x73,
0x74, 0x72, 0x75, 0x63, 0x74, 0x7b, 0x0a, 0x09, 0x6e, 0x61, 0x6d, 0x65,
0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x0a, 0x09, 0x64, 0x61, 0x74,
0x61, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x0a, 0x7d, 0x20, 0x7b,
0x0a, 0x09, 0x7b, 0x7b, 0x20, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x20, 0x2e,
0x46, 0x69, 0x6c, 0x65, 0x73, 0x20, 0x2d, 0x7d, 0x7d, 0x0a, 0x09, 0x7b,
0x0a, 0x09, 0x09, 0x6e, 0x61, 0x6d, 0x65, 0x3a, 0x20, 0x7b, 0x7b, 0x20,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x71, 0x22, 0x20,
0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x20, 0x7d, 0x7d, 0x2c, 0x0a, 0x09, 0x09,
0x64, 0x61, 0x74, 0x61, 0x3a, 0x20, 0x7b, 0x7b, 0x20, 0x2e, 0x42, 0x61,
0x73, 0x65, 0x20, 0x7c, 0x20, 0x63, 0x61, 0x6d, 0x65, 0x6c, 0x69, 0x7a,
0x65, 0x20, 0x7d, 0x7d, 0x2c, 0x0a, 0x09, 0x7d, 0x2c, 0x0a, 0x09, 0x7b,
0x7b, 0x2d, 0x20, 0x65, 0x6e, 0x64, 0x20, 0x7d, 0x7d, 0x0a, 0x7d, 0x0a,
0x0a, 0x66, 0x75, 0x6e, 0x63, 0x20, 0x69, 0x6e, 0x69, 0x74, 0x28, 0x29,
0x20, 0x7b, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x5f, 0x2c, 0x20, 0x66,
0x69, 0x6c, 0x65, 0x20, 0x3a, 0x3d, 0x20, 0x72, 0x61, 0x6e, 0x67, 0x65,
0x20, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x69,
0x31, 0x38, 0x6e, 0x2e, 0x50, 0x61, 0x72, 0x73, 0x65, 0x54, 0x72, 0x61,
0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x46, 0x69, 0x6c, 0x65,
0x42, 0x79, 0x74, 0x65, 0x73, 0x28, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x4e,
0x61, 0x6d, 0x65, 0x2c, 0x20, 0x5b, 0x5d, 0x62, 0x79, 0x74, 0x65, 0x28,
0x66, 0x69, 0x6c, 0x65, 0x2e, 0x64, 0x61, 0x74, 0x61, 0x29, 0x29, 0x0a,
0x09, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x2f, 0x2f, 0x0a, 0x2f, 0x2f, 0x20,
0x65, 0x6d, 0x62, 0x65, 0x64, 0x64, 0x65, 0x64, 0x20, 0x74, 0x65, 0x6d,
0x70, 0x6c, 0x61, 0x74, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e,
0x0a, 0x2f, 0x2f, 0x0a, 0x0a, 0x7b, 0x7b, 0x20, 0x72, 0x61, 0x6e, 0x67,
0x65, 0x20, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x73, 0x20, 0x7d, 0x7d, 0x0a,
0x2f, 0x2f, 0x20, 0x7b, 0x7b, 0x20, 0x2e, 0x50, 0x61, 0x74, 0x68, 0x20,
0x7d, 0x7d, 0x0a, 0x76, 0x61, 0x72, 0x20, 0x7b, 0x7b, 0x20, 0x2e, 0x42,
0x61, 0x73, 0x65, 0x20, 0x7c, 0x20, 0x63, 0x61, 0x6d, 0x65, 0x6c, 0x69,
0x7a, 0x65, 0x20, 0x7d, 0x7d, 0x20, 0x3d, 0x20, 0x60, 0x7b, 0x7b, 0x20,
0x2e, 0x44, 0x61, 0x74, 0x61, 0x20, 0x7d, 0x7d, 0x60, 0x0a, 0x7b, 0x7b,
0x20, 0x65, 0x6e, 0x64, 0x20, 0x7d, 0x7d, 0x0a,
}
// files/sql.tmpl
var sql = []byte{
0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x7b, 0x7b, 0x20, 0x2e,

Loading…
Cancel
Save