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

Diff for: Taskfile.yml

+5-5
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:

Diff for: commands/instances.go

+2-1
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

Diff for: internal/i18n/i18n.go

+19-22
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
}

Diff for: internal/i18n/i18n_test.go

+4-3
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

Diff for: internal/i18n/cmd/ast/parser.go renamed to internal/locales/cmd/ast/parser.go

+1-1
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

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

+1-1
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

Diff for: internal/i18n/cmd/commands/root.go renamed to internal/locales/cmd/commands/root.go

+2-2
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

Diff for: internal/i18n/cmd/main.go renamed to internal/locales/cmd/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121

22-
"github.com/arduino/arduino-cli/internal/i18n/cmd/commands"
22+
"github.com/arduino/arduino-cli/internal/locales/cmd/commands"
2323
)
2424

2525
func main() {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: internal/i18n/convert.go renamed to internal/locales/convert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
import (
1919
"regexp"

Diff for: internal/i18n/convert_test.go renamed to internal/locales/convert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
import (
1919
"fmt"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: internal/i18n/detect.go renamed to internal/locales/detect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
import (
1919
"os"

Diff for: internal/i18n/detect_cgo_darwin.go renamed to internal/locales/detect_cgo_darwin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
//go:build darwin && cgo
1717

18-
package i18n
18+
package locales
1919

2020
/*
2121
#cgo CFLAGS: -x objective-c

Diff for: internal/i18n/detect_freebsd.go renamed to internal/locales/detect_freebsd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
func getLocaleIdentifier() string {
1919
return getLocaleIdentifierFromEnv()

Diff for: internal/i18n/detect_linux.go renamed to internal/locales/detect_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
func getLocaleIdentifier() string {
1919
return getLocaleIdentifierFromEnv()

Diff for: internal/i18n/detect_nocgo_darwin.go renamed to internal/locales/detect_nocgo_darwin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
//go:build darwin && !cgo
1717

18-
package i18n
18+
package locales
1919

2020
func getLocaleIdentifier() string {
2121
return getLocaleIdentifierFromEnv()

Diff for: internal/i18n/detect_windows.go renamed to internal/locales/detect_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
import (
1919
"strings"

Diff for: internal/locales/i18n.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package locales
17+
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")
39+
}

Diff for: internal/i18n/locale.go renamed to internal/locales/locale.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,19 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
import (
1919
"embed"
2020
"strings"
2121

22+
"github.com/arduino/arduino-cli/internal/i18n"
2223
"github.com/leonelquinteros/gotext"
2324
)
2425

25-
var po *gotext.Po
26-
2726
//go:embed data/*.po
2827
var contents embed.FS
2928

30-
func init() {
31-
po = gotext.NewPo()
32-
}
33-
3429
func supportedLocales() []string {
3530
var locales []string
3631
files, err := contents.ReadDir("data")
@@ -75,6 +70,7 @@ func setLocale(locale string) {
7570
if err != nil {
7671
panic("Error reading embedded i18n data: " + err.Error())
7772
}
78-
po = gotext.NewPo()
79-
po.Parse(poFile)
73+
dict := gotext.NewPo()
74+
dict.Parse(poFile)
75+
i18n.SetLocale(dict)
8076
}

Diff for: internal/i18n/locale_test.go renamed to internal/locales/locale_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package i18n
16+
package locales
1717

1818
import (
1919
"testing"

Diff for: main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/arduino/arduino-cli/internal/cli/configuration"
2828
"github.com/arduino/arduino-cli/internal/cli/feedback"
2929
"github.com/arduino/arduino-cli/internal/i18n"
30+
"github.com/arduino/arduino-cli/internal/locales"
3031
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3132
"github.com/arduino/go-paths-helper"
3233
"github.com/sirupsen/logrus"
@@ -67,7 +68,7 @@ func main() {
6768
config := resp.GetConfiguration()
6869

6970
// Setup i18n
70-
i18n.Init(config.GetLocale())
71+
locales.Init(config.GetLocale())
7172

7273
// Setup command line parser with the server and settings
7374
arduinoCmd := cli.NewCommand(srv)

0 commit comments

Comments
 (0)