1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-24 05:17:03 +00:00

fixes, i18n, sitemap generator and new examples

Former-commit-id: 54801dc705ee0fa66232f65063f8a68c9cc31921
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-12-13 23:06:18 +02:00
parent c66f7a6d21
commit 04477c3e41
58 changed files with 1474 additions and 1311 deletions

View File

@@ -1,26 +0,0 @@
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 mydomain.com
127.0.0.1 en.mydomain.com
127.0.0.1 el.mydomain.com
127.0.0.1 el-gr.mydomain.com
127.0.0.1 zh.mydomain.com

View File

@@ -1 +0,0 @@
hi = γεια, %s

View File

@@ -1 +0,0 @@
hi = hello, %s

View File

@@ -1 +0,0 @@
key1 = αυτό είναι μια τιμή από το πρώτο αρχείο: locale_multi_first

View File

@@ -1 +0,0 @@
key1 = this is a value from the first file: locale_multi_first

View File

@@ -1 +0,0 @@
key2 = αυτό είναι μια τιμή από το δεύτερο αρχείο μετάφρασης: locale_multi_second

View File

@@ -1 +0,0 @@
key2 = this is a value from the second file: locale_multi_second

View File

@@ -1 +0,0 @@
hi = 您好,%s

View File

@@ -1,125 +0,0 @@
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/i18n"
)
var i18nConfig = i18n.Config{
Default: "en-US",
Languages: map[string]string{
"en-US": "./locales/locale_en-US.ini", // maps to en-US, en-us and en.
"el-GR": "./locales/locale_el-GR.ini", // maps to el-GR, el-gr and el.
"zh-CN": "./locales/locale_zh-CN.ini", // maps to zh-CN, zh-cn and zh.
},
// Optionals.
// LanguagesMap: i18n.Map{
// "en": "en-US", // now en maps to en-US
// "el": "el-GR",
// "zh": "zh-CN",
// } or a custom i18n.MapFunc, defaults to accept all lowercase and [en] as [en-US] and e.t.c.
URLParameter: "lang",
Subdomain: true,
// Cookie: "lang",
// SetCookie: false,
// Indentifier: func(ctx iris.Context) string { return "zh-CN" },
}
func newApp() *iris.Application {
app := iris.New()
i18nMiddleware := i18n.NewI18n(i18nConfig)
app.Use(i18nMiddleware.Handler())
// See https://github.com/kataras/iris/issues/1369
// if you want to enable this (SEO) feature (OPTIONAL).
app.WrapRouter(i18nMiddleware.Wrapper())
app.Get("/", func(ctx iris.Context) {
// Ir tries to find the language by:
// ctx.Values().GetString("language")
// if that was empty then
// it tries to find from the URLParameter set on the configuration
// if not found then
// it tries to find the language by the "language" cookie
// if didn't found then it it set to the Default set on the configuration
// hi is the key/word, 'iris' is the %s on the .ini file
// the second parameter is optional
hi := ctx.Translate("hi", "iris")
// GetTranslateLanguageContextKey() == "language"
language := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey())
// return is form of 'en-US'
// The first succeed language found saved at the cookie with name ("language"),
// you can change that by changing the value of the: iris.TranslateLanguageContextKey
ctx.Writef("From the language %s translated output: %s", language, hi)
})
app.Get("/some-path", func(ctx iris.Context) {
ctx.Writef("%s", ctx.Translate("hi", "iris"))
})
app.Get("/sitemap.xml", func(ctx iris.Context) {
ctx.WriteString("sitemap")
})
// Note: It is highly recommended to use one and no more i18n middleware instances at a time,
// the first one was already passed by `app.Use` above.
// This middleware which registers on "/multi" route is here just for the shake of the example.
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 := ctx.Translate("key1")
fromSecondFileValue := ctx.Translate("key2")
ctx.Writef("From the language: %s, translated output:\n%s=%s\n%s=%s",
language, "key1", fromFirstFileValue,
"key2", fromSecondFileValue)
})
// using in inside your templates:
view := iris.HTML("./templates", ".html")
app.RegisterView(view)
app.Get("/templates", func(ctx iris.Context) {
ctx.View("index.html", iris.Map{
"tr": ctx.Translate, // word, arguments...
"trLang": ctx.TranslateLang, // locale, word, arguments...
})
// it will return "hello, iris"
// when {{call .tr "hi" "iris"}}
})
//
return app
}
func main() {
app := newApp()
// go to http://localhost:8080/el-gr/some-path (by path prefix)
// or http://el.mydomain.com8080/some-path (by subdomain - test locally with the hosts file)
// or http://localhost:8080/zh-CN/templates (by path prefix with uppercase)
// or http://localhost:8080/some-path?lang=el-GR (by url parameter)
// 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"))
}

