i18n_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package i18n
  14. import (
  15. "os"
  16. "testing"
  17. )
  18. var knownTestLocale = "en_US.UTF-8"
  19. func TestTranslation(t *testing.T) {
  20. err := LoadTranslations("test", func() string { return "default" })
  21. if err != nil {
  22. t.Errorf("Unexpected error: %v", err)
  23. }
  24. result := T("test_string")
  25. if result != "foo" {
  26. t.Errorf("expected: %s, saw: %s", "foo", result)
  27. }
  28. }
  29. func TestTranslationPlural(t *testing.T) {
  30. err := LoadTranslations("test", func() string { return "default" })
  31. if err != nil {
  32. t.Errorf("Unexpected error: %v", err)
  33. }
  34. result := T("test_plural", 3)
  35. if result != "there were 3 items" {
  36. t.Errorf("expected: %s, saw: %s", "there were 3 items", result)
  37. }
  38. result = T("test_plural", 1)
  39. if result != "there was 1 item" {
  40. t.Errorf("expected: %s, saw: %s", "there was 1 item", result)
  41. }
  42. }
  43. func TestTranslationUsingEnvVar(t *testing.T) {
  44. // We must backup and restore env vars before setting test values in tests
  45. // othervise we are risking to break other tests/test cases
  46. // which rely on the same env vars
  47. envVarsToBackup := []string{"LC_MESSAGES", "LANG", "LC_ALL"}
  48. expectedStrEnUSLocale := "baz"
  49. expectedStrFallback := "foo"
  50. testCases := []struct {
  51. name string
  52. setenvFn func()
  53. expectedStr string
  54. }{
  55. {
  56. name: "Only LC_ALL is set",
  57. setenvFn: func() { os.Setenv("LC_ALL", knownTestLocale) },
  58. expectedStr: expectedStrEnUSLocale,
  59. },
  60. {
  61. name: "Only LC_MESSAGES is set",
  62. setenvFn: func() { os.Setenv("LC_MESSAGES", knownTestLocale) },
  63. expectedStr: expectedStrEnUSLocale,
  64. },
  65. {
  66. name: "Only LANG",
  67. setenvFn: func() { os.Setenv("LANG", knownTestLocale) },
  68. expectedStr: expectedStrEnUSLocale,
  69. },
  70. {
  71. name: "LC_MESSAGES overrides LANG",
  72. setenvFn: func() {
  73. os.Setenv("LANG", "be_BY.UTF-8") // Unknown locale
  74. os.Setenv("LC_MESSAGES", knownTestLocale)
  75. },
  76. expectedStr: expectedStrEnUSLocale,
  77. },
  78. {
  79. name: "LC_ALL overrides LANG",
  80. setenvFn: func() {
  81. os.Setenv("LANG", "be_BY.UTF-8") // Unknown locale
  82. os.Setenv("LC_ALL", knownTestLocale)
  83. },
  84. expectedStr: expectedStrEnUSLocale,
  85. },
  86. {
  87. name: "LC_ALL overrides LC_MESSAGES",
  88. setenvFn: func() {
  89. os.Setenv("LC_MESSAGES", "be_BY.UTF-8") // Unknown locale
  90. os.Setenv("LC_ALL", knownTestLocale)
  91. },
  92. expectedStr: expectedStrEnUSLocale,
  93. },
  94. {
  95. name: "Unknown locale in LANG",
  96. setenvFn: func() { os.Setenv("LANG", "be_BY.UTF-8") },
  97. expectedStr: expectedStrFallback,
  98. },
  99. {
  100. name: "Unknown locale in LC_MESSAGES",
  101. setenvFn: func() { os.Setenv("LC_MESSAGES", "be_BY.UTF-8") },
  102. expectedStr: expectedStrFallback,
  103. },
  104. {
  105. name: "Unknown locale in LC_ALL",
  106. setenvFn: func() { os.Setenv("LC_ALL", "be_BY.UTF-8") },
  107. expectedStr: expectedStrFallback,
  108. },
  109. {
  110. name: "Invalid env var",
  111. setenvFn: func() { os.Setenv("LC_MESSAGES", "fake.locale.UTF-8") },
  112. expectedStr: expectedStrFallback,
  113. },
  114. {
  115. name: "No env vars",
  116. setenvFn: func() {},
  117. expectedStr: expectedStrFallback,
  118. },
  119. }
  120. for _, test := range testCases {
  121. t.Run(test.name, func(t *testing.T) {
  122. for _, envVar := range envVarsToBackup {
  123. if envVarValue := os.Getenv(envVar); envVarValue != "" {
  124. os.Unsetenv(envVar)
  125. // Restore env var at the end
  126. defer func() { os.Setenv(envVar, envVarValue) }()
  127. }
  128. }
  129. test.setenvFn()
  130. err := LoadTranslations("test", nil)
  131. if err != nil {
  132. t.Errorf("Unexpected error: %v", err)
  133. }
  134. result := T("test_string")
  135. if result != test.expectedStr {
  136. t.Errorf("expected: %s, saw: %s", test.expectedStr, result)
  137. }
  138. })
  139. }
  140. }