Skip to content

Commit b87a215

Browse files
committed
Isolate locale-handling functions from i18n interface
This changes allows to make a clean i18n package (without dependency on a specific implementation of the translation package) that, in turn, allows to export packages that internally use i18n with the minimal dependency load.
1 parent a3c9f72 commit b87a215

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+90
-55
lines changed

Taskfile.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,24 +309,24 @@ tasks:
309309
i18n:update:
310310
desc: Updates i18n files
311311
cmds:
312-
- go run ./internal/i18n/cmd/main.go catalog generate . > ./internal/i18n/data/en.po
312+
- go run ./internal/locales/cmd/main.go catalog generate . > ./internal/locales/data/en.po
313313

314314
i18n:pull:
315315
desc: Pull i18n files from transifex
316316
cmds:
317-
- go run ./internal/i18n/cmd/main.go transifex pull ./internal/i18n/data
317+
- go run ./internal/locales/cmd/main.go transifex pull ./internal/locales/data
318318

319319
i18n:push:
320320
desc: Push i18n files to transifex
321321
cmds:
322-
- go run ./internal/i18n/cmd/main.go transifex push ./internal/i18n/data
322+
- go run ./internal/locales/cmd/main.go transifex push ./internal/locales/data
323323

324324
i18n:check:
325325
desc: Check if the i18n message catalog was updated
326326
cmds:
327327
- task: i18n:pull
328-
- git add -N ./internal/i18n/data
329-
- git diff --exit-code ./internal/i18n/data
328+
- git add -N ./internal/locales/data
329+
- git diff --exit-code ./internal/locales/data
330330

331331
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-mkdocs-task/Taskfile.yml
332332
website:check:

commands/instances.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/arduino/arduino-cli/internal/arduino/sketch"
3939
"github.com/arduino/arduino-cli/internal/arduino/utils"
4040
"github.com/arduino/arduino-cli/internal/i18n"
41+
"github.com/arduino/arduino-cli/internal/locales"
4142
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
4243
paths "github.com/arduino/go-paths-helper"
4344
"github.com/sirupsen/logrus"
@@ -420,7 +421,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
420421
// language of the CLI if the locale is different
421422
// after started.
422423
if locale, ok, _ := s.settings.GetStringOk("locale"); ok {
423-
i18n.Init(locale)
424+
locales.Init(locale)
424425
}
425426

426427
return nil

internal/i18n/i18n.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,28 @@
1515

1616
package i18n
1717

18-
// Init initializes the i18n module, setting the locale according to this order of preference:
19-
// 1. Locale specified via the function call
20-
// 2. OS Locale
21-
// 3. en (default)
22-
func Init(configLocale string) {
23-
locales := supportedLocales()
24-
if configLocale != "" {
25-
if locale := findMatchingLocale(configLocale, locales); locale != "" {
26-
setLocale(locale)
27-
return
28-
}
29-
}
30-
31-
if osLocale := getLocaleIdentifierFromOS(); osLocale != "" {
32-
if locale := findMatchingLocale(osLocale, locales); locale != "" {
33-
setLocale(locale)
34-
return
35-
}
36-
}
37-
38-
setLocale("en")
18+
import "fmt"
19+
20+
type Locale interface {
21+
Get(msg string, args ...interface{}) string
22+
}
23+
24+
type nullLocale struct{}
25+
26+
func (n nullLocale) Parse([]byte) {}
27+
28+
func (n nullLocale) Get(msg string, args ...interface{}) string {
29+
return fmt.Sprintf(msg, args...)
30+
}
31+
32+
var locale Locale = &nullLocale{}
33+
34+
func SetLocale(l Locale) {
35+
locale = l
3936
}
4037

4138
// Tr returns msg translated to the selected locale
4239
// the msg argument must be a literal string
4340
func Tr(msg string, args ...interface{}) string {
44-
return po.Get(msg, args...)
41+
return locale.Get(msg, args...)
4542
}

internal/i18n/i18n_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525
)
2626

2727
func setPo(poFile string) {
28-
po = gotext.NewPo()
29-
po.Parse([]byte(poFile))
28+
dict := gotext.NewPo()
29+
dict.Parse([]byte(poFile))
30+
SetLocale(dict)
3031
}
3132

3233
func TestPoTranslation(t *testing.T) {
@@ -39,7 +40,7 @@ func TestPoTranslation(t *testing.T) {
3940
}
4041

4142
func TestNoLocaleSet(t *testing.T) {
42-
po = gotext.NewPo()
43+
locale = gotext.NewPo()
4344
require.Equal(t, "test-key", Tr("test-key"))
4445
}
4546

internal/i18n/cmd/ast/parser.go renamed to internal/locales/cmd/ast/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"path/filepath"
2525
"strconv"
2626

27-
"github.com/arduino/arduino-cli/internal/i18n/cmd/po"
27+
"github.com/arduino/arduino-cli/internal/locales/cmd/po"
2828
)
2929

3030
// GenerateCatalog generates the i18n message catalog for the go source files

internal/i18n/cmd/commands/catalog/generate_catalog.go renamed to internal/locales/cmd/commands/catalog/generate_catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"os"
2020
"path/filepath"
2121

22-
"github.com/arduino/arduino-cli/internal/i18n/cmd/ast"
22+
"github.com/arduino/arduino-cli/internal/locales/cmd/ast"
2323
"github.com/spf13/cobra"
2424
)
2525

internal/i18n/cmd/commands/root.go renamed to internal/locales/cmd/commands/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package commands
1717

1818
import (
19-
"github.com/arduino/arduino-cli/internal/i18n/cmd/commands/catalog"
20-
"github.com/arduino/arduino-cli/internal/i18n/cmd/commands/transifex"
19+
"github.com/arduino/arduino-cli/internal/locales/cmd/commands/catalog"
20+
"github.com/arduino/arduino-cli/internal/locales/cmd/commands/transifex"
2121
"github.com/spf13/cobra"
2222
)
2323

0 commit comments

Comments
 (0)