Skip to content

Commit bd2e304

Browse files
getting substitutions to work
1 parent 0982988 commit bd2e304

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

plugin/i18n/i18n.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"golang.org/x/text/language"
88
"encoding/json"
99
"fmt"
10-
// "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config"
10+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config"
1111
"github.com/Xuanwo/go-locale"
1212
goi18n "github.com/nicksnyder/go-i18n/v2/i18n"
1313
// "github.ibm.com/SoftLayer/softlayer-cli/plugin/resources"
@@ -58,56 +58,58 @@ func SetResourcePath(path string) {
5858

5959
// var T goi18n.TranslateFunc = Init(core_config.NewCoreConfig(func(error) {}))
6060

61+
// Translates a string, with any substitutions needed
62+
// text: string to be translated
63+
// subs: A single map[string]interface{}
6164
func T(text string, subs ...interface{}) string {
6265

6366
// fmt.Printf("SUBS: %v\n", subs)
6467
message := &goi18n.Message{ID: text, Other: text}
6568
config := &goi18n.LocalizeConfig{DefaultMessage: message}
69+
// Need to use `subs ...interface{}` so that we can have 0 or 1 subs.
70+
// Should never have 2
71+
if subs != nil && len(subs) == 1 {
72+
config.TemplateData = subs[0]
73+
}
74+
6675
l_string, err := localizer.Localize(config)
6776
if err != nil {
6877
fmt.Printf("ERROR i18n\n%v\n", err.Error())
69-
return err.Error()
78+
// return err.Error()
7079
}
7180
return l_string
7281
}
7382

7483

7584

76-
85+
// Sets the localizer, reads local from config/system
7786
func Init() *goi18n.Localizer {
78-
bundle := goi18n.NewBundle(language.English)
79-
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
80-
bundle.LoadMessageFileFS(LocaleFS, "resources/en_US.json")
81-
bundle.LoadMessageFileFS(LocaleFS, "resources/ja_JP.json")
82-
loc := goi18n.NewLocalizer(bundle, "ja_JP")
83-
return loc
87+
88+
coreConfig := core_config.NewCoreConfig(func(error) {})
89+
userLocale := coreConfig.Locale()
90+
locale := supportedLocale(userLocale)
91+
return InitWithLocale(locale)
8492
}
8593

86-
87-
func InitWithLocale(locale string) {
94+
// Sets the localizer with the proper language
95+
func InitWithLocale(locale string) *goi18n.Localizer {
96+
8897
bundle := goi18n.NewBundle(language.English)
8998
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
9099
bundle.LoadMessageFileFS(LocaleFS, "resources/en_US.json")
91100
if locale != "en_US" {
92101
bundle.LoadMessageFileFS(LocaleFS, fmt.Sprintf("resources/%s.json", locale))
93102
}
94103
loc := goi18n.NewLocalizer(bundle, locale)
95-
localizer = loc
104+
return loc
96105
}
97106

98-
/*
99-
func loadFromAsset(locale string) (err error) {
100-
assetName := locale + ".all.json"
101-
assetKey := filepath.Join(resourcePath, assetName)
102-
bytes, err := resources.Asset(assetKey)
103-
if err != nil {
104-
return
105-
}
106-
err = goi18n.ParseTranslationFileBytes(assetName, bytes)
107-
return
107+
// Used for testing and changing the language output dynamically
108+
func SetLocalizer(new_localizer *goi18n.Localizer) {
109+
localizer = new_localizer
108110
}
109111

110-
*/
112+
111113
// Tries to determine the system locale, when local isn't set, default to en_US
112114
func DetectLocal() string {
113115
tag, err := locale.Detect()

plugin/i18n/i18n_test.go

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

6060
It("Testing "+language, func() {
6161
coreConfig.SetLocale(language)
62-
i18n.InitWithLocale(language)
62+
i18n.SetLocalizer(i18n.InitWithLocale(language))
6363
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-
i18n.InitWithLocale(language)
69+
i18n.SetLocalizer(i18n.InitWithLocale(language))
7070
file, err := ioutil.ReadFile("resources/" + language + ".json")
7171
Expect(err).NotTo(HaveOccurred())
7272
xlations := []I18nXlation{}
@@ -91,7 +91,7 @@ var _ = Describe("I18NTests", func() {
9191
envLang := strings.Replace(language, "_", "-", 1)
9292
It("LANGUAGE="+envLang, func() {
9393
os.Setenv("LANGUAGE", envLang)
94-
i18n.InitWithLocale(language)
94+
i18n.SetLocalizer(i18n.InitWithLocale(language))
9595
locale := i18n.DetectLocal()
9696
Expect(locale).To(Equal(language))
9797
Expect(i18n.T("Recurring Price")).To(Equal(xlationMap[language]))

0 commit comments

Comments
 (0)