1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 20:41:57 +00:00

update the vendor of the new iris-contrib/i18n to support multi locale files as requested at: https://github.com/kataras/iris/issues/815

Former-commit-id: 06d7c704caf97eae1fc81228425fddb588f2f68c
This commit is contained in:
kataras
2017-11-22 01:01:45 +02:00
parent 53ed4f3a4e
commit 42e7faec52
3 changed files with 30 additions and 16 deletions

View File

@@ -22,7 +22,8 @@ func (i *i18nMiddleware) ServeHTTP(ctx context.Context) {
language := i.config.Default
langKey := ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey()
if ctx.Values().GetString(langKey) == "" {
language = ctx.Values().GetString(langKey)
if language == "" {
// try to get by url parameter
language = ctx.URLParam(i.config.URLParameter)
if language == "" {
@@ -52,8 +53,9 @@ func (i *i18nMiddleware) ServeHTTP(ctx context.Context) {
if language == "" {
language = i.config.Default
}
}
ctx.Values().Set(langKey, language)
}
locale := i18n.Locale{Lang: language}
// if unexpected language given, the middleware will transtlate to the default language, the language key should be
@@ -61,7 +63,7 @@ func (i *i18nMiddleware) ServeHTTP(ctx context.Context) {
if indexLang := locale.Index(); indexLang == -1 {
locale.Lang = i.config.Default
}
ctx.Values().Set(langKey, locale.Lang)
translateFuncKey := ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey()
ctx.Values().Set(translateFuncKey, locale.Tr)
ctx.Next()
@@ -82,16 +84,23 @@ func New(c Config) context.Handler {
i := &i18nMiddleware{config: c}
firstlanguage := ""
//load the files
for k, v := range c.Languages {
if !strings.HasSuffix(v, ".ini") {
v += ".ini"
}
err := i18n.SetMessage(k, v)
if err != nil && err != i18n.ErrLangAlreadyExist {
panic("iris i18n Middleware: Failed to set locale file" + k + " Error:" + err.Error())
}
if firstlanguage == "" {
firstlanguage = k
for k, langFileOrFiles := range c.Languages {
// remove all spaces.
langFileOrFiles = strings.Replace(langFileOrFiles, " ", "", -1)
// note: if only one, then the first element is the "v".
languages := strings.Split(langFileOrFiles, ",")
for _, v := range languages { // loop each of the files separated by comma, if any.
if !strings.HasSuffix(v, ".ini") {
v += ".ini"
}
err := i18n.SetMessage(k, v)
if err != nil && err != i18n.ErrLangAlreadyExist {
panic("Failed to set locale file'" + k + "' Error:" + err.Error())
}
if firstlanguage == "" {
firstlanguage = k
}
}
}
// if not default language setted then set to the first of the i.config.Languages