-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internationalize login, error and selectprovider page
- Loading branch information
Showing
8 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package locales | ||
|
||
import "golang.org/x/text/language" | ||
|
||
func GetLocale(acceptLangHeader string) map[string]string { | ||
langBase := getPreferredLang(acceptLangHeader) | ||
switch langBase { | ||
case language.English.String(): | ||
return locale_en | ||
case language.Chinese.String(): | ||
return locale_zh | ||
case language.Japanese.String(): | ||
return locale_ja | ||
case language.Korean.String(): | ||
return locale_ko | ||
default: | ||
return locale_en | ||
} | ||
} | ||
|
||
func getPreferredLang(acceptLangHeader string) string { | ||
matcher := language.NewMatcher(supportedLangs) | ||
userPrefs, _, err := language.ParseAcceptLanguage(acceptLangHeader) | ||
if err != nil { | ||
// if error occurs, fallback to English | ||
return language.English.String() | ||
} | ||
tag, _, _ := matcher.Match(userPrefs...) | ||
base, _ := tag.Base() | ||
return base.String() | ||
} | ||
|
||
var supportedLangs = []language.Tag{ | ||
language.English, // en - first language is fallback | ||
language.Chinese, // zh | ||
language.Japanese, // ja | ||
language.Korean, // ko | ||
} | ||
|
||
var locale_en = map[string]string{ | ||
"LogInToYourAccount": "Log in to your account", | ||
"Username": "Username", | ||
"Password": "Password", | ||
"LogIn": "Log in", | ||
"WelcomeTo": "Welcome to", | ||
"LogInWith": "Log in with", | ||
"Error": "Error", | ||
} | ||
|
||
var locale_zh = map[string]string{ | ||
"LogInToYourAccount": "登录到您的帐户", | ||
"Username": "用户名", | ||
"Password": "密码", | ||
"LogIn": "登录", | ||
"WelcomeTo": "欢迎使用", | ||
"LogInWith": "登录使用", | ||
"Error": "错误", | ||
} | ||
|
||
var locale_ja = map[string]string{ | ||
"LogInToYourAccount": "アカウントにログイン", | ||
"Username": "ユーザー名", | ||
"Password": "パスワード", | ||
"LogIn": "ログイン", | ||
"WelcomeTo": "ようこそ:", | ||
"LogInWith": "ログイン:", | ||
"Error": "エラー", | ||
} | ||
|
||
var locale_ko = map[string]string{ | ||
"LogInToYourAccount": "귀하의 계정에 로그인하십시오", | ||
"Username": "사용자 이름", | ||
"Password": "암호", | ||
"LogIn": "로그인", | ||
"WelcomeTo": "환영합니다", | ||
"LogInWith": "로그인", | ||
"Error": "오류", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package locales | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestLocales(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
header string | ||
locale map[string]string | ||
}{ | ||
{ | ||
name: "Test empty 'Accept-Language' request header which defaults to English language", | ||
header: "", | ||
locale: locale_en, | ||
}, | ||
{ | ||
name: "Test 'Accept-Language' request header which favours English language", | ||
header: "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5", | ||
locale: locale_en, | ||
}, | ||
{ | ||
name: "Test 'Accept-Language' request header which favours Japan language", | ||
header: "ja;q=0.8, en;q=0.7", | ||
locale: locale_ja, | ||
}, | ||
{ | ||
name: "Test 'Accept-Language' request header which favours Korean language", | ||
header: "ja;q=0.8, ko;q=0.9", | ||
locale: locale_ko, | ||
}, | ||
{ | ||
name: "Test 'Accept-Language' request header which favours Chinese language", | ||
header: "en;q=0.3, zh;q=0.7", | ||
locale: locale_zh, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if !reflect.DeepEqual(GetLocale(tt.header), tt.locale) { | ||
t.Error(tt.name) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.