2017-07-10 17:32:42 +02:00
package main
import (
"fmt"
2019-11-20 01:35:41 +01:00
"strings"
2017-07-10 17:32:42 +02:00
"testing"
2019-10-25 00:27:02 +02:00
"github.com/kataras/iris/v12/httptest"
2017-07-10 17:32:42 +02:00
)
func TestI18n ( t * testing . T ) {
app := newApp ( )
2019-11-22 01:57:26 +01:00
const (
expectedf = "From the language %s translated output: %s"
enUS = "hello, iris"
elGR = "γεια, iris"
zhCN = "您好, iris"
)
2017-07-10 17:32:42 +02:00
var (
2019-11-20 01:35:41 +01:00
tests = map [ string ] string {
2019-11-22 01:57:26 +01:00
"en-US" : fmt . Sprintf ( expectedf , "en-US" , enUS ) ,
"el-GR" : fmt . Sprintf ( expectedf , "el-GR" , elGR ) ,
"zh-CN" : fmt . Sprintf ( expectedf , "zh-CN" , zhCN ) ,
2019-11-20 01:35:41 +01:00
}
2017-11-22 00:08:26 +01:00
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" )
2017-07-10 17:32:42 +02:00
)
e := httptest . New ( t , app )
2019-11-20 01:35:41 +01:00
// 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 )
}
2017-11-22 00:08:26 +01:00
2019-12-13 22:06:18 +01:00
e . GET ( "/other" ) . WithQueryString ( "lang=el-GR" ) . Expect ( ) . Status ( httptest . StatusOK ) .
2017-11-22 00:08:26 +01:00
Body ( ) . Equal ( elgrMulti )
2019-12-13 22:06:18 +01:00
e . GET ( "/other" ) . WithQueryString ( "lang=en-US" ) . Expect ( ) . Status ( httptest . StatusOK ) .
2017-11-22 00:08:26 +01:00
Body ( ) . Equal ( enusMulti )
2019-11-20 01:35:41 +01:00
// test path prefix (i18n router wrapper).
2019-12-13 22:06:18 +01:00
e . GET ( "/el-gr/other" ) . Expect ( ) . Status ( httptest . StatusOK ) .
2019-11-20 01:35:41 +01:00
Body ( ) . Equal ( elgrMulti )
2019-12-13 22:06:18 +01:00
e . GET ( "/en/other" ) . Expect ( ) . Status ( httptest . StatusOK ) .
2019-11-20 01:35:41 +01:00
Body ( ) . Equal ( enusMulti )
2019-11-22 01:57:26 +01:00
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 )
2020-08-18 07:05:51 +02:00
e . GET ( "/not-matched" ) . WithQuery ( "lang" , "en-gb" ) . Expect ( ) . Status ( httptest . StatusOK ) . Body ( ) . Equal ( "user language input: en-gb: matched as: en-US: not found key: not_found_key: args: [some values 42]" )
2017-07-10 17:32:42 +02:00
}