Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

less verbose language selector #9715

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pkg/minikube/out/out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 الإصدار",
}
Expand Down
27 changes: 10 additions & 17 deletions pkg/minikube/translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

}
Expand All @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions pkg/minikube/translate/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down