Skip to content

Commit 9ac3024

Browse files
first pass at updating go-i18n library
1 parent 6ddec1b commit 9ac3024

5 files changed

Lines changed: 35 additions & 18 deletions

File tree

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/Xuanwo/go-locale v1.1.0
1313
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1
1414
github.com/miekg/dns v1.1.58
15-
github.com/nicksnyder/go-i18n v1.10.3
15+
github.com/nicksnyder/go-i18n/v2 v2.4.0
1616
github.com/onsi/ginkgo/v2 v2.17.0
1717
github.com/onsi/gomega v1.31.1
1818
github.com/softlayer/softlayer-go v1.1.3
@@ -36,8 +36,6 @@ require (
3636
github.com/mattn/go-colorable v0.1.13 // indirect
3737
github.com/mattn/go-isatty v0.0.20 // indirect
3838
github.com/mattn/go-runewidth v0.0.15 // indirect
39-
github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect
40-
github.com/pelletier/go-toml v1.9.5 // indirect
4139
github.com/pmezard/go-difflib v1.0.0 // indirect
4240
github.com/rivo/uniseg v0.4.7 // indirect
4341
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e // indirect

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,12 @@ github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1 h1:NicmruxkeqHjDv03SfSxqmaLuisdd
5252
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1/go.mod h1:eyp4DdUJAKkr9tvxR3jWhw2mDK7CWABMG5r9uyaKC7I=
5353
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
5454
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
55-
github.com/nicksnyder/go-i18n v1.10.3 h1:0U60fnLBNrLBVt8vb8Q67yKNs+gykbQuLsIkiesJL+w=
56-
github.com/nicksnyder/go-i18n v1.10.3/go.mod h1:hvLG5HTlZ4UfSuVLSRuX7JRUomIaoKQM19hm6f+no7o=
5755
github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM=
5856
github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4=
5957
github.com/onsi/ginkgo/v2 v2.17.0 h1:kdnunFXpBjbzN56hcJHrXZ8M+LOkenKA7NnBzTNigTI=
6058
github.com/onsi/ginkgo/v2 v2.17.0/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
6159
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
6260
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
63-
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
64-
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
65-
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
6661
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6762
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6863
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
@@ -132,7 +127,6 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
132127
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
133128
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
134129
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
135-
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
136130
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
137131
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
138132
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

plugin/i18n/i18n.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package i18n
33
import (
44
"path/filepath"
55
"strings"
6-
7-
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config"
6+
"golang.org/x/text/language"
7+
"encoding/json"
8+
"fmt"
9+
// "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config"
810
"github.com/Xuanwo/go-locale"
9-
goi18n "github.com/nicksnyder/go-i18n/i18n"
10-
"github.ibm.com/SoftLayer/softlayer-cli/plugin/resources"
11+
goi18n "github.com/nicksnyder/go-i18n/v2/i18n"
12+
// "github.ibm.com/SoftLayer/softlayer-cli/plugin/resources"
1113
)
1214

1315
const (
@@ -28,6 +30,7 @@ var SUPPORTED_LOCALES = []string{
2830
}
2931

3032
var resourcePath = filepath.Join("plugin", "i18n", "resources")
33+
var localizer = Init()
3134

3235
func GetResourcePath() string {
3336
return resourcePath
@@ -37,14 +40,34 @@ func SetResourcePath(path string) {
3740
resourcePath = path
3841
}
3942

40-
var T goi18n.TranslateFunc = Init(core_config.NewCoreConfig(func(error) {}))
43+
// var T goi18n.TranslateFunc = Init(core_config.NewCoreConfig(func(error) {}))
44+
45+
func T(text string, subs ...interface{}) string {
46+
47+
// fmt.Printf("SUBS: %v\n", subs)
48+
message := &goi18n.Message{ID: text, Other: text}
49+
config := &goi18n.LocalizeConfig{DefaultMessage: message}
50+
l_string, err := localizer.Localize(config)
51+
if err != nil {
52+
fmt.Printf("ERROR i18n\n")
53+
return err.Error()
54+
}
55+
return l_string
56+
}
57+
58+
4159

42-
func Init(coreConfig core_config.Repository) goi18n.TranslateFunc {
43-
userLocale := coreConfig.Locale()
44-
locale := supportedLocale(userLocale)
45-
return initWithLocale(locale)
60+
61+
func Init() *goi18n.Localizer {
62+
bundle := goi18n.NewBundle(language.English)
63+
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
64+
bundle.MustLoadMessageFile("plugin/i18n/resources/en_US.json")
65+
bundle.MustLoadMessageFile("plugin/i18n/resources/ja_JP.json")
66+
loc := goi18n.NewLocalizer(bundle, language.English.String())
67+
return loc
4668
}
4769

70+
/*
4871
func initWithLocale(locale string) goi18n.TranslateFunc {
4972
err := loadFromAsset(locale)
5073
if err != nil {
@@ -64,6 +87,7 @@ func loadFromAsset(locale string) (err error) {
6487
return
6588
}
6689
90+
*/
6791
// Tries to determine the system locale, when local isn't set, default to en_US
6892
func DetectLocal() string {
6993
tag, err := locale.Detect()
@@ -75,6 +99,7 @@ func DetectLocal() string {
7599
return locale
76100
}
77101

102+
78103
// Tries to match the system locale with a supported locale, otherwise sets a DEFAULT_LOCALE
79104
func supportedLocale(configLocal string) string {
80105

0 commit comments

Comments
 (0)