logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

gettext_test.exs (5989B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.GettextTest do
  5. use ExUnit.Case
  6. require Pleroma.Web.Gettext
  7. test "put_locales/1: set the first in the list to Gettext's locale" do
  8. Pleroma.Web.Gettext.put_locales(["zh_Hans", "en_test"])
  9. assert "zh_Hans" == Gettext.get_locale(Pleroma.Web.Gettext)
  10. end
  11. test "with_locales/2: reset locale on exit" do
  12. old_first_locale = Gettext.get_locale(Pleroma.Web.Gettext)
  13. old_locales = Pleroma.Web.Gettext.get_locales()
  14. Pleroma.Web.Gettext.with_locales ["zh_Hans", "en_test"] do
  15. assert "zh_Hans" == Gettext.get_locale(Pleroma.Web.Gettext)
  16. assert ["zh_Hans", "en_test"] == Pleroma.Web.Gettext.get_locales()
  17. end
  18. assert old_first_locale == Gettext.get_locale(Pleroma.Web.Gettext)
  19. assert old_locales == Pleroma.Web.Gettext.get_locales()
  20. end
  21. describe "handle_missing_translation/5" do
  22. test "fallback to next locale if some translation is not available" do
  23. Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
  24. assert "xxYour account is awaiting approvalxx" ==
  25. Pleroma.Web.Gettext.dpgettext(
  26. "static_pages",
  27. "approval pending email subject",
  28. "Your account is awaiting approval"
  29. )
  30. end
  31. end
  32. test "putting en locale at the front should not make gettext fallback unexpectedly" do
  33. Pleroma.Web.Gettext.with_locales ["en", "en_test"] do
  34. assert "Your account is awaiting approval" ==
  35. Pleroma.Web.Gettext.dpgettext(
  36. "static_pages",
  37. "approval pending email subject",
  38. "Your account is awaiting approval"
  39. )
  40. end
  41. end
  42. test "duplicated locale in list should not result in infinite loops" do
  43. Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do
  44. assert "xxYour account is awaiting approvalxx" ==
  45. Pleroma.Web.Gettext.dpgettext(
  46. "static_pages",
  47. "approval pending email subject",
  48. "Your account is awaiting approval"
  49. )
  50. end
  51. end
  52. test "direct interpolation" do
  53. Pleroma.Web.Gettext.with_locales ["en_test"] do
  54. assert "xxYour digest from some instancexx" ==
  55. Pleroma.Web.Gettext.dpgettext(
  56. "static_pages",
  57. "digest email subject",
  58. "Your digest from %{instance_name}",
  59. instance_name: "some instance"
  60. )
  61. end
  62. end
  63. test "fallback with interpolation" do
  64. Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
  65. assert "xxYour digest from some instancexx" ==
  66. Pleroma.Web.Gettext.dpgettext(
  67. "static_pages",
  68. "digest email subject",
  69. "Your digest from %{instance_name}",
  70. instance_name: "some instance"
  71. )
  72. end
  73. end
  74. test "fallback to msgid" do
  75. Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
  76. assert "Your digest from some instance" ==
  77. Pleroma.Web.Gettext.dpgettext(
  78. "static_pages",
  79. "digest email subject",
  80. "Your digest from %{instance_name}",
  81. instance_name: "some instance"
  82. )
  83. end
  84. end
  85. end
  86. describe "handle_missing_plural_translation/7" do
  87. test "direct interpolation" do
  88. Pleroma.Web.Gettext.with_locales ["en_test"] do
  89. assert "xx1 New Followerxx" ==
  90. Pleroma.Web.Gettext.dpngettext(
  91. "static_pages",
  92. "new followers count header",
  93. "%{count} New Follower",
  94. "%{count} New Followers",
  95. 1,
  96. count: 1
  97. )
  98. assert "xx5 New Followersxx" ==
  99. Pleroma.Web.Gettext.dpngettext(
  100. "static_pages",
  101. "new followers count header",
  102. "%{count} New Follower",
  103. "%{count} New Followers",
  104. 5,
  105. count: 5
  106. )
  107. end
  108. end
  109. test "fallback with interpolation" do
  110. Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
  111. assert "xx1 New Followerxx" ==
  112. Pleroma.Web.Gettext.dpngettext(
  113. "static_pages",
  114. "new followers count header",
  115. "%{count} New Follower",
  116. "%{count} New Followers",
  117. 1,
  118. count: 1
  119. )
  120. assert "xx5 New Followersxx" ==
  121. Pleroma.Web.Gettext.dpngettext(
  122. "static_pages",
  123. "new followers count header",
  124. "%{count} New Follower",
  125. "%{count} New Followers",
  126. 5,
  127. count: 5
  128. )
  129. end
  130. end
  131. test "fallback to msgid" do
  132. Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
  133. assert "1 New Follower" ==
  134. Pleroma.Web.Gettext.dpngettext(
  135. "static_pages",
  136. "new followers count header",
  137. "%{count} New Follower",
  138. "%{count} New Followers",
  139. 1,
  140. count: 1
  141. )
  142. assert "5 New Followers" ==
  143. Pleroma.Web.Gettext.dpngettext(
  144. "static_pages",
  145. "new followers count header",
  146. "%{count} New Follower",
  147. "%{count} New Followers",
  148. 5,
  149. count: 5
  150. )
  151. end
  152. end
  153. end
  154. end