i18n_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package i18n
  2. import "testing"
  3. func TestDefaultIsChinese(t *testing.T) {
  4. Set("zh") // ensure a clean Chinese baseline regardless of test order
  5. if got := T("已启用"); got != "已启用" {
  6. t.Fatalf("zh: T(已启用) = %q, want 已启用", got)
  7. }
  8. }
  9. func TestEnglishTranslation(t *testing.T) {
  10. Set("en")
  11. defer Set("zh")
  12. if got := T("已启用"); got != "Enabled" {
  13. t.Fatalf("en: T(已启用) = %q, want Enabled", got)
  14. }
  15. if got := T("美国"); got != "United States" {
  16. t.Fatalf("en: T(美国) = %q, want United States", got)
  17. }
  18. // unknown strings fall back to the Chinese key unchanged
  19. if got := T("未收录的字符串"); got != "未收录的字符串" {
  20. t.Fatalf("en: unknown string = %q, want fallback unchanged", got)
  21. }
  22. }
  23. func TestTfInterpolation(t *testing.T) {
  24. Set("en")
  25. defer Set("zh")
  26. got := Tf("设备数量已达上限,最多只能添加 %d 台设备", 5)
  27. want := "Device limit reached; at most 5 devices can be added."
  28. if got != want {
  29. t.Fatalf("Tf = %q, want %q", got, want)
  30. }
  31. // region reason: country name itself is translated before interpolation
  32. got = Tf("SIM 卡归属地为%s(MCC %s),本服务不向该地区卡片提供数据/短信/VoWiFi", T("中国"), "460")
  33. want = "The SIM's home region is China (MCC 460); this service does not provide data, SMS, or VoWiFi to cards from that region."
  34. if got != want {
  35. t.Fatalf("Tf region = %q, want %q", got, want)
  36. }
  37. }
  38. func TestSetNormalizesUnknownToEnglish(t *testing.T) {
  39. Set("fr") // unsupported -> treated as English
  40. defer Set("zh")
  41. if Lang() != "en" {
  42. t.Fatalf("Lang after Set(fr) = %q, want en", Lang())
  43. }
  44. }