summaryrefslogtreecommitdiffstats
path: root/utils/i18n.go
diff options
context:
space:
mode:
authorenahum <nahumhbl@gmail.com>2016-06-01 09:56:13 -0300
committerChristopher Speller <crspeller@gmail.com>2016-06-01 08:56:13 -0400
commitb00a60ab71b2bd4640c8608d71805ba9caae97d9 (patch)
tree02137b19bc56e5041530e710955f039682da43c7 /utils/i18n.go
parent7be2a05cf58c22d1edfab12a2b55569c5e48ab2f (diff)
downloadchat-b00a60ab71b2bd4640c8608d71805ba9caae97d9.tar.gz
chat-b00a60ab71b2bd4640c8608d71805ba9caae97d9.tar.bz2
chat-b00a60ab71b2bd4640c8608d71805ba9caae97d9.zip
PLT-1800 Load server side locale from the config.json (#3135)
* PLT-1800 Load server side locale from the config.json * Add support for locales with country specifics * Fix localization on served locale file as plain/text * Remove github.com/cloudfoundry/jibber_jabber as vendor dependency
Diffstat (limited to 'utils/i18n.go')
-rw-r--r--utils/i18n.go31
1 files changed, 19 insertions, 12 deletions
diff --git a/utils/i18n.go b/utils/i18n.go
index 2503cd500..b3e10a831 100644
--- a/utils/i18n.go
+++ b/utils/i18n.go
@@ -7,15 +7,16 @@ import (
"strings"
l4g "github.com/alecthomas/log4go"
- "github.com/cloudfoundry/jibber_jabber"
"github.com/mattermost/platform/model"
"github.com/nicksnyder/go-i18n/i18n"
)
var T i18n.TranslateFunc
var locales map[string]string = make(map[string]string)
+var settings model.LocalizationSettings
-func InitTranslations() {
+func InitTranslations(localizationSettings model.LocalizationSettings) {
+ settings = localizationSettings
InitTranslationsWithDir("i18n")
}
@@ -34,14 +35,10 @@ func InitTranslationsWithDir(dir string) {
}
func GetTranslationsBySystemLocale() i18n.TranslateFunc {
- locale := model.DEFAULT_LOCALE
- if userLanguage, err := jibber_jabber.DetectLanguage(); err == nil {
- if _, ok := locales[userLanguage]; ok {
- locale = userLanguage
- } else {
- l4g.Error("Failed to load system translations for '%v' attempting to fall back to '%v'", locale, model.DEFAULT_LOCALE)
- locale = model.DEFAULT_LOCALE
- }
+ locale := *settings.DefaultServerLocale
+ if _, ok := locales[locale]; !ok {
+ l4g.Error("Failed to load system translations for '%v' attempting to fall back to '%v'", locale, model.DEFAULT_LOCALE)
+ locale = model.DEFAULT_LOCALE
}
if locales[locale] == "" {
@@ -72,10 +69,20 @@ func SetTranslations(locale string) i18n.TranslateFunc {
}
func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) {
+ // This is for checking against locales like pt_BR or zn_CN
+ headerLocaleFull := strings.Split(r.Header.Get("Accept-Language"), ",")[0]
+ // This is for checking agains locales like en, es
headerLocale := strings.Split(strings.Split(r.Header.Get("Accept-Language"), ",")[0], "-")[0]
- if locales[headerLocale] != "" {
+ defaultLocale := *settings.DefaultClientLocale
+ if locales[headerLocaleFull] != "" {
+ translations := TfuncWithFallback(headerLocaleFull)
+ return translations, headerLocaleFull
+ } else if locales[headerLocale] != "" {
translations := TfuncWithFallback(headerLocale)
return translations, headerLocale
+ } else if locales[defaultLocale] != "" {
+ translations := TfuncWithFallback(defaultLocale)
+ return translations, headerLocale
}
translations := TfuncWithFallback(model.DEFAULT_LOCALE)
@@ -89,7 +96,7 @@ func TfuncWithFallback(pref string) i18n.TranslateFunc {
return translated
}
- t, _ := i18n.Tfunc("en")
+ t, _ := i18n.Tfunc(model.DEFAULT_LOCALE)
return t(translationID, args...)
}
}