From 1b632656b3246b5a8ffa966fda166ac948ae7038 Mon Sep 17 00:00:00 2001 From: "Vasilyev, Viacheslav" Date: Mon, 16 Nov 2020 12:43:56 +0200 Subject: [PATCH] less verbose language selector --- pkg/minikube/out/out_test.go | 5 ++--- pkg/minikube/translate/translate.go | 27 +++++++++--------------- pkg/minikube/translate/translate_test.go | 8 ++----- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/pkg/minikube/out/out_test.go b/pkg/minikube/out/out_test.go index ee611e44ea61..9094be289c72 100644 --- a/pkg/minikube/out/out_test.go +++ b/pkg/minikube/out/out_test.go @@ -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 الإصدار", } diff --git a/pkg/minikube/translate/translate.go b/pkg/minikube/translate/translate.go index 3a69fcaf65a9..892450e60428 100644 --- a/pkg/minikube/translate/translate.go +++ b/pkg/minikube/translate/translate.go @@ -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. diff --git a/pkg/minikube/translate/translate_test.go b/pkg/minikube/translate/translate_test.go index 1482544eb265..d04579109375 100644 --- a/pkg/minikube/translate/translate_test.go +++ b/pkg/minikube/translate/translate_test.go @@ -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()