From 40b8a3220b133889f17a54b9fa248cfc214cbe53 Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 22 Jul 2021 02:22:19 -0700 Subject: [PATCH] [breaking] Pass user locale preference directly to i18n package 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() --- docs/UPGRADING.md | 15 +++++++++++++++ i18n/i18n.go | 14 ++++++++------ main.go | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/docs/UPGRADING.md b/docs/UPGRADING.md index 39729a1c592..a6cfad9a5f9 100644 --- a/docs/UPGRADING.md +++ b/docs/UPGRADING.md @@ -6,6 +6,21 @@ Here you can find a list of migration guides to handle breaking changes between ### Change public library interface +#### `github.com/arduino/arduino-cli/i18n` package + +The behavior of the `Init` function has changed. The user specified locale code is no longer read from the +`github.com/arduino/arduino-cli/configuration` package and now must be passed directly to `Init` as a string: + +```go +i18n.Init("it") +``` + +Omit the argument for automated locale detection: + +```go +i18n.Init() +``` + #### `github.com/arduino/arduino-cli/arduino/builder` package `GenBuildPath()` function has been moved to `github.com/arduino/arduino-cli/arduino/sketch` package. The signature is diff --git a/i18n/i18n.go b/i18n/i18n.go index 6881922b96a..9a0ffead925 100644 --- a/i18n/i18n.go +++ b/i18n/i18n.go @@ -15,18 +15,20 @@ package i18n -import "github.com/arduino/arduino-cli/configuration" - // Init initializes the i18n module, setting the locale according to this order of preference: -// 1. Configuration set in arduino-cli.yaml +// 1. Locale specified via the function call // 2. OS Locale // 3. en (default) -func Init() { +func Init(configLocale ...string) { initRiceBox() locales := supportedLocales() - if configLocale := configuration.Settings.GetString("locale"); configLocale != "" { - if locale := findMatchingLocale(configLocale, locales); locale != "" { + if len(configLocale) > 1 { + panic("Multiple arguments not supported") + } + + if len(configLocale) > 0 && configLocale[0] != "" { + if locale := findMatchingLocale(configLocale[0], locales); locale != "" { setLocale(locale) return } diff --git a/main.go b/main.go index fd691f526c9..2405f612188 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ import ( func main() { configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args)) - i18n.Init() + i18n.Init(configuration.Settings.GetString("locale")) arduinoCmd := cli.NewCommand() if err := arduinoCmd.Execute(); err != nil { os.Exit(errorcodes.ErrGeneric)