77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
// Copyright 2022 E99p1ant. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package i18n
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// TestDictionaryComplete 校验四种语言的字典 key 完全一致,防止漏翻译。
|
|
func TestDictionaryComplete(t *testing.T) {
|
|
ref := dictionary[LangZhCN]
|
|
langs := []string{LangZhTW, LangEn, LangJa}
|
|
|
|
for _, lang := range langs {
|
|
dict := dictionary[lang]
|
|
if len(dict) != len(ref) {
|
|
t.Errorf("%s: 词条数量 %d != 简体中文 %d", lang, len(dict), len(ref))
|
|
}
|
|
for key := range ref {
|
|
if _, ok := dict[key]; !ok {
|
|
t.Errorf("%s: 缺少词条 %q", lang, key)
|
|
}
|
|
}
|
|
for key := range dict {
|
|
if _, ok := ref[key]; !ok {
|
|
t.Errorf("%s: 多出简体中文没有的词条 %q", lang, key)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResolve(t *testing.T) {
|
|
cases := map[string]string{
|
|
"": LangZhCN,
|
|
"zh-cn": LangZhCN,
|
|
"zh": LangZhCN,
|
|
"zh-TW": LangZhTW,
|
|
"zh-Hant": LangZhTW,
|
|
"en": LangEn,
|
|
"en-US": LangEn,
|
|
"ja": LangJa,
|
|
"ja-JP": LangJa,
|
|
"fr": LangZhCN, // 未知回退
|
|
" En ": LangEn, // 自动去除首尾空白
|
|
"zh_cn": LangZhCN,
|
|
"zh_tw": LangZhTW,
|
|
"zh_hk": LangZhTW, // 香港繁体(下划线变体)
|
|
}
|
|
for in, want := range cases {
|
|
if got := Resolve(in); got != want {
|
|
t.Errorf("Resolve(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestT(t *testing.T) {
|
|
if got := T(LangZhCN, "nav.explore"); got != "探索" {
|
|
t.Errorf("zh-CN nav.explore = %q", got)
|
|
}
|
|
if got := T(LangEn, "nav.explore"); got != "Explore" {
|
|
t.Errorf("en nav.explore = %q", got)
|
|
}
|
|
if got := T(LangJa, "nav.explore"); got != "探索" {
|
|
t.Errorf("ja nav.explore = %q", got)
|
|
}
|
|
// 参数填充
|
|
if got := T(LangEn, "site.footer.visitor", "42"); got != "You are visitor #42" {
|
|
t.Errorf("en site.footer.visitor with arg = %q", got)
|
|
}
|
|
// 未知 key 回退返回 key 本身
|
|
if got := T(LangEn, "no.such.key"); got != "no.such.key" {
|
|
t.Errorf("unknown key = %q", got)
|
|
}
|
|
}
|