printer_test.go 4.6 KB

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