summaryrefslogtreecommitdiffstats
path: root/utils/i18n.go
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2016-01-28 09:36:32 -0500
committer=Corey Hulen <corey@hulen.com>2016-01-28 09:36:32 -0500
commitc7d9754f4b8ce8eefb0c9608cf6c2cec2a7d44fe (patch)
tree2ef65e0802a2eac6ef0ac2bd292a1850e334c83c /utils/i18n.go
parentfa6daad64d474e99f3a2964784585a748350c25a (diff)
downloadchat-c7d9754f4b8ce8eefb0c9608cf6c2cec2a7d44fe.tar.gz
chat-c7d9754f4b8ce8eefb0c9608cf6c2cec2a7d44fe.tar.bz2
chat-c7d9754f4b8ce8eefb0c9608cf6c2cec2a7d44fe.zip
Adding english fallback to server
Diffstat (limited to 'utils/i18n.go')
-rw-r--r--utils/i18n.go20
1 files changed, 15 insertions, 5 deletions
diff --git a/utils/i18n.go b/utils/i18n.go
index 05154bd92..d545d18b8 100644
--- a/utils/i18n.go
+++ b/utils/i18n.go
@@ -44,7 +44,7 @@ func GetTranslationsBySystemLocale() i18n.TranslateFunc {
panic("Failed to load system translations for '" + model.DEFAULT_LOCALE + "'")
}
- translations, _ := i18n.Tfunc(locale)
+ translations, _ := TfuncWithFallback(locale)
if translations == nil {
panic("Failed to load system translations")
}
@@ -58,22 +58,32 @@ func GetUserTranslations(locale string) i18n.TranslateFunc {
locale = model.DEFAULT_LOCALE
}
- translations, _ := i18n.Tfunc(locale)
+ translations, _ := TfuncWithFallback(locale)
return translations
}
func SetTranslations(locale string) i18n.TranslateFunc {
- translations, _ := i18n.Tfunc(locale)
+ translations, _ := TfuncWithFallback(locale)
return translations
}
func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) {
headerLocale := strings.Split(strings.Split(r.Header.Get("Accept-Language"), ",")[0], "-")[0]
if locales[headerLocale] != "" {
- translations, _ := i18n.Tfunc(headerLocale)
+ translations, _ := TfuncWithFallback(headerLocale)
return translations, headerLocale
}
- translations, _ := i18n.Tfunc(model.DEFAULT_LOCALE)
+ translations, _ := TfuncWithFallback(model.DEFAULT_LOCALE)
return translations, model.DEFAULT_LOCALE
}
+
+func TfuncWithFallback(pref string, prefs ...string) TranslateFunc {
+ t := i18n.MustTfunc(pref, prefs...)
+ return func(translationID string, args ...interface{}) string {
+ if translated := t(translationID, args...); translated != translationID {
+ return translated
+ }
+ return i18n.MustTfunc("en", args...)
+ }
+}