Debian Codegraph @

debian/go/src/github.com/gohugoio/go-i18n/v2/goi18n/main.go

raw
1 // Command goi18n manages message files used by the i18n package.
2 //
3 // go get -u github.com/gohugoio/go-i18n/v2/goi18n
4 // goi18n -help
5 //
6 // Use `goi18n extract` to create a message file that contains the messages defined in your Go source files.
7 //
8 // # en.toml
9 // [PersonCats]
10 // description = "The number of cats a person has"
11 // one = "{{.Name}} has {{.Count}} cat."
12 // other = "{{.Name}} has {{.Count}} cats."
13 //
14 // Use `goi18n merge` to create message files for translation.
15 //
16 // # translate.es.toml
17 // [PersonCats]
18 // description = "The number of cats a person has"
19 // hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
20 // one = "{{.Name}} has {{.Count}} cat."
21 // other = "{{.Name}} has {{.Count}} cats."
22 //
23 // Use `goi18n merge` to merge translated message files with your existing message files.
24 //
25 // # active.es.toml
26 // [PersonCats]
27 // description = "The number of cats a person has"
28 // hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
29 // one = "{{.Name}} tiene {{.Count}} gato."
30 // other = "{{.Name}} tiene {{.Count}} gatos."
31 //
32 // Load the active messages into your bundle.
33 //
34 // bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
35 // bundle.MustLoadMessageFile("active.es.toml")
36 package main
37
38 import (
39 "flag"
40 "fmt"
41 "os"
42
43 "golang.org/x/text/language"
44 )
45
46 func mainUsage() {
47 fmt.Fprintf(os.Stderr, `goi18n (v2) is a tool for managing message translations.
48
49 Usage:
50
51 goi18n command [arguments]
52
53 The commands are:
54
55 merge merge message files
56 extract extract messages from Go files
57
58 Workflow:
59
60 Use 'goi18n extract' to create a message file that contains the messages defined in your Go source files.
61
62 # en.toml
63 [PersonCats]
64 description = "The number of cats a person has"
65 one = "{{.Name}} has {{.Count}} cat."
66 other = "{{.Name}} has {{.Count}} cats."
67
68 Use 'goi18n merge' to create message files for translation.
69
70 # translate.es.toml
71 [PersonCats]
72 description = "The number of cats a person has"
73 hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
74 one = "{{.Name}} has {{.Count}} cat."
75 other = "{{.Name}} has {{.Count}} cats."
76
77 Use 'goi18n merge' to merge translated message files with your existing message files.
78
79 # active.es.toml
80 [PersonCats]
81 description = "The number of cats a person has"
82 hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
83 one = "{{.Name}} tiene {{.Count}} gato."
84 other = "{{.Name}} tiene {{.Count}} gatos."
85
86 Load the active messages into your bundle.
87
88 bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
89 bundle.MustLoadMessageFile("active.es.toml")
90 `)
91 }
92
93 type command interface {
94 name() string
95 parse(arguments []string) error
96 execute() error
97 }
98
99 func main() {
100 os.Exit(testableMain(os.Args[1:]))
101 }
102
103 func testableMain(args []string) int {
104 flags := flag.NewFlagSet("goi18n", flag.ContinueOnError)
105 flags.Usage = mainUsage
106 if err := flags.Parse(args); err != nil {
107 if err == flag.ErrHelp {
108 return 2
109 }
110 return 1
111 }
112 if flags.NArg() == 0 {
113 mainUsage()
114 return 2
115 }
116 commands := []command{
117 &mergeCommand{},
118 &extractCommand{},
119 }
120 cmdName := flags.Arg(0)
121 for _, cmd := range commands {
122 if cmd.name() == cmdName {
123 if err := cmd.parse(flags.Args()[1:]); err != nil {
124 fmt.Fprintln(os.Stderr, err)
125 return 1
126 }
127 if err := cmd.execute(); err != nil {
128 fmt.Fprintln(os.Stderr, err)
129 return 1
130 }
131 return 0
132 }
133 }
134 fmt.Fprintf(os.Stderr, "goi18n: unknown subcommand %s\n", cmdName)
135 return 1
136 }
137
138 type languageTag language.Tag
139
140 func (lt languageTag) String() string {
141 return lt.Tag().String()
142 }
143
144 func (lt *languageTag) Set(value string) error {
145 t, err := language.Parse(value)
146 if err != nil {
147 return err
148 }
149 *lt = languageTag(t)
150 return nil
151 }
152
153 func (lt languageTag) Tag() language.Tag {
154 tag := language.Tag(lt)
155 if tag.IsRoot() {
156 return language.English
157 }
158 return tag
159 }