|
7 | 7 | "golang.org/x/text/language" |
8 | 8 | "encoding/json" |
9 | 9 | "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" |
11 | 11 | "github.com/Xuanwo/go-locale" |
12 | 12 | goi18n "github.com/nicksnyder/go-i18n/v2/i18n" |
13 | 13 | // "github.ibm.com/SoftLayer/softlayer-cli/plugin/resources" |
@@ -58,56 +58,58 @@ func SetResourcePath(path string) { |
58 | 58 |
|
59 | 59 | // var T goi18n.TranslateFunc = Init(core_config.NewCoreConfig(func(error) {})) |
60 | 60 |
|
| 61 | +// Translates a string, with any substitutions needed |
| 62 | +// text: string to be translated |
| 63 | +// subs: A single map[string]interface{} |
61 | 64 | func T(text string, subs ...interface{}) string { |
62 | 65 |
|
63 | 66 | // fmt.Printf("SUBS: %v\n", subs) |
64 | 67 | message := &goi18n.Message{ID: text, Other: text} |
65 | 68 | 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 | + |
66 | 75 | l_string, err := localizer.Localize(config) |
67 | 76 | if err != nil { |
68 | 77 | fmt.Printf("ERROR i18n\n%v\n", err.Error()) |
69 | | - return err.Error() |
| 78 | + // return err.Error() |
70 | 79 | } |
71 | 80 | return l_string |
72 | 81 | } |
73 | 82 |
|
74 | 83 |
|
75 | 84 |
|
76 | | - |
| 85 | +// Sets the localizer, reads local from config/system |
77 | 86 | 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) |
84 | 92 | } |
85 | 93 |
|
86 | | - |
87 | | -func InitWithLocale(locale string) { |
| 94 | +// Sets the localizer with the proper language |
| 95 | +func InitWithLocale(locale string) *goi18n.Localizer { |
| 96 | + |
88 | 97 | bundle := goi18n.NewBundle(language.English) |
89 | 98 | bundle.RegisterUnmarshalFunc("json", json.Unmarshal) |
90 | 99 | bundle.LoadMessageFileFS(LocaleFS, "resources/en_US.json") |
91 | 100 | if locale != "en_US" { |
92 | 101 | bundle.LoadMessageFileFS(LocaleFS, fmt.Sprintf("resources/%s.json", locale)) |
93 | 102 | } |
94 | 103 | loc := goi18n.NewLocalizer(bundle, locale) |
95 | | - localizer = loc |
| 104 | + return loc |
96 | 105 | } |
97 | 106 |
|
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 |
108 | 110 | } |
109 | 111 |
|
110 | | -*/ |
| 112 | + |
111 | 113 | // Tries to determine the system locale, when local isn't set, default to en_US |
112 | 114 | func DetectLocal() string { |
113 | 115 | tag, err := locale.Detect() |
|
0 commit comments