-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement localization using gettext files — I18N — L10N #2090
base: main
Are you sure you want to change the base?
Changes from 1 commit
ab14efa
c922911
20e0d82
f7f98c4
d362950
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
import ( | ||
"fmt" | ||
"github.com/leonelquinteros/gotext" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could import this as |
||
"strings" | ||
) | ||
|
||
|
@@ -33,15 +34,15 @@ | |
|
||
// root command with subcommands, do subcommand checking. | ||
if !cmd.HasParent() && len(args) > 0 { | ||
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0])) | ||
return fmt.Errorf(gotext.Get("LegacyArgsValidationError"), args[0], cmd.CommandPath(), cmd.findSuggestions(args[0])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about leaving the original english string as the index for all the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve looked at gotext in more detail, and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the crux of the matter. I've done a whole bunch of implementations of translations in the past, and every time I try to use plain english instead of keys I end up either regretting it or refactoring heavily to keys. Here's some food for thought when using raw english as keys:
I'm very aware that using keys makes the code harder to read and understand. This is mitigated a little by a careful choice of the wording of the key. I had to pick one way or the other ; it was not an easy choice, but it's one I made many times and I decided to go with hindsight from past experiences. I'm not adamant on this, quite the contrary. |
||
} | ||
return nil | ||
} | ||
|
||
// NoArgs returns an error if any args are included. | ||
func NoArgs(cmd *Command, args []string) error { | ||
if len(args) > 0 { | ||
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath()) | ||
return fmt.Errorf(gotext.Get("NoArgsValidationError"), args[0], cmd.CommandPath()) | ||
} | ||
return nil | ||
} | ||
|
@@ -58,7 +59,7 @@ | |
} | ||
for _, v := range args { | ||
if !stringInSlice(v, validArgs) { | ||
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0])) | ||
return fmt.Errorf(gotext.Get("OnlyValidArgsValidationError"), v, cmd.CommandPath(), cmd.findSuggestions(args[0])) | ||
} | ||
} | ||
} | ||
|
@@ -74,7 +75,7 @@ | |
func MinimumNArgs(n int) PositionalArgs { | ||
return func(cmd *Command, args []string) error { | ||
if len(args) < n { | ||
return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args)) | ||
return fmt.Errorf(gotext.GetN("MinimumNArgsValidationError", "MinimumNArgsValidationErrorPlural", n), n, len(args)) | ||
} | ||
return nil | ||
} | ||
|
@@ -84,7 +85,7 @@ | |
func MaximumNArgs(n int) PositionalArgs { | ||
return func(cmd *Command, args []string) error { | ||
if len(args) > n { | ||
return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args)) | ||
return fmt.Errorf(gotext.GetN("MaximumNArgsValidationError", "MaximumNArgsValidationErrorPlural", n), n, len(args)) | ||
} | ||
return nil | ||
} | ||
|
@@ -94,7 +95,7 @@ | |
func ExactArgs(n int) PositionalArgs { | ||
return func(cmd *Command, args []string) error { | ||
if len(args) != n { | ||
return fmt.Errorf("accepts %d arg(s), received %d", n, len(args)) | ||
return fmt.Errorf(gotext.GetN("ExactArgsValidationError", "ExactArgsValidationErrorPlural", n), n, len(args)) | ||
} | ||
return nil | ||
} | ||
|
@@ -104,7 +105,7 @@ | |
func RangeArgs(min int, max int) PositionalArgs { | ||
return func(cmd *Command, args []string) error { | ||
if len(args) < min || len(args) > max { | ||
return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args)) | ||
return fmt.Errorf(gotext.GetN("RangeArgsValidationError", "RangeArgsValidationErrorPlural", max), min, max, len(args)) | ||
} | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand correctly that whenever a file changes, this make target must be re-run to update the line numbers in the
default.pot
file?If that is the case, then it is essential that we run this make target in
.github/test.yml
and check ifdefault.pot
(or anything else) gets changed. I don't expect many contributors to know they have to run this, so CI should fail if they don't