-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
133 lines (121 loc) · 3.12 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
//go:generate go run main.go extract . ./locales/en.json
//go:generate go run main.go update ./locales/en.json ./locales/zh-Hans.json
//go:generate go run main.go update ./locales/en.json ./locales/zh-Hant.json
//go:generate go run main.go generate --pkg=catalog ./locales ./catalog/catalog.go
import (
"fmt"
"log"
"os"
"github.com/Xuanwo/go-locale"
_ "github.com/mylukin/easy-i18n/easyi18n/catalog"
"github.com/mylukin/easy-i18n/i18n"
"github.com/urfave/cli/v2"
)
func main() {
// Detect OS language
tag, _ := locale.Detect()
// Set Language
i18n.SetLang(tag)
appName := "easyi18n"
app := &cli.App{
HelpName: appName,
Name: appName,
Usage: i18n.Sprintf(`a tool for managing message translations.`),
Action: func(c *cli.Context) error {
cli.ShowAppHelp(c)
return nil
},
Commands: []*cli.Command{
{
Name: "update",
Aliases: []string{"u"},
Usage: i18n.Sprintf(`merge translations and generate catalog`),
UsageText: i18n.Sprintf(`%s update srcfile destfile`, appName),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "flush",
Aliases: []string{"f"},
Value: false,
Usage: fmt.Sprintf(`flush messages`),
},
},
Action: func(c *cli.Context) error {
srcFile := c.Args().Get(0)
if len(srcFile) == 0 {
return fmt.Errorf(i18n.Sprintf(`srcfile cannot be empty`))
}
destFile := c.Args().Get(1)
if len(destFile) == 0 {
return fmt.Errorf(i18n.Sprintf(`destfile cannot be empty`))
}
flush := c.Bool("flush")
err := i18n.Update(srcFile, destFile, flush)
return err
},
},
{
Name: "extract",
Aliases: []string{"e"},
Usage: i18n.Sprintf(`extracts strings to be translated from code`),
UsageText: i18n.Sprintf(`%s extract [path] [outfile]`, appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pkg",
Value: "i18n",
Usage: i18n.Sprintf(`package name`),
},
},
Action: func(c *cli.Context) error {
path := c.Args().Get(0)
if len(path) == 0 {
path = "."
}
outFile := c.Args().Get(1)
if len(outFile) == 0 {
outFile = "./locales/en.json"
}
pkgName := c.String("pkg")
err := i18n.Extract(pkgName, []string{
path,
}, outFile)
return err
},
},
{
Name: "generate",
Aliases: []string{"g"},
Usage: i18n.Sprintf(`generates code to insert translated messages`),
UsageText: i18n.Sprintf(`%s generate [path] [outfile]`, appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pkg",
Value: "catalog",
Usage: i18n.Sprintf(`generated go file package name`),
},
},
Action: func(c *cli.Context) error {
path := c.Args().Get(0)
if len(path) == 0 {
path = "./locales"
}
outFile := c.Args().Get(1)
if len(outFile) == 0 {
outFile = "./catalog/catalog.go"
}
pkgName := c.String("pkg")
err := i18n.Generate(
pkgName,
[]string{
path,
}, outFile)
return err
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}