diff options
author | =Corey Hulen <corey@hulen.com> | 2016-01-22 16:31:58 -0600 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2016-01-22 16:31:58 -0600 |
commit | c2d98c2c1f4860c11aedf43aff5e360256a89835 (patch) | |
tree | 4304f7584025477d74e5e70677c4f5a70c26bb58 /utils | |
parent | 6e2c1b7fd5248c6a4a91edcd59fa124c8d3c744a (diff) | |
parent | d352c5b64dddfb8e46b18edbd7352c41495078a1 (diff) | |
download | chat-c2d98c2c1f4860c11aedf43aff5e360256a89835.tar.gz chat-c2d98c2c1f4860c11aedf43aff5e360256a89835.tar.bz2 chat-c2d98c2c1f4860c11aedf43aff5e360256a89835.zip |
merging
Diffstat (limited to 'utils')
-rw-r--r-- | utils/config.go | 3 | ||||
-rw-r--r-- | utils/i18n.go | 79 |
2 files changed, 82 insertions, 0 deletions
diff --git a/utils/config.go b/utils/config.go index 024c28d16..1180ed6d4 100644 --- a/utils/config.go +++ b/utils/config.go @@ -225,5 +225,8 @@ func getClientConfig(c *model.Config) map[string]string { props["EnableLdap"] = strconv.FormatBool(*c.LdapSettings.Enable) + props["WebsocketPort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketPort) + props["WebsocketSecurePort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketSecurePort) + return props } diff --git a/utils/i18n.go b/utils/i18n.go new file mode 100644 index 000000000..05154bd92 --- /dev/null +++ b/utils/i18n.go @@ -0,0 +1,79 @@ +package utils + +import ( + "io/ioutil" + "net/http" + "path/filepath" + "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) + +func InitTranslations() { + i18nDirectory := FindDir("i18n") + files, _ := ioutil.ReadDir(i18nDirectory) + for _, f := range files { + if filepath.Ext(f.Name()) == ".json" { + filename := f.Name() + locales[strings.Split(filename, ".")[0]] = i18nDirectory + filename + i18n.MustLoadTranslationFile(i18nDirectory + filename) + } + } + + T = GetTranslationsBySystemLocale() +} + +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 + } + } + + if locales[locale] == "" { + panic("Failed to load system translations for '" + model.DEFAULT_LOCALE + "'") + } + + translations, _ := i18n.Tfunc(locale) + if translations == nil { + panic("Failed to load system translations") + } + + l4g.Info(translations("utils.i18n.loaded"), locale, locales[locale]) + return translations +} + +func GetUserTranslations(locale string) i18n.TranslateFunc { + if _, ok := locales[locale]; !ok { + locale = model.DEFAULT_LOCALE + } + + translations, _ := i18n.Tfunc(locale) + return translations +} + +func SetTranslations(locale string) i18n.TranslateFunc { + translations, _ := i18n.Tfunc(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) + return translations, headerLocale + } + + translations, _ := i18n.Tfunc(model.DEFAULT_LOCALE) + return translations, model.DEFAULT_LOCALE +} |