mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01: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:
parent
53ed4f3a4e
commit
42e7faec52
|
@ -225,8 +225,10 @@ func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti stri
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Party is just a group joiner of routes which have the same prefix and share same middleware(s) also.
|
// Party groups routes which may have the same prefix and share same handlers,
|
||||||
// Party could also be named as 'Join' or 'Node' or 'Group' , Party chosen because it is fun.
|
// 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 {
|
func (api *APIBuilder) Party(relativePath string, handlers ...context.Handler) Party {
|
||||||
parentPath := api.relativePath
|
parentPath := api.relativePath
|
||||||
dot := string(SubdomainPrefix[0])
|
dot := string(SubdomainPrefix[0])
|
||||||
|
|
|
@ -13,7 +13,10 @@ import (
|
||||||
//
|
//
|
||||||
// Look the "APIBuilder" for its implementation.
|
// Look the "APIBuilder" for its implementation.
|
||||||
type Party interface {
|
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
|
Party(relativePath string, middleware ...context.Handler) Party
|
||||||
// PartyFunc same as `Party`, groups routes that share a base path or/and same handlers.
|
// 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.
|
// However this function accepts a function that receives this created Party instead.
|
||||||
|
|
|
@ -22,7 +22,8 @@ func (i *i18nMiddleware) ServeHTTP(ctx context.Context) {
|
||||||
language := i.config.Default
|
language := i.config.Default
|
||||||
|
|
||||||
langKey := ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey()
|
langKey := ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey()
|
||||||
if ctx.Values().GetString(langKey) == "" {
|
language = ctx.Values().GetString(langKey)
|
||||||
|
if language == "" {
|
||||||
// try to get by url parameter
|
// try to get by url parameter
|
||||||
language = ctx.URLParam(i.config.URLParameter)
|
language = ctx.URLParam(i.config.URLParameter)
|
||||||
if language == "" {
|
if language == "" {
|
||||||
|
@ -52,8 +53,9 @@ func (i *i18nMiddleware) ServeHTTP(ctx context.Context) {
|
||||||
if language == "" {
|
if language == "" {
|
||||||
language = i.config.Default
|
language = i.config.Default
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
ctx.Values().Set(langKey, language)
|
||||||
|
}
|
||||||
locale := i18n.Locale{Lang: language}
|
locale := i18n.Locale{Lang: language}
|
||||||
|
|
||||||
// if unexpected language given, the middleware will transtlate to the default language, the language key should be
|
// 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 {
|
if indexLang := locale.Index(); indexLang == -1 {
|
||||||
locale.Lang = i.config.Default
|
locale.Lang = i.config.Default
|
||||||
}
|
}
|
||||||
ctx.Values().Set(langKey, locale.Lang)
|
|
||||||
translateFuncKey := ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey()
|
translateFuncKey := ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey()
|
||||||
ctx.Values().Set(translateFuncKey, locale.Tr)
|
ctx.Values().Set(translateFuncKey, locale.Tr)
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
|
@ -82,16 +84,23 @@ func New(c Config) context.Handler {
|
||||||
i := &i18nMiddleware{config: c}
|
i := &i18nMiddleware{config: c}
|
||||||
firstlanguage := ""
|
firstlanguage := ""
|
||||||
//load the files
|
//load the files
|
||||||
for k, v := range c.Languages {
|
for k, langFileOrFiles := range c.Languages {
|
||||||
if !strings.HasSuffix(v, ".ini") {
|
// remove all spaces.
|
||||||
v += ".ini"
|
langFileOrFiles = strings.Replace(langFileOrFiles, " ", "", -1)
|
||||||
}
|
// note: if only one, then the first element is the "v".
|
||||||
err := i18n.SetMessage(k, v)
|
languages := strings.Split(langFileOrFiles, ",")
|
||||||
if err != nil && err != i18n.ErrLangAlreadyExist {
|
|
||||||
panic("iris i18n Middleware: Failed to set locale file" + k + " Error:" + err.Error())
|
for _, v := range languages { // loop each of the files separated by comma, if any.
|
||||||
}
|
if !strings.HasSuffix(v, ".ini") {
|
||||||
if firstlanguage == "" {
|
v += ".ini"
|
||||||
firstlanguage = k
|
}
|
||||||
|
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
|
// if not default language setted then set to the first of the i.config.Languages
|
||||||
|
|
Loading…
Reference in New Issue
Block a user