Skip to content

Commit

Permalink
less verbose language selector
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidik committed Nov 16, 2020
1 parent 9b96485 commit ad90883
Showing 1 changed file with 10 additions and 17 deletions.
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

0 comments on commit ad90883

Please sign in to comment.