diff --git a/cmd/gotext/doc.go b/cmd/gotext/doc.go index d363ae25..fcf36058 100644 --- a/cmd/gotext/doc.go +++ b/cmd/gotext/doc.go @@ -46,5 +46,5 @@ // // Usage: // -// gotext generate +// gotext generate [-out ] package main diff --git a/cmd/gotext/extract.go b/cmd/gotext/extract.go index 103d7e60..80a3a72b 100644 --- a/cmd/gotext/extract.go +++ b/cmd/gotext/extract.go @@ -14,16 +14,17 @@ import ( // - handle features (gender, plural) // - message rewriting -func init() { - lang = cmdExtract.Flag.String("lang", "en-US", "comma-separated list of languages to process") -} - var cmdExtract = &Command{ + Init: initExtract, Run: runExtract, UsageLine: "extract *", Short: "extracts strings to be translated from code", } +func initExtract(cmd *Command) { + lang = cmd.Flag.String("lang", "en-US", "comma-separated list of languages to process") +} + func runExtract(cmd *Command, config *pipeline.Config, args []string) error { config.Packages = args state, err := pipeline.Extract(config) diff --git a/cmd/gotext/generate.go b/cmd/gotext/generate.go index 36820df8..77b3c81e 100644 --- a/cmd/gotext/generate.go +++ b/cmd/gotext/generate.go @@ -8,16 +8,17 @@ import ( "golang.org/x/text/message/pipeline" ) -func init() { - out = cmdGenerate.Flag.String("out", "", "output file to write to") -} - var cmdGenerate = &Command{ + Init: initGenerate, Run: runGenerate, - UsageLine: "generate ", + UsageLine: "generate [-out ]", Short: "generates code to insert translated messages", } +func initGenerate(cmd *Command) { + out = cmd.Flag.String("out", "", "output file to write to") +} + func runGenerate(cmd *Command, config *pipeline.Config, args []string) error { config.Packages = args s, err := pipeline.Extract(config) @@ -27,5 +28,8 @@ func runGenerate(cmd *Command, config *pipeline.Config, args []string) error { if err := s.Import(); err != nil { return wrap(err, "import failed") } + if err := s.Merge(); err != nil { + return wrap(err, "merge failed") + } return wrap(s.Generate(), "generation failed") } diff --git a/cmd/gotext/main.go b/cmd/gotext/main.go index aad1d4a1..f69ea931 100644 --- a/cmd/gotext/main.go +++ b/cmd/gotext/main.go @@ -35,6 +35,10 @@ func init() { } var ( + lang *string + out *string + overwrite *bool + srcLang = flag.String("srclang", "en-US", "the source-code language") dir = flag.String("dir", "locales", "default subdirectory to store translation files") ) @@ -57,6 +61,9 @@ func config() (*pipeline.Config, error) { // A Command is an implementation of a go command // like go build or go fix. type Command struct { + // Init initializes the flag set of the command. + Init func(cmd *Command) + // Run runs the command. // The args are the arguments after the command name. Run func(cmd *Command, c *pipeline.Config, args []string) error @@ -139,6 +146,7 @@ func main() { for _, cmd := range commands { if cmd.Name() == args[0] && cmd.Runnable() { + cmd.Init(cmd) cmd.Flag.Usage = func() { cmd.Usage() } cmd.Flag.Parse(args[1:]) args = cmd.Flag.Args() @@ -331,6 +339,9 @@ func help(args []string) { } func getLangs() (tags []language.Tag) { + if lang == nil { + return []language.Tag{language.AmericanEnglish} + } for _, t := range strings.Split(*lang, ",") { if t == "" { continue diff --git a/cmd/gotext/rewrite.go b/cmd/gotext/rewrite.go index 9702ca7f..5a6fd3a2 100644 --- a/cmd/gotext/rewrite.go +++ b/cmd/gotext/rewrite.go @@ -19,15 +19,8 @@ const printerType = "golang.org/x/text/message.Printer" // - handle features (gender, plural) // - message rewriting -func init() { - overwrite = cmdRewrite.Flag.Bool("w", false, "write files in place") -} - -var ( - overwrite *bool -) - var cmdRewrite = &Command{ + Init: initRewrite, Run: runRewrite, UsageLine: "rewrite ", Short: "rewrites fmt functions to use a message Printer", @@ -39,6 +32,10 @@ using Printf to allow translators to reorder arguments. `, } +func initRewrite(cmd *Command) { + overwrite = cmd.Flag.Bool("w", false, "write files in place") +} + func runRewrite(cmd *Command, _ *pipeline.Config, args []string) error { var w io.Writer if !*overwrite { diff --git a/cmd/gotext/update.go b/cmd/gotext/update.go index 1260750c..50f1c2d1 100644 --- a/cmd/gotext/update.go +++ b/cmd/gotext/update.go @@ -14,22 +14,18 @@ import ( // - handle features (gender, plural) // - message rewriting -var ( - lang *string - out *string -) - -func init() { - lang = cmdUpdate.Flag.String("lang", "en-US", "comma-separated list of languages to process") - out = cmdUpdate.Flag.String("out", "", "output file to write to") -} - var cmdUpdate = &Command{ + Init: initUpdate, Run: runUpdate, UsageLine: "update * [-out ]", Short: "merge translations and generate catalog", } +func initUpdate(cmd *Command) { + lang = cmd.Flag.String("lang", "en-US", "comma-separated list of languages to process") + out = cmd.Flag.String("out", "", "output file to write to") +} + func runUpdate(cmd *Command, config *pipeline.Config, args []string) error { config.Packages = args state, err := pipeline.Extract(config) diff --git a/message/pipeline/generate.go b/message/pipeline/generate.go index bb1f85b9..f747c376 100644 --- a/message/pipeline/generate.go +++ b/message/pipeline/generate.go @@ -8,6 +8,7 @@ import ( "fmt" "go/build" "io" + "os" "path/filepath" "regexp" "sort" @@ -52,6 +53,10 @@ func (s *State) Generate() error { gopath := filepath.SplitList(build.Default.GOPATH)[0] path = filepath.Join(gopath, "src", filepath.FromSlash(pkgs[0].Pkg.Path())) } + if len(s.Config.GenFile) == 0 { + cw.WriteGo(os.Stdout, pkg, "") + return nil + } if filepath.IsAbs(s.Config.GenFile) { path = s.Config.GenFile } else {