diff options
author | enahum <nahumhbl@gmail.com> | 2016-05-27 09:25:37 -0300 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2016-05-27 08:25:37 -0400 |
commit | a2c183f401bdf8e9abddd35d4561d42034325046 (patch) | |
tree | 66565a9e21ad6b47b4dca18113ad9be48bd98492 /utils | |
parent | 15e427e806716b372f7fbef12d922f847ccc71b5 (diff) | |
download | chat-a2c183f401bdf8e9abddd35d4561d42034325046.tar.gz chat-a2c183f401bdf8e9abddd35d4561d42034325046.tar.bz2 chat-a2c183f401bdf8e9abddd35d4561d42034325046.zip |
PLT-1800 Load server side locale from the config.json (#3092)
* 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
Diffstat (limited to 'utils')
-rw-r--r-- | utils/config.go | 3 | ||||
-rw-r--r-- | utils/config_test.go | 2 | ||||
-rw-r--r-- | utils/i18n.go | 32 |
3 files changed, 23 insertions, 14 deletions
diff --git a/utils/config.go b/utils/config.go index 313b4e29c..a3969fc40 100644 --- a/utils/config.go +++ b/utils/config.go @@ -246,7 +246,8 @@ func getClientConfig(c *model.Config) map[string]string { props["WebsocketPort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketPort) props["WebsocketSecurePort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketSecurePort) - props["AllowCorsFrom"] = *c.ServiceSettings.AllowCorsFrom + props["DefaultClientLocale"] = *c.LocalizationSettings.DefaultClientLocale + props["AvailableLocales"] = *c.LocalizationSettings.AvailableLocales if IsLicensed { if *License.Features.CustomBrand { diff --git a/utils/config_test.go b/utils/config_test.go index 6f36b30c3..96ef49696 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -9,5 +9,5 @@ import ( func TestConfig(t *testing.T) { LoadConfig("config.json") - InitTranslations() + InitTranslations(Cfg.LocalizationSettings) } diff --git a/utils/i18n.go b/utils/i18n.go index 2503cd500..300f5ca7c 100644 --- a/utils/i18n.go +++ b/utils/i18n.go @@ -7,15 +7,17 @@ import ( "strings" l4g "github.com/alecthomas/log4go" - "github.com/cloudfoundry/jibber_jabber" + //"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 +36,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 +70,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 +97,7 @@ func TfuncWithFallback(pref string) i18n.TranslateFunc { return translated } - t, _ := i18n.Tfunc("en") + t, _ := i18n.Tfunc(model.DEFAULT_LOCALE) return t(translationID, args...) } } |