i18n_test.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. require 'test_helper'
  2. class I18nTest < Test::Unit::TestCase
  3. def setup
  4. I18n.backend.store_translations(:'en', :currency => { :format => { :separator => '.', :delimiter => ',', } })
  5. end
  6. test "exposes its VERSION constant" do
  7. assert I18n::VERSION
  8. end
  9. test "uses the simple backend by default" do
  10. assert I18n.backend.is_a?(I18n::Backend::Simple)
  11. end
  12. test "can set the backend" do
  13. begin
  14. assert_nothing_raised { I18n.backend = self }
  15. assert_equal self, I18n.backend
  16. ensure
  17. I18n.backend = I18n::Backend::Simple.new
  18. end
  19. end
  20. test "uses :en as a default_locale by default" do
  21. assert_equal :en, I18n.default_locale
  22. end
  23. test "can set the default locale" do
  24. begin
  25. assert_nothing_raised { I18n.default_locale = 'de' }
  26. assert_equal :de, I18n.default_locale
  27. ensure
  28. I18n.default_locale = :en
  29. end
  30. end
  31. test "uses the default locale as a locale by default" do
  32. assert_equal I18n.default_locale, I18n.locale
  33. end
  34. test "sets the current locale to Thread.current" do
  35. assert_nothing_raised { I18n.locale = 'de' }
  36. assert_equal :de, I18n.locale
  37. assert_equal :de, Thread.current[:i18n_config].locale
  38. I18n.locale = :en
  39. end
  40. test "can set the configuration object" do
  41. begin
  42. I18n.config = self
  43. assert_equal self, I18n.config
  44. assert_equal self, Thread.current[:i18n_config]
  45. ensure
  46. I18n.config = ::I18n::Config.new
  47. end
  48. end
  49. test "locale is not shared between configurations" do
  50. a = I18n::Config.new
  51. b = I18n::Config.new
  52. a.locale = :fr
  53. b.locale = :es
  54. assert_equal :fr, a.locale
  55. assert_equal :es, b.locale
  56. assert_equal :en, I18n.locale
  57. end
  58. test "other options are shared between configurations" do
  59. begin
  60. a = I18n::Config.new
  61. b = I18n::Config.new
  62. a.default_locale = :fr
  63. b.default_locale = :es
  64. assert_equal :es, a.default_locale
  65. assert_equal :es, b.default_locale
  66. assert_equal :es, I18n.default_locale
  67. ensure
  68. I18n.default_locale = :en
  69. end
  70. end
  71. test "uses a dot as a default_separator by default" do
  72. assert_equal '.', I18n.default_separator
  73. end
  74. test "can set the default_separator" do
  75. begin
  76. assert_nothing_raised { I18n.default_separator = "\001" }
  77. ensure
  78. I18n.default_separator = '.'
  79. end
  80. end
  81. test "normalize_keys normalizes given locale, keys and scope to an array of single-key symbols" do
  82. assert_equal [:en, :foo, :bar], I18n.normalize_keys(:en, :bar, :foo)
  83. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo.bar')
  84. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, 'baz.buz', 'foo.bar')
  85. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, %w(baz buz), %w(foo bar))
  86. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, [:baz, :buz], [:foo, :bar])
  87. end
  88. test "normalize_keys discards empty keys" do
  89. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz..buz', :'foo..bar')
  90. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz......buz', :'foo......bar')
  91. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, ['baz', nil, '', 'buz'], ['foo', nil, '', 'bar'])
  92. end
  93. test "normalize_keys uses a given separator" do
  94. assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|')
  95. end
  96. test "can set the exception_handler" do
  97. begin
  98. previous_exception_handler = I18n.exception_handler
  99. assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
  100. ensure
  101. I18n.exception_handler = previous_exception_handler
  102. end
  103. end
  104. test "uses a custom exception handler set to I18n.exception_handler" do
  105. begin
  106. previous_exception_handler = I18n.exception_handler
  107. I18n.exception_handler = :custom_exception_handler
  108. I18n.expects(:custom_exception_handler)
  109. I18n.translate :bogus
  110. ensure
  111. I18n.exception_handler = previous_exception_handler
  112. end
  113. end
  114. test "uses a custom exception handler passed as an option" do
  115. I18n.expects(:custom_exception_handler)
  116. I18n.translate(:bogus, :exception_handler => :custom_exception_handler)
  117. end
  118. test "delegates translate calls to the backend" do
  119. I18n.backend.expects(:translate).with('de', :foo, {})
  120. I18n.translate :foo, :locale => 'de'
  121. end
  122. test "delegates localize calls to the backend" do
  123. I18n.backend.expects(:localize).with('de', :whatever, :default, {})
  124. I18n.localize :whatever, :locale => 'de'
  125. end
  126. test "translate given no locale uses the current locale" do
  127. I18n.backend.expects(:translate).with(:en, :foo, {})
  128. I18n.translate :foo
  129. end
  130. test "translate works with nested symbol keys" do
  131. assert_equal ".", I18n.t(:'currency.format.separator')
  132. end
  133. test "translate works with nested string keys" do
  134. assert_equal ".", I18n.t('currency.format.separator')
  135. end
  136. test "translate with an array as a scope works" do
  137. assert_equal ".", I18n.t(:separator, :scope => %w(currency format))
  138. end
  139. test "translate with an array containing dot separated strings as a scope works" do
  140. assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
  141. end
  142. test "translate with an array of keys and a dot separated string as a scope works" do
  143. assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
  144. end
  145. test "translate with an array of dot separated keys and a scope works" do
  146. assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
  147. end
  148. # def test_translate_given_no_args_raises_missing_translation_data
  149. # assert_equal "translation missing: en, no key", I18n.t
  150. # end
  151. test "translate given a bogus key returns an error message" do
  152. assert_equal "translation missing: en.bogus", I18n.t(:bogus)
  153. end
  154. test "translate given an empty string as a key raises an I18n::ArgumentError" do
  155. assert_raise(I18n::ArgumentError) { I18n.t("") }
  156. end
  157. test "localize given nil raises an I18n::ArgumentError" do
  158. assert_raise(I18n::ArgumentError) { I18n.l nil }
  159. end
  160. test "localize givan an Object raises an I18n::ArgumentError" do
  161. assert_raise(I18n::ArgumentError) { I18n.l Object.new }
  162. end
  163. test "can use a lambda as an exception handler" do
  164. begin
  165. previous_exception_handler = I18n.exception_handler
  166. I18n.exception_handler = Proc.new { |exception, locale, key, options| key }
  167. assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
  168. ensure
  169. I18n.exception_handler = previous_exception_handler
  170. end
  171. end
  172. test "can use an object responding to #call as an exception handler" do
  173. begin
  174. previous_exception_handler = I18n.exception_handler
  175. I18n.exception_handler = Class.new do
  176. def call(exception, locale, key, options); key; end
  177. end.new
  178. assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
  179. ensure
  180. I18n.exception_handler = previous_exception_handler
  181. end
  182. end
  183. test "I18n.with_locale temporarily sets the given locale" do
  184. store_translations(:en, :foo => 'Foo in :en')
  185. store_translations(:de, :foo => 'Foo in :de')
  186. store_translations(:pl, :foo => 'Foo in :pl')
  187. I18n.with_locale { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
  188. I18n.with_locale(:de) { assert_equal [:de, 'Foo in :de'], [I18n.locale, I18n.t(:foo)] }
  189. I18n.with_locale(:pl) { assert_equal [:pl, 'Foo in :pl'], [I18n.locale, I18n.t(:foo)] }
  190. I18n.with_locale(:en) { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
  191. assert_equal I18n.default_locale, I18n.locale
  192. end
  193. test "I18n.with_locale resets the locale in case of errors" do
  194. assert_raise(I18n::ArgumentError) { I18n.with_locale(:pl) { raise I18n::ArgumentError } }
  195. assert_equal I18n.default_locale, I18n.locale
  196. end
  197. end