Skip to content

Commit 1f75f3b

Browse files
Getting i18n/v2 working
1 parent 9ac3024 commit 1f75f3b

10 files changed

Lines changed: 31 additions & 15 deletions

File tree

plugin/i18n/i18n.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ var SUPPORTED_LOCALES = []string{
3232
var resourcePath = filepath.Join("plugin", "i18n", "resources")
3333
var localizer = Init()
3434

35+
36+
// var matcher = InitMatcher()
37+
38+
39+
// func InitMatcher() language.Matcher {
40+
// var supported []language.Tag
41+
// for _, lang := range SUPPORTED_LOCALES {
42+
// supported = append(supported, language.MustParse(lang))
43+
// }
44+
// return language.NewMatcher(supported)
45+
// }
46+
3547
func GetResourcePath() string {
3648
return resourcePath
3749
}
@@ -49,7 +61,7 @@ func T(text string, subs ...interface{}) string {
4961
config := &goi18n.LocalizeConfig{DefaultMessage: message}
5062
l_string, err := localizer.Localize(config)
5163
if err != nil {
52-
fmt.Printf("ERROR i18n\n")
64+
fmt.Printf("ERROR i18n\n%v\n", err.Error())
5365
return err.Error()
5466
}
5567
return l_string
@@ -63,19 +75,23 @@ func Init() *goi18n.Localizer {
6375
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
6476
bundle.MustLoadMessageFile("plugin/i18n/resources/en_US.json")
6577
bundle.MustLoadMessageFile("plugin/i18n/resources/ja_JP.json")
66-
loc := goi18n.NewLocalizer(bundle, language.English.String())
78+
loc := goi18n.NewLocalizer(bundle, "ja_JP")
6779
return loc
6880
}
6981

70-
/*
71-
func initWithLocale(locale string) goi18n.TranslateFunc {
72-
err := loadFromAsset(locale)
73-
if err != nil {
74-
locale = DEFAULT_LOCALE
82+
83+
func InitWithLocale(locale string) {
84+
bundle := goi18n.NewBundle(language.English)
85+
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
86+
bundle.MustLoadMessageFile("plugin/i18n/resources/en_US.json")
87+
if locale != "en_US" {
88+
bundle.MustLoadMessageFile(fmt.Sprintf("plugin/i18n/resources/%s.json", locale))
7589
}
76-
return goi18n.MustTfunc(locale)
90+
loc := goi18n.NewLocalizer(bundle, locale)
91+
localizer = loc
7792
}
7893

94+
/*
7995
func loadFromAsset(locale string) (err error) {
8096
assetName := locale + ".all.json"
8197
assetKey := filepath.Join(resourcePath, assetName)

plugin/i18n/i18n_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,22 @@ var _ = Describe("I18NTests", func() {
5959

6060
It("Testing "+language, func() {
6161
coreConfig.SetLocale(language)
62-
translator := i18n.Init(coreConfig)
63-
Expect(translator("Recurring Price")).To(Equal(xlationMap[language]))
62+
i18n.InitWithLocale(language)
63+
Expect(i18n.T("Recurring Price")).To(Equal(xlationMap[language]))
6464
})
6565
It("Testing "+language+" everything", func() {
6666
// If these fails as untranslated, try running ./bin/generate-i18n-resources.sh
6767
regex, _ := regexp.Compile("{{.([[:alnum:]])*}}")
6868
coreConfig.SetLocale(language)
69-
translator := i18n.Init(coreConfig)
70-
file, err := ioutil.ReadFile("resources/" + language + ".all.json")
69+
i18n.InitWithLocale(language)
70+
file, err := ioutil.ReadFile("resources/" + language + ".json")
7171
Expect(err).NotTo(HaveOccurred())
7272
xlations := []I18nXlation{}
7373
jsonErr := json.Unmarshal([]byte(file), &xlations)
7474
Expect(jsonErr).NotTo(HaveOccurred())
7575
for i := 0; i < len(xlations); i++ {
7676
subs := regex.ReplaceAllString(xlations[i].Translation, "<no value>")
77-
Expect(translator(xlations[i].Id)).To(Equal(subs))
77+
Expect(i18n.T(xlations[i].Id)).To(Equal(subs))
7878
}
7979
})
8080
}
@@ -91,10 +91,10 @@ var _ = Describe("I18NTests", func() {
9191
envLang := strings.Replace(language, "_", "-", 1)
9292
It("LANGUAGE="+envLang, func() {
9393
os.Setenv("LANGUAGE", envLang)
94-
translator := i18n.Init(coreConfig)
94+
i18n.InitWithLocale(language)
9595
locale := i18n.DetectLocal()
9696
Expect(locale).To(Equal(language))
97-
Expect(translator("Recurring Price")).To(Equal(xlationMap[language]))
97+
Expect(i18n.T("Recurring Price")).To(Equal(xlationMap[language]))
9898
})
9999
}
100100
})

0 commit comments

Comments
 (0)