Skip to content

Commit

Permalink
Isolate locale-handling functions from i18n interface
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmaglie committed Nov 29, 2024
1 parent a3c9f72 commit b87a215
Show file tree
Hide file tree
Showing 54 changed files with 90 additions and 55 deletions.
10 changes: 5 additions & 5 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,24 +309,24 @@ tasks:
i18n:update:
desc: Updates i18n files
cmds:
- go run ./internal/i18n/cmd/main.go catalog generate . > ./internal/i18n/data/en.po
- go run ./internal/locales/cmd/main.go catalog generate . > ./internal/locales/data/en.po

i18n:pull:
desc: Pull i18n files from transifex
cmds:
- go run ./internal/i18n/cmd/main.go transifex pull ./internal/i18n/data
- go run ./internal/locales/cmd/main.go transifex pull ./internal/locales/data

i18n:push:
desc: Push i18n files to transifex
cmds:
- go run ./internal/i18n/cmd/main.go transifex push ./internal/i18n/data
- go run ./internal/locales/cmd/main.go transifex push ./internal/locales/data

i18n:check:
desc: Check if the i18n message catalog was updated
cmds:
- task: i18n:pull
- git add -N ./internal/i18n/data
- git diff --exit-code ./internal/i18n/data
- git add -N ./internal/locales/data
- git diff --exit-code ./internal/locales/data

# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-mkdocs-task/Taskfile.yml
website:check:
Expand Down
3 changes: 2 additions & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/arduino/arduino-cli/internal/arduino/sketch"
"github.com/arduino/arduino-cli/internal/arduino/utils"
"github.com/arduino/arduino-cli/internal/i18n"
"github.com/arduino/arduino-cli/internal/locales"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -420,7 +421,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
// language of the CLI if the locale is different
// after started.
if locale, ok, _ := s.settings.GetStringOk("locale"); ok {
i18n.Init(locale)
locales.Init(locale)
}

return nil
Expand Down
41 changes: 19 additions & 22 deletions internal/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,28 @@

package i18n

// Init initializes the i18n module, setting the locale according to this order of preference:
// 1. Locale specified via the function call
// 2. OS Locale
// 3. en (default)
func Init(configLocale string) {
locales := supportedLocales()
if configLocale != "" {
if locale := findMatchingLocale(configLocale, locales); locale != "" {
setLocale(locale)
return
}
}

if osLocale := getLocaleIdentifierFromOS(); osLocale != "" {
if locale := findMatchingLocale(osLocale, locales); locale != "" {
setLocale(locale)
return
}
}

setLocale("en")
import "fmt"

type Locale interface {
Get(msg string, args ...interface{}) string
}

type nullLocale struct{}

func (n nullLocale) Parse([]byte) {}

func (n nullLocale) Get(msg string, args ...interface{}) string {
return fmt.Sprintf(msg, args...)
}

var locale Locale = &nullLocale{}

func SetLocale(l Locale) {
locale = l
}

// Tr returns msg translated to the selected locale
// the msg argument must be a literal string
func Tr(msg string, args ...interface{}) string {
return po.Get(msg, args...)
return locale.Get(msg, args...)
}
7 changes: 4 additions & 3 deletions internal/i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

func setPo(poFile string) {
po = gotext.NewPo()
po.Parse([]byte(poFile))
dict := gotext.NewPo()
dict.Parse([]byte(poFile))
SetLocale(dict)
}

func TestPoTranslation(t *testing.T) {
Expand All @@ -39,7 +40,7 @@ func TestPoTranslation(t *testing.T) {
}

func TestNoLocaleSet(t *testing.T) {
po = gotext.NewPo()
locale = gotext.NewPo()
require.Equal(t, "test-key", Tr("test-key"))
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"path/filepath"
"strconv"

"github.com/arduino/arduino-cli/internal/i18n/cmd/po"
"github.com/arduino/arduino-cli/internal/locales/cmd/po"
)

// GenerateCatalog generates the i18n message catalog for the go source files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"os"
"path/filepath"

"github.com/arduino/arduino-cli/internal/i18n/cmd/ast"
"github.com/arduino/arduino-cli/internal/locales/cmd/ast"
"github.com/spf13/cobra"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package commands

import (
"github.com/arduino/arduino-cli/internal/i18n/cmd/commands/catalog"
"github.com/arduino/arduino-cli/internal/i18n/cmd/commands/transifex"
"github.com/arduino/arduino-cli/internal/locales/cmd/commands/catalog"
"github.com/arduino/arduino-cli/internal/locales/cmd/commands/transifex"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/i18n/cmd/main.go → internal/locales/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"os"

"github.com/arduino/arduino-cli/internal/i18n/cmd/commands"
"github.com/arduino/arduino-cli/internal/locales/cmd/commands"
)

func main() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/i18n/convert.go → internal/locales/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

import (
"regexp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

import (
"fmt"
Expand Down
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.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/i18n/detect.go → internal/locales/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//go:build darwin && cgo

package i18n
package locales

/*
#cgo CFLAGS: -x objective-c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

func getLocaleIdentifier() string {
return getLocaleIdentifierFromEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

func getLocaleIdentifier() string {
return getLocaleIdentifierFromEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//go:build darwin && !cgo

package i18n
package locales

func getLocaleIdentifier() string {
return getLocaleIdentifierFromEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

import (
"strings"
Expand Down
39 changes: 39 additions & 0 deletions internal/locales/i18n.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package locales

// Init initializes the i18n module, setting the locale according to this order of preference:
// 1. Locale specified via the function call
// 2. OS Locale
// 3. en (default)
func Init(configLocale string) {
locales := supportedLocales()
if configLocale != "" {
if locale := findMatchingLocale(configLocale, locales); locale != "" {
setLocale(locale)
return
}
}

if osLocale := getLocaleIdentifierFromOS(); osLocale != "" {
if locale := findMatchingLocale(osLocale, locales); locale != "" {
setLocale(locale)
return
}
}

setLocale("en")
}
14 changes: 5 additions & 9 deletions internal/i18n/locale.go → internal/locales/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

import (
"embed"
"strings"

"github.com/arduino/arduino-cli/internal/i18n"
"github.com/leonelquinteros/gotext"
)

var po *gotext.Po

//go:embed data/*.po
var contents embed.FS

func init() {
po = gotext.NewPo()
}

func supportedLocales() []string {
var locales []string
files, err := contents.ReadDir("data")
Expand Down Expand Up @@ -75,6 +70,7 @@ func setLocale(locale string) {
if err != nil {
panic("Error reading embedded i18n data: " + err.Error())
}
po = gotext.NewPo()
po.Parse(poFile)
dict := gotext.NewPo()
dict.Parse(poFile)
i18n.SetLocale(dict)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package i18n
package locales

import (
"testing"
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/i18n"
"github.com/arduino/arduino-cli/internal/locales"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,7 +68,7 @@ func main() {
config := resp.GetConfiguration()

// Setup i18n
i18n.Init(config.GetLocale())
locales.Init(config.GetLocale())

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

0 comments on commit b87a215

Please sign in to comment.