less verbose language selector

pull/9715/head
Vasilyev, Viacheslav 2020-11-16 12:43:56 +02:00
parent 9b964853a9
commit 1b632656b3
3 changed files with 14 additions and 26 deletions

View File

@ -29,9 +29,8 @@ import (
func TestOutT(t *testing.T) {
// Set the system locale to Arabic and define a dummy translation file.
if err := translate.SetPreferredLanguage("ar"); err != nil {
t.Fatalf("SetPreferredLanguage: %v", err)
}
translate.SetPreferredLanguage("ar")
translate.Translations = map[string]interface{}{
"Installing Kubernetes version {{.version}} ...": "... {{.version}} تثبيت Kubernetes الإصدار",
}

View File

@ -62,18 +62,10 @@ func T(s string) string {
func DetermineLocale() {
locale, err := jibber_jabber.DetectIETF()
if err != nil {
klog.Infof("Getting system locale failed: %v", err)
klog.V(1).Infof("Getting system locale failed: %v", err)
locale = ""
}
err = SetPreferredLanguage(locale)
if err != nil {
klog.Infof("Setting locale failed: %v", err)
preferredLanguage = defaultLanguage
}
if preferredLanguage == defaultLanguage {
return
}
SetPreferredLanguage(locale)
// Load translations for preferred language into memory.
p := preferredLanguage.String()
@ -86,18 +78,18 @@ func DetermineLocale() {
translationFile := path.Join("translations", fmt.Sprintf("%s.json", p))
t, err = Asset(translationFile)
if err != nil {
klog.Infof("Failed to load translation file for %s: %v", p, err)
klog.V(1).Infof("Failed to load translation file for %s: %v", p, err)
return
}
} else {
klog.Infof("Failed to load translation file for %s: %v", preferredLanguage.String(), err)
klog.V(1).Infof("Failed to load translation file for %s: %v", preferredLanguage.String(), err)
return
}
}
err = json.Unmarshal(t, &Translations)
if err != nil {
klog.Infof("Failed to populate translation map: %v", err)
klog.V(1).Infof("Failed to populate translation map: %v", err)
}
}
@ -110,21 +102,22 @@ func setPreferredLanguageTag(l language.Tag) {
}
// SetPreferredLanguage configures which language future messages should use, based on a LANG string.
func SetPreferredLanguage(s string) error {
func SetPreferredLanguage(s string) {
// "C" is commonly used to denote a neutral POSIX locale. See http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html#tag_07_02
if s == "" || s == "C" {
setPreferredLanguageTag(defaultLanguage)
return nil
return
}
// Handles "de_DE" or "de_DE.utf8"
// We don't process encodings, since Rob Pike invented utf8 and we're mostly stuck with it.
// Fallback to the default language if not detected
parts := strings.Split(s, ".")
l, err := language.Parse(parts[0])
if err != nil {
return err
setPreferredLanguageTag(defaultLanguage)
return
}
setPreferredLanguageTag(l)
return nil
}
// GetPreferredLanguage returns the preferred language tag.

View File

@ -31,16 +31,12 @@ func TestSetPreferredLanguage(t *testing.T) {
{"C", language.AmericanEnglish},
{"zh", language.Chinese},
{"fr_FR.utf8", language.French},
{"zzyy.utf8", language.AmericanEnglish},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
// Set something so that we can assert change.
if err := SetPreferredLanguage("is"); err != nil {
t.Errorf("unexpected error: %q", err)
}
if err := SetPreferredLanguage(tc.input); err != nil {
t.Errorf("unexpected error: %q", err)
}
SetPreferredLanguage(tc.input)
want, _ := tc.want.Base()
got, _ := GetPreferredLanguage().Base()