Skip to content
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

Feat: Add minimal support for i18n #1789

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -7,7 +7,7 @@
GO_RUN_BUILD := go run internal/build/build.go

.PHONY: all
all: generate vet test check-binary-size gfmrun
all: generate go-generate vet test check-binary-size gfmrun

# NOTE: this is a special catch-all rule to run any of the commands
# defined in internal/build/build.go with optional arguments passed
Expand Down
2 changes: 1 addition & 1 deletion command.go
Expand Up @@ -368,7 +368,7 @@ func (cmd *Command) Run(ctx context.Context, arguments []string) (deferErr error
err = cCtx.Command.handleExitCoder(cCtx, err)
return err
}
_, _ = fmt.Fprintf(cCtx.Command.Root().ErrWriter, "%s %s\n\n", "Incorrect Usage:", err.Error())
_, _ = mprinter.Fprintf(cCtx.Command.Root().ErrWriter, "%s %s\n\n", "Incorrect Usage:", err.Error())
if cCtx.Command.Suggest {
if suggestion, err := cmd.suggestFlagFromError(err, ""); err == nil {
fmt.Fprintf(cCtx.Command.Root().ErrWriter, "%s", suggestion)
Expand Down
5 changes: 2 additions & 3 deletions completion.go
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"embed"
"fmt"
"sort"
)

Expand Down Expand Up @@ -50,12 +49,12 @@ func completionCommandAction(cCtx *Context) error {
sort.Strings(shells)

if cCtx.Args().Len() == 0 {
return Exit(fmt.Sprintf("no shell provided for completion command. available shells are %+v", shells), 1)
return Exit(mprinter.Sprintf("no shell provided for completion command. available shells are %+v", shells), 1)
}
s := cCtx.Args().First()

if rc, ok := shellCompletions[s]; !ok {
return Exit(fmt.Sprintf("unknown shell %s, available shells are %+v", s, shells), 1)
return Exit(mprinter.Sprintf("unknown shell %s, available shells are %+v", s, shells), 1)
} else if c, err := rc(cCtx.Command); err != nil {
return Exit(err, 1)
} else {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -10,5 +10,6 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -6,6 +6,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
2 changes: 1 addition & 1 deletion help.go
Expand Up @@ -316,7 +316,7 @@ func ShowVersion(cCtx *Context) {
}

func printVersion(cCtx *Context) {
_, _ = fmt.Fprintf(cCtx.Command.Root().Writer, "%v version %v\n", cCtx.Command.Name, cCtx.Command.Version)
_, _ = mprinter.Fprintf(cCtx.Command.Root().Writer, "%v version %v\n", cCtx.Command.Name, cCtx.Command.Version)
}

func handleTemplateError(err error) {
Expand Down
12 changes: 12 additions & 0 deletions i18n.go
@@ -0,0 +1,12 @@
package cli

import (
"golang.org/x/text/language"
"golang.org/x/text/message"
)

var mprinter *message.Printer

func init() {
mprinter = message.NewPrinter(language.AmericanEnglish)
}
4 changes: 4 additions & 0 deletions internal/build/build.go
Expand Up @@ -60,6 +60,10 @@ func main() {
Name: "vet",
Action: topRunAction("go", "vet", "./..."),
},
{
Name: "go-generate",
Action: topRunAction("go", "generate", "./..."),
},
{
Name: "test",
Action: TestActionFunc,
Expand Down
68 changes: 68 additions & 0 deletions internal/translations/catalog.go

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

89 changes: 89 additions & 0 deletions internal/translations/locales/en-CA/out.gotext.json
@@ -0,0 +1,89 @@
{
"language": "en-CA",
"messages": [
{
"id": "{Incorrect_Usage} {Error}",
"message": "{Incorrect_Usage} {Error}",
"translation": "",
"placeholders": [
{
"id": "Incorrect_Usage",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "\"Incorrect Usage:\""
},
{
"id": "Error",
"string": "%[2]s",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "err.Error()"
}
]
},
{
"id": "no shell provided for completion command. available shells are {Shells}",
"message": "no shell provided for completion command. available shells are {Shells}",
"translation": "",
"placeholders": [
{
"id": "Shells",
"string": "%+[1]v",
"type": "[]string",
"underlyingType": "[]string",
"argNum": 1,
"expr": "shells"
}
]
},
{
"id": "unknown shell {S}, available shells are {Shells}",
"message": "unknown shell {S}, available shells are {Shells}",
"translation": "",
"placeholders": [
{
"id": "S",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "s"
},
{
"id": "Shells",
"string": "%+[2]v",
"type": "[]string",
"underlyingType": "[]string",
"argNum": 2,
"expr": "shells"
}
]
},
{
"id": "{Name} version {Version}",
"message": "{Name} version {Version}",
"translation": "",
"placeholders": [
{
"id": "Name",
"string": "%[1]v",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "cCtx.Command.Name"
},
{
"id": "Version",
"string": "%[2]v",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "cCtx.Command.Version"
}
]
}
]
}
97 changes: 97 additions & 0 deletions internal/translations/locales/en-GB/messages.gotext.json
@@ -0,0 +1,97 @@
{
"language": "en-GB",
"messages": [
{
"id": "{Incorrect_Usage} {Error}",
"message": "{Incorrect_Usage} {Error}",
"translation": "{Incorrect_Usage} {Error}",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "Incorrect_Usage",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "\"Incorrect Usage:\""
},
{
"id": "Error",
"string": "%[2]s",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "err.Error()"
}
],
"fuzzy": true
},
{
"id": "no shell provided for completion command. available shells are {Shells}",
"message": "no shell provided for completion command. available shells are {Shells}",
"translation": "no shell provided for completion command. available shells are {Shells}",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "Shells",
"string": "%+[1]v",
"type": "[]string",
"underlyingType": "[]string",
"argNum": 1,
"expr": "shells"
}
],
"fuzzy": true
},
{
"id": "unknown shell {S}, available shells are {Shells}",
"message": "unknown shell {S}, available shells are {Shells}",
"translation": "unknown shell {S}, available shells are {Shells}",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "S",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "s"
},
{
"id": "Shells",
"string": "%+[2]v",
"type": "[]string",
"underlyingType": "[]string",
"argNum": 2,
"expr": "shells"
}
],
"fuzzy": true
},
{
"id": "{Name} version {Version}",
"message": "{Name} version {Version}",
"translation": "{Name} version {Version}",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "Name",
"string": "%[1]v",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "cCtx.Command.Name"
},
{
"id": "Version",
"string": "%[2]v",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "cCtx.Command.Version"
}
],
"fuzzy": true
}
]
}