-
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
159 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,82 @@ | ||
package locales | ||
|
||
import ( | ||
"golang.org/x/text/language" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type Localization map[string]string | ||
|
||
var supportedLocalizations = map[string]Localization{ | ||
language.English.String(): locale_en, | ||
language.Chinese.String(): locale_zh, | ||
language.Japanese.String(): locale_ja, | ||
language.Korean.String(): locale_ko, | ||
} | ||
|
||
func GetLocale(acceptLangHeader string) Localization { | ||
locale, ok := supportedLocalizations[getPreferredLang(acceptLangHeader)] | ||
if !ok { | ||
return locale_en | ||
} | ||
return locale | ||
} | ||
|
||
func getPreferredLang(acceptLangHeader string) string { | ||
matcher := language.NewMatcher(supportedLangs) | ||
userPrefs, _, err := language.ParseAcceptLanguage(acceptLangHeader) | ||
if err != nil { | ||
klog.Warningf("Error parsing 'Accept-Language' header, falling back to English language: %v", err) | ||
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 = Localization{ | ||
"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 = Localization{ | ||
"LogInToYourAccount": "登录到您的帐户", | ||
"Username": "用户名", | ||
"Password": "密码", | ||
"LogIn": "登录", | ||
"WelcomeTo": "欢迎使用", | ||
"LogInWith": "登录使用", | ||
"Error": "错误", | ||
} | ||
|
||
var locale_ja = Localization{ | ||
"LogInToYourAccount": "アカウントにログイン", | ||
"Username": "ユーザー名", | ||
"Password": "パスワード", | ||
"LogIn": "ログイン", | ||
"WelcomeTo": "ようこそ:", | ||
"LogInWith": "ログイン:", | ||
"Error": "エラー", | ||
} | ||
|
||
var locale_ko = Localization{ | ||
"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,52 @@ | ||
package locales | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestLocales(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
header string | ||
locale Localization | ||
}{ | ||
{ | ||
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, | ||
}, | ||
{ | ||
name: "Test empty 'Accept-Language' request header which doesn't match any supported languages, so defaults to English language", | ||
header: "fr;q=0.5, de;q=0.8", | ||
locale: locale_en, | ||
}, | ||
} | ||
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.