Skip to content

Commit c740326

Browse files
committed
Fixed i18n early static initialization problem
#1425 (comment) these particular strings are not correctly handled by the i18n package because they are declared at package level before the call to i18n.Init(), and so are just returned as-is.
1 parent fcd21fe commit c740326

24 files changed

+199
-253
lines changed

Diff for: arduino/libraries/librariesmanager/install.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package librariesmanager
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221
"net/url"
2322
"os"
@@ -32,10 +31,16 @@ import (
3231
"gopkg.in/src-d/go-git.v4"
3332
)
3433

34+
type AlreadyInstalledError struct{}
35+
36+
func (e *AlreadyInstalledError) Error() string {
37+
return tr("library already installed")
38+
}
39+
3540
var (
3641
// ErrAlreadyInstalled is returned when a library is already installed and task
3742
// cannot proceed.
38-
ErrAlreadyInstalled = errors.New(tr("library already installed"))
43+
ErrAlreadyInstalled = &AlreadyInstalledError{}
3944
)
4045

4146
// InstallPrerequisiteCheck performs prequisite checks to install a library. It returns the

Diff for: cli/cli.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func NewCommand() *cobra.Command {
7575
PersistentPostRun: postRun,
7676
}
7777

78-
arduinoCli.SetUsageTemplate(usageTemplate)
78+
arduinoCli.SetUsageTemplate(getUsageTemplate())
7979

8080
createCliCommandTree(arduinoCli)
8181

Diff for: cli/usage.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ import (
1919
"github.com/arduino/arduino-cli/i18n"
2020
)
2121

22-
// Declare ids used in usage
23-
var (
24-
tr = i18n.Tr
25-
_ = tr("Usage:")
26-
_ = tr("Aliases:")
27-
_ = tr("Examples:")
28-
_ = tr("Available Commands:")
29-
_ = tr("Flags:")
30-
_ = tr("Global Flags:")
31-
_ = tr("Additional help topics:")
32-
_ = tr("Use %s for more information about a command.")
33-
)
34-
35-
const usageTemplate = `{{tr "Usage:"}}{{if .Runnable}}
22+
var tr = i18n.Tr
23+
24+
func getUsageTemplate() string {
25+
// Force i18n to generate translation strings
26+
_ = tr("Usage:")
27+
_ = tr("Aliases:")
28+
_ = tr("Examples:")
29+
_ = tr("Available Commands:")
30+
_ = tr("Flags:")
31+
_ = tr("Global Flags:")
32+
_ = tr("Additional help topics:")
33+
_ = tr("Use %s for more information about a command.")
34+
35+
return `{{tr "Usage:"}}{{if .Runnable}}
3636
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
3737
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
3838
@@ -56,3 +56,4 @@ const usageTemplate = `{{tr "Usage:"}}{{if .Runnable}}
5656
5757
{{tr "Use %s for more information about a command." (printf "%s %s" .CommandPath "[command] --help" | printf "%q")}}{{end}}
5858
`
59+
}

Diff for: commands/board/list.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ import (
3535
"github.com/sirupsen/logrus"
3636
)
3737

38+
type BoardNotFoundError struct{}
39+
40+
func (e *BoardNotFoundError) Error() string {
41+
return tr("board not found")
42+
}
43+
3844
var (
3945
// ErrNotFound is returned when the API returns 404
40-
ErrNotFound = errors.New(tr("board not found"))
46+
ErrNotFound = &BoardNotFoundError{}
4147
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
4248
validVidPid = regexp.MustCompile(`0[xX][a-fA-F\d]{4}`)
4349
)

0 commit comments

Comments
 (0)