diff --git a/core/router/api_builder.go b/core/router/api_builder.go index 5858df2d..475b68d7 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -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]) diff --git a/core/router/party.go b/core/router/party.go index 86ea1d1a..16834468 100644 --- a/core/router/party.go +++ b/core/router/party.go @@ -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. diff --git a/middleware/i18n/i18n.go b/middleware/i18n/i18n.go index 6e5a9dd7..4fef4470 100644 --- a/middleware/i18n/i18n.go +++ b/middleware/i18n/i18n.go @@ -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