From 53ed4f3a4e702522fe57dbffd004aad2129cab91 Mon Sep 17 00:00:00 2001 From: kataras Date: Wed, 22 Nov 2017 00:57:29 +0200 Subject: [PATCH] add support for multi languages, without change the API, separated by commas as requested at: https://github.com/kataras/iris/issues/815 Former-commit-id: ae3dcabb543c017f0661f2b2a1af8250d73773cf --- .../i18n/locales/locale_multi_first_el-GR.ini | 1 + .../i18n/locales/locale_multi_first_en-US.ini | 1 + .../locales/locale_multi_second_el-GR.ini | 1 + .../locales/locale_multi_second_en-US.ini | 1 + _examples/miscellaneous/i18n/main.go | 30 +++++++++++++++++-- 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 _examples/miscellaneous/i18n/locales/locale_multi_first_el-GR.ini create mode 100644 _examples/miscellaneous/i18n/locales/locale_multi_first_en-US.ini create mode 100644 _examples/miscellaneous/i18n/locales/locale_multi_second_el-GR.ini create mode 100644 _examples/miscellaneous/i18n/locales/locale_multi_second_en-US.ini diff --git a/_examples/miscellaneous/i18n/locales/locale_multi_first_el-GR.ini b/_examples/miscellaneous/i18n/locales/locale_multi_first_el-GR.ini new file mode 100644 index 00000000..cf043c86 --- /dev/null +++ b/_examples/miscellaneous/i18n/locales/locale_multi_first_el-GR.ini @@ -0,0 +1 @@ +key1 = αυτό είναι μια τιμή από το πρώτο αρχείο: locale_multi_first \ No newline at end of file diff --git a/_examples/miscellaneous/i18n/locales/locale_multi_first_en-US.ini b/_examples/miscellaneous/i18n/locales/locale_multi_first_en-US.ini new file mode 100644 index 00000000..9e264821 --- /dev/null +++ b/_examples/miscellaneous/i18n/locales/locale_multi_first_en-US.ini @@ -0,0 +1 @@ +key1 = this is a value from the first file: locale_multi_first \ No newline at end of file diff --git a/_examples/miscellaneous/i18n/locales/locale_multi_second_el-GR.ini b/_examples/miscellaneous/i18n/locales/locale_multi_second_el-GR.ini new file mode 100644 index 00000000..6569d65b --- /dev/null +++ b/_examples/miscellaneous/i18n/locales/locale_multi_second_el-GR.ini @@ -0,0 +1 @@ +key2 = αυτό είναι μια τιμή από το δεύτερο αρχείο μετάφρασης: locale_multi_second \ No newline at end of file diff --git a/_examples/miscellaneous/i18n/locales/locale_multi_second_en-US.ini b/_examples/miscellaneous/i18n/locales/locale_multi_second_en-US.ini new file mode 100644 index 00000000..97826b69 --- /dev/null +++ b/_examples/miscellaneous/i18n/locales/locale_multi_second_en-US.ini @@ -0,0 +1 @@ +key2 = this is a value from the second file: locale_multi_second \ No newline at end of file diff --git a/_examples/miscellaneous/i18n/main.go b/_examples/miscellaneous/i18n/main.go index 53e653d2..ab7b53cc 100644 --- a/_examples/miscellaneous/i18n/main.go +++ b/_examples/miscellaneous/i18n/main.go @@ -8,13 +8,14 @@ import ( func newApp() *iris.Application { app := iris.New() - app.Use(i18n.New(i18n.Config{ + globalLocale := i18n.New(i18n.Config{ Default: "en-US", URLParameter: "lang", Languages: map[string]string{ "en-US": "./locales/locale_en-US.ini", "el-GR": "./locales/locale_el-GR.ini", - "zh-CN": "./locales/locale_zh-CN.ini"}})) + "zh-CN": "./locales/locale_zh-CN.ini"}}) + app.Use(globalLocale) app.Get("/", func(ctx iris.Context) { @@ -41,6 +42,23 @@ func newApp() *iris.Application { ctx.Writef("From the language %s translated output: %s", language, hi) }) + multiLocale := i18n.New(i18n.Config{ + Default: "en-US", + URLParameter: "lang", + Languages: map[string]string{ + "en-US": "./locales/locale_multi_first_en-US.ini, ./locales/locale_multi_second_en-US.ini", + "el-GR": "./locales/locale_multi_first_el-GR.ini, ./locales/locale_multi_second_el-GR.ini"}}) + + app.Get("/multi", multiLocale, func(ctx iris.Context) { + language := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey()) + + fromFirstFileValue := i18n.Translate(ctx, "key1") + fromSecondFileValue := i18n.Translate(ctx, "key2") + ctx.Writef("From the language: %s, translated output:\n%s=%s\n%s=%s", + language, "key1", fromFirstFileValue, + "key2", fromSecondFileValue) + }) + return app } @@ -48,7 +66,13 @@ func main() { app := newApp() // go to http://localhost:8080/?lang=el-GR - // or http://localhost:8080 + // or http://localhost:8080 (default is en-US) // or http://localhost:8080/?lang=zh-CN + // + // go to http://localhost:8080/multi?lang=el-GR + // or http://localhost:8080/multi (default is en-US) + // or http://localhost:8080/multi?lang=en-US + // + // or use cookies to set the language. app.Run(iris.Addr(":8080")) }