Skip to content

Commit 7b68b48

Browse files
authored
[breaking] Pass user locale preference directly to i18n package (#1365)
The user can configure their locale preference via the `locale` configuration key. This information is given priority by the `github.com/arduino/arduino-cli/configuration` package over the automatically detected locale. Previously, the `i18n` package got the configuration setting from the `github.com/arduino/arduino-cli/configuration` package, but this will result in an import cycle when the `i18n` package is used to enable translation of the output strings of the `configuration` package. To avoid this, the caller now reads the configuration and passes the locale code to the `i18n` package via its `Init` function: i18n.Init("it") The argument can be omitted if only automated locale detection is needed: i18n.Init()
1 parent fc367b7 commit 7b68b48

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

Diff for: docs/UPGRADING.md

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ Here you can find a list of migration guides to handle breaking changes between
66

77
### Change public library interface
88

9+
#### `github.com/arduino/arduino-cli/i18n` package
10+
11+
The behavior of the `Init` function has changed. The user specified locale code is no longer read from the
12+
`github.com/arduino/arduino-cli/configuration` package and now must be passed directly to `Init` as a string:
13+
14+
```go
15+
i18n.Init("it")
16+
```
17+
18+
Omit the argument for automated locale detection:
19+
20+
```go
21+
i18n.Init()
22+
```
23+
924
#### `github.com/arduino/arduino-cli/arduino/builder` package
1025

1126
`GenBuildPath()` function has been moved to `github.com/arduino/arduino-cli/arduino/sketch` package. The signature is

Diff for: i18n/i18n.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515

1616
package i18n
1717

18-
import "github.com/arduino/arduino-cli/configuration"
19-
2018
// Init initializes the i18n module, setting the locale according to this order of preference:
21-
// 1. Configuration set in arduino-cli.yaml
19+
// 1. Locale specified via the function call
2220
// 2. OS Locale
2321
// 3. en (default)
24-
func Init() {
22+
func Init(configLocale ...string) {
2523
initRiceBox()
2624
locales := supportedLocales()
2725

28-
if configLocale := configuration.Settings.GetString("locale"); configLocale != "" {
29-
if locale := findMatchingLocale(configLocale, locales); locale != "" {
26+
if len(configLocale) > 1 {
27+
panic("Multiple arguments not supported")
28+
}
29+
30+
if len(configLocale) > 0 && configLocale[0] != "" {
31+
if locale := findMatchingLocale(configLocale[0], locales); locale != "" {
3032
setLocale(locale)
3133
return
3234
}

Diff for: main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func main() {
2828
configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args))
29-
i18n.Init()
29+
i18n.Init(configuration.Settings.GetString("locale"))
3030
arduinoCmd := cli.NewCommand()
3131
if err := arduinoCmd.Execute(); err != nil {
3232
os.Exit(errorcodes.ErrGeneric)

0 commit comments

Comments
 (0)