Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Updated go-i18n API to support v2 and v1 #99

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmds/rewrite_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ import (
"path/filepath"

i18n "github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc
var T i18n.TranslateFunc
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callout: To avoid disruption the API i18n.TranslateFunc method looks the same as the TranslateFunc from goi18n from version 1


func init() {
T = i18n.Init(__FULL_IMPORT_PATH__, i18n.GetResourcesPath())
Expand Down
2 changes: 1 addition & 1 deletion examples/demo1-i18n/i18n_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/pivotal-cf-experimental/jibber_jabber"

goi18n "github.com/nicksnyder/go-i18n/i18n"
goi18n "github.com/maximilien/i18n4go/i18n"
)

type Detector interface {
Expand Down
4 changes: 2 additions & 2 deletions examples/i18n_init/cf_cli_i18n_init.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/pivotal-cf-experimental/jibber_jabber"

goi18n "github.com/nicksnyder/go-i18n/i18n"
goi18n "github.com/maximilien/i18n4go/i18n"
)

type Detector interface {
Expand Down Expand Up @@ -175,4 +175,4 @@ func (detector *JibberJabberDetector) DetectIETF() (string, error) {

func (detector *JibberJabberDetector) DetectLanguage() (string, error) {
return jibber_jabber.DetectLanguage()
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ go 1.18

require (
github.com/nicksnyder/go-i18n v1.10.1
github.com/nicksnyder/go-i18n/v2 v2.2.1
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.27.7
github.com/pivotal-cf-experimental/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
github.com/spf13/cobra v1.7.0
golang.org/x/text v0.9.0
)

require (
Expand All @@ -21,7 +23,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 h1:tuijfIjZyjZaHq9xDUh0tNitwXshJpbLkqMOJv4H3do=
Expand Down Expand Up @@ -36,6 +37,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/nicksnyder/go-i18n v1.10.1 h1:isfg77E/aCD7+0lD/D00ebR2MV5vgeQ276WYyDaCRQc=
github.com/nicksnyder/go-i18n v1.10.1/go.mod h1:e4Di5xjP9oTVrC6y3C7C0HoSYXjSbhh/dU0eUV32nB4=
github.com/nicksnyder/go-i18n/v2 v2.2.1 h1:aOzRCdwsJuoExfZhoiXHy4bjruwCMdt5otbYojM/PaA=
github.com/nicksnyder/go-i18n/v2 v2.2.1/go.mod h1:fF2++lPHlo+/kPaj3nB0uxtPwzlPm+BlgwGX7MkeGj0=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand All @@ -61,20 +64,26 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -83,16 +92,25 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 1 addition & 2 deletions i18n/i18n_init.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"path/filepath"

i18n "github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc

func init() {
T = i18n.Init(__FULL_IMPORT_PATH__, i18n.GetResourcesPath())
}
}
98 changes: 88 additions & 10 deletions i18n/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package i18n

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -24,14 +25,17 @@ import (

"github.com/pivotal-cf-experimental/jibber_jabber"

go_i18n "github.com/nicksnyder/go-i18n/i18n"
go_i18n "github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)

const (
DEFAULT_LOCALE = "en_US"
DEFAULT_LANGUAGE = "en"
)

type TranslateFunc func(translateID string, args ...interface{}) string

var SUPPORTED_LOCALES = map[string]string{
"de": "de_DE",
"en": "en_US",
Expand All @@ -44,24 +48,29 @@ var SUPPORTED_LOCALES = map[string]string{
"ru": "ru_RU",
"zh": "zh_CN",
}
var RESOUCES_PATH = filepath.Join("cf", "i18n", "resources")
var (
RESOUCES_PATH = filepath.Join("cf", "i18n", "resources")
bundle *go_i18n.Bundle
)

func GetResourcesPath() string {
return RESOUCES_PATH
}

func Init(packageName string, i18nDirname string) go_i18n.TranslateFunc {
userLocale, err := initWithUserLocale(packageName, i18nDirname)
if err != nil {
userLocale = mustLoadDefaultLocale(packageName, i18nDirname)
func init() {
if bundle == nil {
bundle = go_i18n.NewBundle(language.AmericanEnglish)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
}
}

T, err := go_i18n.Tfunc(userLocale, DEFAULT_LOCALE)
func Init(packageName string, i18nDirname string) TranslateFunc {
userLocale, err := initWithUserLocale(packageName, i18nDirname)
if err != nil {
panic(err)
userLocale = mustLoadDefaultLocale(packageName, i18nDirname)
}

return T
return Tfunc(userLocale, DEFAULT_LOCALE)
}

func initWithUserLocale(packageName, i18nDirname string) (string, error) {
Expand Down Expand Up @@ -127,7 +136,7 @@ func loadFromAsset(packageName, assetPath, locale, language string) error {
return err
}

go_i18n.MustLoadTranslationFile(fileName)
bundle.MustLoadMessageFile(fileName)

os.RemoveAll(fileName)

Expand All @@ -149,3 +158,72 @@ func saveLanguageFileToDisk(tmpDir, assetName string, byteArray []byte) (fileNam

return
}

func isNumber(value interface{}) bool {
switch value.(type) {
case int, int8, int16, int32, int64:
return true
}
return false
}

func SupportedLocaleLanguageTags() []language.Tag {
tags := []language.Tag{language.English}
for _, locale := range SUPPORTED_LOCALES {
tag, _ := language.Parse(locale)
tags = append(tags, tag)
}

return tags
}

// translate is a wrapper function that is based on the translate method for v1.3.0
// To allow compatibility v2.0+ and older a wrapper method was created
// @see https://github.com/nicksnyder/go-i18n/blob/v1.3.0/i18n/bundle/bundle.go#L227-L257
func translate(loc *go_i18n.Localizer) TranslateFunc {
return func(messageId string, args ...interface{}) string {
var (
count interface{}
data interface{}
)

if argc := len(args); argc > 0 {
if isNumber(args[0]) {
count = args[0]
if argc > 1 {
data = args[1]
}
} else {
data = args[0]
}
}

return loc.MustLocalize(&go_i18n.LocalizeConfig{
MessageID: messageId,
TemplateData: data,
PluralCount: count,
})
}
}

// Tfunc will return a method of TranslateFunc type to be used to tranlation messages
func Tfunc(sources ...string) TranslateFunc {
localizer := go_i18n.NewLocalizer(bundle, DEFAULT_LOCALE)
tfunc := translate(localizer)
for _, s := range sources {
if s == "" {
Copy link
Collaborator Author

@Aerex Aerex Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callout: If we can't determine the language source we will just skip it. I am open to other suggestions.

continue
}

if s == DEFAULT_LOCALE {
return tfunc
}
localizer := go_i18n.NewLocalizer(bundle, s)
t := translate(localizer)
return func(translationID string, args ...interface{}) string {
return t(translationID, args...)
}
}

return tfunc
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"path/filepath"

i18n "github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc
var T i18n.TranslateFunc

func init() {
T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "f_option", "input_files"), i18n.GetResourcesPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"path/filepath"

i18n "github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc
var T i18n.TranslateFunc

func init() {
T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "f_option", "input_files", "nested_dir"), i18n.GetResourcesPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"path/filepath"

i18n "github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc
var T i18n.TranslateFunc

func init() {
T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "init_code_snippet_filename", "input_files"), i18n.GetResourcesPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"path/filepath"

"github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc
var T i18n.TranslateFunc

func init() {
fmt.Println("DEBUG: this is a test i18n_init.go file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"path/filepath"

"github.com/maximilien/i18n4go/i18n"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)

var T goi18n.TranslateFunc
var T i18n.TranslateFunc

func init() {
fmt.Println("DEBUG: this is a test i18n_init.go file")
Expand Down