1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-06 03:27:27 +00:00

Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded

Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
kataras
2017-07-10 18:32:42 +03:00
parent 2d4c2779a7
commit 9f85b74fc9
344 changed files with 4842 additions and 5174 deletions

View File

@@ -1,4 +1,4 @@
// Package i18n provides internalization and localization via middleware. See _examples/intermediate/i18n
// Package i18n provides internalization and localization via middleware. See _examples/miscellaneous/i18n
package i18n
import (
@@ -9,6 +9,7 @@ import (
"github.com/kataras/iris/context"
)
// test file: ../../_examples/miscellaneous/i18n/main_test.go
type i18nMiddleware struct {
config Config
}
@@ -23,7 +24,6 @@ func (i *i18nMiddleware) ServeHTTP(ctx context.Context) {
if ctx.Values().GetString(langKey) == "" {
// try to get by url parameter
language = ctx.URLParam(i.config.URLParameter)
if language == "" {
// then try to take the lang field from the cookie
language = ctx.GetCookie(langKey)
@@ -44,9 +44,16 @@ 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
// also this language instead of the user-given
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()
@@ -73,13 +80,13 @@ func New(c Config) context.Handler {
}
err := i18n.SetMessage(k, v)
if err != nil && err != i18n.ErrLangAlreadyExist {
panic("Iris i18n Middleware: Failed to set locale file" + k + " Error:" + err.Error())
panic("iris i18n Middleware: 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.options.Languages
// if not default language setted then set to the first of the i.config.Languages
if c.Default == "" {
c.Default = firstlanguage
}
@@ -88,8 +95,8 @@ func New(c Config) context.Handler {
return i.ServeHTTP
}
// TranslatedMap returns translated map[string]interface{} from i18n structure
func TranslatedMap(sourceInterface interface{}, ctx context.Context) map[string]interface{} {
// TranslatedMap returns translated map[string]interface{} from i18n structure.
func TranslatedMap(ctx context.Context, sourceInterface interface{}) map[string]interface{} {
iType := reflect.TypeOf(sourceInterface).Elem()
result := make(map[string]interface{})