Skip to content

Commit

Permalink
cmd/gotext: fix misbehaviors
Browse files Browse the repository at this point in the history
The existing implementation has misbehaviors described in golang/go#56842:

- `gotext extract` ignores `lang` flag
- `gotext generate` ignores `out` flag
- Misbehavior of `gotext generate` when no flag specified

This commit fixes these bugs:

- Update `Command.UsageLine` for `cmdGenerate`
- Fix flag misbehaviors by encapsuling flag definition statements into individual `Command.Init()` functions of commands
- Fix `gotext generate` misbehavior by executing `pipeline.State.Merge()` before `pipeline.State.Generate()`

Fixes golang/go#56842

Change-Id: Id5e324b573b2b389bec22181482a97445230d0cc
Reviewed-on: https://go-review.googlesource.com/c/text/+/452115
Auto-Submit: Dmitri Shuralyov <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Heschi Kreinick <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Run-TryBot: Dmitri Shuralyov <[email protected]>
  • Loading branch information
ilharp authored and gopherbot committed Jul 20, 2023
1 parent ab07ad1 commit f3e69ed
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/gotext/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions cmd/gotext/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <package>*",
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)
Expand Down
14 changes: 9 additions & 5 deletions cmd/gotext/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <package>",
UsageLine: "generate <package> [-out <gofile>]",
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)
Expand All @@ -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")
}
11 changes: 11 additions & 0 deletions cmd/gotext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions cmd/gotext/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <package>",
Short: "rewrites fmt functions to use a message Printer",
Expand All @@ -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 {
Expand Down
16 changes: 6 additions & 10 deletions cmd/gotext/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <package>* [-out <gofile>]",
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)
Expand Down
5 changes: 5 additions & 0 deletions message/pipeline/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"go/build"
"io"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f3e69ed

Please sign in to comment.