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 all commits
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: 2 additions & 0 deletions .github/workflows/cli.yml
Expand Up @@ -25,6 +25,8 @@ jobs:
- name: Set PATH
run: echo "${GITHUB_WORKSPACE}/.local/bin" >>"${GITHUB_PATH}"
- uses: actions/checkout@v3
- run: make ensure-gotext
- run: make go-generate
- if: matrix.go == '1.20.x' && matrix.os == 'ubuntu-latest'
run: make ensure-goimports
- if: matrix.go == '1.20.x' && matrix.os == 'ubuntu-latest'
Expand Down
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 @@ -384,7 +384,7 @@ func (cmd *Command) Run(ctx context.Context, osArgs []string) (deferErr error) {
err = cmd.handleExitCoder(ctx, err)
return err
}
fmt.Fprintf(cmd.Root().ErrWriter, "Incorrect Usage: %s\n\n", err.Error())
mprinter.Fprintf(cmd.Root().ErrWriter, "Incorrect Usage: %s\n\n", err.Error())
if cmd.Suggest {
if suggestion, err := cmd.suggestFlagFromError(err, ""); err == nil {
fmt.Fprintf(cmd.Root().ErrWriter, "%s", suggestion)
Expand Down
5 changes: 2 additions & 3 deletions completion.go
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"context"
"embed"
"fmt"
"sort"
)

Expand Down Expand Up @@ -51,12 +50,12 @@ func completionCommandAction(ctx context.Context, cmd *Command) error {
sort.Strings(shells)

if cmd.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 := cmd.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(cmd); 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 @@ -318,7 +318,7 @@ func ShowVersion(cmd *Command) {
}

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

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

import (
ts "github.com/urfave/cli/v3/internal/translations"
"golang.org/x/text/message"
)

var mprinter *message.Printer

func init() {
// TBD add language detection logic and fallback to en-US
mprinter = message.NewPrinter(message.MatchLanguage("en-US"), message.Catalog(ts.Catalog))
}
19 changes: 19 additions & 0 deletions internal/build/build.go
Expand Up @@ -64,6 +64,10 @@ func main() {
Name: "vet",
Action: topRunAction("go", "vet", "./..."),
},
{
Name: "go-generate",
Action: topRunAction("go", "generate", "./..."),
},
{
Name: "test",
Action: TestActionFunc,
Expand Down Expand Up @@ -95,6 +99,10 @@ func main() {
Name: "ensure-goimports",
Action: EnsureGoimportsActionFunc,
},
{
Name: "ensure-gotext",
Action: EnsureGoTextActionFunc,
},
{
Name: "ensure-gfmrun",
Action: EnsureGfmrunActionFunc,
Expand Down Expand Up @@ -535,6 +543,17 @@ func EnsureGoimportsActionFunc(ctx context.Context, cmd *cli.Command) error {
return runCmd(ctx, "go", "install", "golang.org/x/tools/cmd/goimports@latest")
}

func EnsureGoTextActionFunc(ctx context.Context, cmd *cli.Command) error {
top := cmd.String("top")
if err := os.Chdir(top); err != nil {
return err
}

os.Setenv("GOBIN", filepath.Join(top, ".local/bin"))

return runCmd(ctx, "go", "install", "golang.org/x/text/cmd/gotext@latest")
}

func EnsureGfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
top := cmd.String("top")
gfmrunExe := filepath.Join(top, ".local/bin/gfmrun")
Expand Down
12 changes: 12 additions & 0 deletions internal/translations/bcatalog.go
@@ -0,0 +1,12 @@
package translations

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

var oldCatalog catalog.Catalog

func init() {
oldCatalog = message.DefaultCatalog
}
70 changes: 70 additions & 0 deletions internal/translations/catalog.go

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

13 changes: 13 additions & 0 deletions internal/translations/dcatalog.go
dearchap marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,13 @@
package translations

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

var Catalog catalog.Catalog

func init() {
Catalog = message.DefaultCatalog
message.DefaultCatalog = oldCatalog
}
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"
}
]
}
]
}