/
i18n.go
103 lines (90 loc) · 2.65 KB
/
i18n.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
"github.com/gleke/hexya/src/tools/generate"
"github.com/spf13/cobra"
"golang.org/x/tools/go/packages"
)
const startFileNameI18n = "i18nUpdate.go"
var i18nCmd = &cobra.Command{
Use: "i18n",
Short: "Internationalization utilities",
Long: `Internationalization utilities for Hexya`,
}
var i18nUpdate = &cobra.Command{
Use: "update [dir]",
Short: "Create or update PO files",
Long: `Create or update PO files of the module specified by 'dir'.
PO files will be generated for each loaded language (--language flag)
in the i18n directory of the module.`,
Run: func(cmd *cobra.Command, args []string) {
moduleDir, _ := filepath.Abs(".")
if len(args) > 0 {
moduleDir = args[0]
}
langs, err := cmd.Flags().GetStringSlice("languages")
if err != nil {
log.Panic("Unable to read languages from the command line")
}
generateAndUpdatePOFiles(moduleDir, langs, startFileTemplateI18n)
},
}
// generateAndUpdatePOFile creates the startup file of the translation update and runs it.
func generateAndUpdatePOFiles(moduleDir string, langs []string, tmpl *template.Template) {
testEnabled = true
runGenerate(moduleDir)
fmt.Println("Please wait, Hexya is starting ...")
moduleDir, _ = filepath.Abs(moduleDir)
loadConf := packages.Config{
Mode: packages.NeedName,
}
packs, err := packages.Load(&loadConf, moduleDir)
if err != nil {
panic(err)
}
modulePath := packs[0].PkgPath
conf := make(map[string]interface{})
conf["moduleDir"] = moduleDir
conf["modulePath"] = modulePath
conf["langs"] = langs
tmplData := struct {
Imports []string
Config string
}{
Imports: []string{modulePath},
Config: fmt.Sprintf("%#v", conf),
}
fileName := filepath.Join(os.TempDir(), startFileNameI18n)
generate.CreateFileFromTemplate(fileName, tmpl, tmplData)
cmd := exec.Command("go", "run", fileName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
os.Remove(fileName)
}
func init() {
i18nUpdate.PersistentFlags().StringSliceP("languages", "l", []string{}, "Comma separated list of languages codes to load (ex: fr,de,es).")
HexyaCmd.AddCommand(i18nCmd)
i18nCmd.AddCommand(i18nUpdate)
}
var startFileTemplateI18n = template.Must(template.New("").Parse(`
// This file is autogenerated by hexya-server
// DO NOT MODIFY THIS FILE - ANY CHANGES WILL BE OVERWRITTEN
package main
import (
"fmt"
"github.com/gleke/hexya/src/i18n/translations"
{{ range .Imports }} _ "{{ . }}"
{{ end }}
)
func main() {
fmt.Println("Starting translation")
translations.UpdatePOFiles({{ .Config }})
}
`))