From ab07ad1b65bc4cdc738e747f7569a3795d2e60ec Mon Sep 17 00:00:00 2001 From: cui fliter Date: Sat, 15 Jul 2023 00:30:59 +0800 Subject: [PATCH 1/3] all: remove repetitive words Change-Id: I496265fcad6dee919eff6fc1fbcf9121ae61730f Reviewed-on: https://go-review.googlesource.com/c/text/+/509855 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Heschi Kreinick TryBot-Result: Gopher Robot Auto-Submit: Ian Lance Taylor --- language/match.go | 2 +- secure/precis/gen.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/language/match.go b/language/match.go index ee45f494..1153baf2 100644 --- a/language/match.go +++ b/language/match.go @@ -434,7 +434,7 @@ func newMatcher(supported []Tag, options []MatchOption) *matcher { // (their canonicalization simply substitutes a different language code, but // nothing else), the match confidence is Exact, otherwise it is High. for i, lm := range language.AliasMap { - // If deprecated codes match and there is no fiddling with the script or + // If deprecated codes match and there is no fiddling with the script // or region, we consider it an exact match. conf := Exact if language.AliasTypes[i] != language.Macro { diff --git a/secure/precis/gen.go b/secure/precis/gen.go index 99ead429..aa20568c 100644 --- a/secure/precis/gen.go +++ b/secure/precis/gen.go @@ -221,12 +221,12 @@ func isLetterDigits(r rune) bool { func isIdDisAndFreePVal(r rune) bool { return unicode.In(r, // OtherLetterDigits: https://tools.ietf.org/html/rfc7564#section-9.18 - // r in in {Lt, Nl, No, Me} + // r in {Lt, Nl, No, Me} unicode.Lt, unicode.Nl, unicode.No, // Other letters / numbers unicode.Me, // Modifiers // Spaces: https://tools.ietf.org/html/rfc7564#section-9.14 - // r in in {Zs} + // r in {Zs} unicode.Zs, // Symbols: https://tools.ietf.org/html/rfc7564#section-9.15 From f3e69ed4a8ab60c16ae76f4ddb08f2726b0a9428 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Sat, 19 Nov 2022 04:59:42 +0800 Subject: [PATCH 2/3] cmd/gotext: fix misbehaviors 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 TryBot-Result: Gopher Robot Reviewed-by: Heschi Kreinick Reviewed-by: Marcel van Lohuizen Reviewed-by: Dmitri Shuralyov Run-TryBot: Dmitri Shuralyov --- cmd/gotext/doc.go | 2 +- cmd/gotext/extract.go | 9 +++++---- cmd/gotext/generate.go | 14 +++++++++----- cmd/gotext/main.go | 11 +++++++++++ cmd/gotext/rewrite.go | 13 +++++-------- cmd/gotext/update.go | 16 ++++++---------- message/pipeline/generate.go | 5 +++++ 7 files changed, 42 insertions(+), 28 deletions(-) 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 { From fb697c0580b4b6ab0a21ca17e64788b981fb6018 Mon Sep 17 00:00:00 2001 From: Smith A Date: Fri, 21 Jul 2023 23:31:27 +0800 Subject: [PATCH 3/3] cmd/gotext: actually use -dir flag gotext has the read -dir flag but is not used. The dir parameter should be passed to pipeline.Config Fixes golang/go#61507 Change-Id: I242a768964fe56c4c7877de9dc487777c6815dae Reviewed-on: https://go-review.googlesource.com/c/text/+/512015 Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Heschi Kreinick Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor --- cmd/gotext/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/gotext/main.go b/cmd/gotext/main.go index f69ea931..ed0f7eca 100644 --- a/cmd/gotext/main.go +++ b/cmd/gotext/main.go @@ -53,6 +53,7 @@ func config() (*pipeline.Config, error) { Supported: getLangs(), TranslationsPattern: `messages\.(.*)\.json$`, GenFile: *out, + Dir: *dir, }, nil }