View File

@@ -1,86 +0,0 @@
package main
import (
"fmt"
"strings"
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestI18n(t *testing.T) {
app := newApp()
const (
expectedf = "From the language %s translated output: %s"
enUS = "hello, iris"
elGR = "γεια, iris"
zhCN = "您好iris"
)
var (
tests = map[string]string{
"en-US": fmt.Sprintf(expectedf, "en-US", enUS),
"el-GR": fmt.Sprintf(expectedf, "el-GR", elGR),
"zh-CN": fmt.Sprintf(expectedf, "zh-CN", zhCN),
}
elgrMulti = fmt.Sprintf("From the language: %s, translated output:\n%s=%s\n%s=%s", "el-GR",
"key1",
"αυτό είναι μια τιμή από το πρώτο αρχείο: locale_multi_first",
"key2",
"αυτό είναι μια τιμή από το δεύτερο αρχείο μετάφρασης: locale_multi_second")
enusMulti = fmt.Sprintf("From the language: %s, translated output:\n%s=%s\n%s=%s", "en-US",
"key1",
"this is a value from the first file: locale_multi_first",
"key2",
"this is a value from the second file: locale_multi_second")
)
e := httptest.New(t, app)
// default should be en-US.
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal(tests["en-US"])
for lang, body := range tests {
e.GET("/").WithQueryString("lang=" + lang).Expect().Status(httptest.StatusOK).
Body().Equal(body)
// test lowercase.
e.GET("/").WithQueryString("lang=" + strings.ToLower(lang)).Expect().Status(httptest.StatusOK).
Body().Equal(body)
// test first part (e.g. en instead of en-US).
langFirstPart := strings.Split(lang, "-")[0]
e.GET("/").WithQueryString("lang=" + langFirstPart).Expect().Status(httptest.StatusOK).
Body().Equal(body)
// test accept-language header prefix (i18n wrapper).
e.GET("/"+lang).WithHeader("Accept-Language", lang).Expect().Status(httptest.StatusOK).
Body().Equal(body)
// test path prefix (i18n router wrapper).
e.GET("/" + lang).Expect().Status(httptest.StatusOK).
Body().Equal(body)
// test path prefix with first part.
e.GET("/" + langFirstPart).Expect().Status(httptest.StatusOK).
Body().Equal(body)
}
e.GET("/multi").WithQueryString("lang=el-GR").Expect().Status(httptest.StatusOK).
Body().Equal(elgrMulti)
e.GET("/multi").WithQueryString("lang=en-US").Expect().Status(httptest.StatusOK).
Body().Equal(enusMulti)
// test path prefix (i18n router wrapper).
e.GET("/el-gr/multi").Expect().Status(httptest.StatusOK).
Body().Equal(elgrMulti)
e.GET("/en/multi").Expect().Status(httptest.StatusOK).
Body().Equal(enusMulti)
e.GET("/el-GRtemplates").Expect().Status(httptest.StatusNotFound)
e.GET("/el-templates").Expect().Status(httptest.StatusNotFound)
e.GET("/el/templates").Expect().Status(httptest.StatusOK).Body().Contains(elGR).Contains(zhCN)
}

View File

@@ -1,9 +0,0 @@
<h3>Test translate current locale template function <i>[dynamic]</i> ("word", arguments...) <br/> <code>call .tr "hi" "iris"</code></h3>
{{call .tr "hi" "iris"}}
<hr/>
<h3>Test translate of any language template function <i>[static]</i> ("language", "word", arguments...) <br/> <code>call .trLang "zh-CN" "hi" "iris"</code></h3>
{{call .trLang "zh-CN" "hi" "iris"}}