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

@ -225,8 +225,10 @@ func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti stri
return
}
// Party is just a group joiner of routes which have the same prefix and share same middleware(s) also.
// Party could also be named as 'Join' or 'Node' or 'Group' , Party chosen because it is fun.
// Party groups routes which may have the same prefix and share same handlers,
// returns that new rich subrouter.
//
// You can even declare a subdomain with relativePath as "mysub." or see `Subdomain`.
func (api *APIBuilder) Party(relativePath string, handlers ...context.Handler) Party {
parentPath := api.relativePath
dot := string(SubdomainPrefix[0])

View File

@ -13,7 +13,10 @@ import (
//
// Look the "APIBuilder" for its implementation.
type Party interface {
// Party creates and returns a new child Party with the following features.
// Party groups routes which may have the same prefix and share same handlers,
// returns that new rich subrouter.
//
// You can even declare a subdomain with relativePath as "mysub." or see `Subdomain`.
Party(relativePath string, middleware ...context.Handler) Party
// PartyFunc same as `Party`, groups routes that share a base path or/and same handlers.
// However this function accepts a function that receives this created Party instead.

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