printer_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package locale // import "miniflux.app/locale"
  5. import "testing"
  6. func TestTranslateWithMissingLanguage(t *testing.T) {
  7. defaultCatalog = catalog{}
  8. translation := NewPrinter("invalid").Printf("missing.key")
  9. if translation != "missing.key" {
  10. t.Errorf(`Wrong translation, got %q`, translation)
  11. }
  12. }
  13. func TestTranslateWithMissingKey(t *testing.T) {
  14. defaultCatalog = catalog{
  15. "en_US": translationDict{
  16. "k": "v",
  17. },
  18. }
  19. translation := NewPrinter("en_US").Printf("missing.key")
  20. if translation != "missing.key" {
  21. t.Errorf(`Wrong translation, got %q`, translation)
  22. }
  23. }
  24. func TestTranslateWithExistingKey(t *testing.T) {
  25. defaultCatalog = catalog{
  26. "en_US": translationDict{
  27. "auth.username": "Login",
  28. },
  29. }
  30. translation := NewPrinter("en_US").Printf("auth.username")
  31. if translation != "Login" {
  32. t.Errorf(`Wrong translation, got %q`, translation)
  33. }
  34. }
  35. func TestTranslateWithExistingKeyAndPlaceholder(t *testing.T) {
  36. defaultCatalog = catalog{
  37. "en_US": translationDict{
  38. "key": "Test: %s",
  39. },
  40. "fr_FR": translationDict{
  41. "key": "Test : %s",
  42. },
  43. }
  44. translation := NewPrinter("fr_FR").Printf("key", "ok")
  45. if translation != "Test : ok" {
  46. t.Errorf(`Wrong translation, got %q`, translation)
  47. }
  48. }
  49. func TestTranslateWithMissingKeyAndPlaceholder(t *testing.T) {
  50. defaultCatalog = catalog{
  51. "en_US": translationDict{
  52. "auth.username": "Login",
  53. },
  54. "fr_FR": translationDict{
  55. "auth.username": "Identifiant",
  56. },
  57. }
  58. translation := NewPrinter("fr_FR").Printf("Status: %s", "ok")
  59. if translation != "Status: ok" {
  60. t.Errorf(`Wrong translation, got %q`, translation)
  61. }
  62. }
  63. func TestTranslateWithInvalidValue(t *testing.T) {
  64. defaultCatalog = catalog{
  65. "en_US": translationDict{
  66. "auth.username": "Login",
  67. },
  68. "fr_FR": translationDict{
  69. "auth.username": true,
  70. },
  71. }
  72. translation := NewPrinter("fr_FR").Printf("auth.username")
  73. if translation != "auth.username" {
  74. t.Errorf(`Wrong translation, got %q`, translation)
  75. }
  76. }
  77. func TestTranslatePluralWithDefaultRule(t *testing.T) {
  78. defaultCatalog = catalog{
  79. "en_US": translationDict{
  80. "number_of_users": []string{"%d user (%s)", "%d users (%s)"},
  81. },
  82. "fr_FR": translationDict{
  83. "number_of_users": []string{"%d utilisateur (%s)", "%d utilisateurs (%s)"},
  84. },
  85. }
  86. printer := NewPrinter("fr_FR")
  87. translation := printer.Plural("number_of_users", 1, 1, "some text")
  88. expected := "1 utilisateur (some text)"
  89. if translation != expected {
  90. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  91. }
  92. translation = printer.Plural("number_of_users", 2, 2, "some text")
  93. expected = "2 utilisateurs (some text)"
  94. if translation != expected {
  95. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  96. }
  97. }
  98. func TestTranslatePluralWithRussianRule(t *testing.T) {
  99. defaultCatalog = catalog{
  100. "en_US": translationDict{
  101. "time_elapsed.years": []string{"%d year", "%d years"},
  102. },
  103. "ru_RU": translationDict{
  104. "time_elapsed.years": []string{"%d год назад", "%d года назад", "%d лет назад"},
  105. },
  106. }
  107. printer := NewPrinter("ru_RU")
  108. translation := printer.Plural("time_elapsed.years", 1, 1)
  109. expected := "1 год назад"
  110. if translation != expected {
  111. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  112. }
  113. translation = printer.Plural("time_elapsed.years", 2, 2)
  114. expected = "2 года назад"
  115. if translation != expected {
  116. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  117. }
  118. translation = printer.Plural("time_elapsed.years", 5, 5)
  119. expected = "5 лет назад"
  120. if translation != expected {
  121. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  122. }
  123. }
  124. func TestTranslatePluralWithMissingTranslation(t *testing.T) {
  125. defaultCatalog = catalog{
  126. "en_US": translationDict{
  127. "number_of_users": []string{"%d user (%s)", "%d users (%s)"},
  128. },
  129. "fr_FR": translationDict{},
  130. }
  131. translation := NewPrinter("fr_FR").Plural("number_of_users", 2)
  132. expected := "number_of_users"
  133. if translation != expected {
  134. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  135. }
  136. }
  137. func TestTranslatePluralWithInvalidValues(t *testing.T) {
  138. defaultCatalog = catalog{
  139. "en_US": translationDict{
  140. "number_of_users": []string{"%d user (%s)", "%d users (%s)"},
  141. },
  142. "fr_FR": translationDict{
  143. "number_of_users": "must be a slice",
  144. },
  145. }
  146. translation := NewPrinter("fr_FR").Plural("number_of_users", 2)
  147. expected := "number_of_users"
  148. if translation != expected {
  149. t.Errorf(`Wrong translation, got %q instead of %q`, translation, expected)
  150. }
  151. }