i18n_test.rb 14.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
# encoding: utf-8
require 'test_helper'

class I18nTest < I18n::TestCase
  def setup
    super
    store_translations(:en, :currency => { :format => { :separator => '.', :delimiter => ',', } })
    store_translations(:nl, :currency => { :format => { :separator => ',', :delimiter => '.', } })
  end

  test "exposes its VERSION constant" do
    assert I18n::VERSION
  end

  test "uses the simple backend by default" do
    assert I18n.backend.is_a?(I18n::Backend::Simple)
  end

  test "can set the backend" do
    begin
      assert_nothing_raised { I18n.backend = self }
      assert_equal self, I18n.backend
    ensure
      I18n.backend = I18n::Backend::Simple.new
    end
  end

  test "uses :en as a default_locale by default" do
    assert_equal :en, I18n.default_locale
  end

  test "can set the default locale" do
    begin
      assert_nothing_raised { I18n.default_locale = 'de' }
      assert_equal :de, I18n.default_locale
    ensure
      I18n.default_locale = :en
    end
  end

  test "default_locale= doesn't ignore junk" do
    assert_raise(NoMethodError) { I18n.default_locale = Class }
  end

  test "raises an I18n::InvalidLocale exception when setting an unavailable default locale" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.default_locale = :klingon }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "uses the default locale as a locale by default" do
    assert_equal I18n.default_locale, I18n.locale
  end

  test "sets the current locale to Thread.current" do
    assert_nothing_raised { I18n.locale = 'de' }
    assert_equal :de, I18n.locale
    assert_equal :de, Thread.current[:i18n_config].locale
    I18n.locale = :en
  end

  test "locale= doesn't ignore junk" do
    assert_raise(NoMethodError) { I18n.locale = Class }
  end

  test "raises an I18n::InvalidLocale exception when setting an unavailable locale" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.locale = :klingon }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "can set the configuration object" do
    begin
      I18n.config = self
      assert_equal self, I18n.config
      assert_equal self, Thread.current[:i18n_config]
    ensure
      I18n.config = ::I18n::Config.new
    end
  end

  test "locale is not shared between configurations" do
    a = I18n::Config.new
    b = I18n::Config.new
    a.locale = :fr
    b.locale = :es
    assert_equal :fr, a.locale
    assert_equal :es, b.locale
    assert_equal :en, I18n.locale
  end

  test "other options are shared between configurations" do
    begin
      a = I18n::Config.new
      b = I18n::Config.new
      a.default_locale = :fr
      b.default_locale = :es
      assert_equal :es, a.default_locale
      assert_equal :es, b.default_locale
      assert_equal :es, I18n.default_locale
    ensure
      I18n.default_locale = :en
    end
  end

  test "uses a dot as a default_separator by default" do
    assert_equal '.', I18n.default_separator
  end

  test "can set the default_separator" do
    begin
      assert_nothing_raised { I18n.default_separator = "\001" }
    ensure
      I18n.default_separator = '.'
    end
  end

  test "normalize_keys normalizes given locale, keys and scope to an array of single-key symbols" do
    assert_equal [:en, :foo, :bar], I18n.normalize_keys(:en, :bar, :foo)
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo.bar')
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, 'baz.buz', 'foo.bar')
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, %w(baz buz), %w(foo bar))
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, [:baz, :buz], [:foo, :bar])
  end

  test "normalize_keys discards empty keys" do
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz..buz', :'foo..bar')
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz......buz', :'foo......bar')
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, ['baz', nil, '', 'buz'], ['foo', nil, '', 'bar'])
  end

  test "normalize_keys uses a given separator" do
    assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|')
  end

  test "can set the exception_handler" do
    begin
      previous_exception_handler = I18n.exception_handler
      assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
    ensure
      I18n.exception_handler = previous_exception_handler
    end
  end

  test "uses a custom exception handler set to I18n.exception_handler" do
    begin
      previous_exception_handler = I18n.exception_handler
      I18n.exception_handler = :custom_exception_handler
      I18n.expects(:custom_exception_handler)
      I18n.translate :bogus
    ensure
      I18n.exception_handler = previous_exception_handler
    end
  end

  test "uses a custom exception handler passed as an option" do
    I18n.expects(:custom_exception_handler)
    I18n.translate(:bogus, :exception_handler => :custom_exception_handler)
  end

  test "delegates translate calls to the backend" do
    I18n.backend.expects(:translate).with('de', :foo, {})
    I18n.translate :foo, :locale => 'de'
  end

  test "delegates localize calls to the backend" do
    I18n.backend.expects(:localize).with('de', :whatever, :default, {})
    I18n.localize :whatever, :locale => 'de'
  end

  test "translate given no locale uses the current locale" do
    I18n.backend.expects(:translate).with(:en, :foo, {})
    I18n.translate :foo
  end

  test "translate works with nested symbol keys" do
    assert_equal ".", I18n.t(:'currency.format.separator')
  end

  test "translate works with nested string keys" do
    assert_equal ".", I18n.t('currency.format.separator')
  end

  test "translate with an array as a scope works" do
    assert_equal ".", I18n.t(:separator, :scope => %w(currency format))
  end

  test "translate with an array containing dot separated strings as a scope works" do
    assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
  end

  test "translate with an array of keys and a dot separated string as a scope works" do
    assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
  end

  test "translate with an array of dot separated keys and a scope works" do
    assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
  end

  # def test_translate_given_no_args_raises_missing_translation_data
  #   assert_equal "translation missing: en, no key", I18n.t
  # end

  test "translate given a bogus key returns an error message" do
    assert_equal "translation missing: en.bogus", I18n.t(:bogus)
  end

  test "translate given an empty string as a key raises an I18n::ArgumentError" do
    assert_raise(I18n::ArgumentError) { I18n.t("") }
  end

  test "translate given an unavailable locale rases an I18n::InvalidLocale" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.t(:foo, :locale => 'klingon') }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "available_locales can be replaced at runtime" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.t(:foo, :locale => 'klingon') }
      old_locales, I18n.config.available_locales = I18n.config.available_locales, [:klingon]
      I18n.t(:foo, :locale => 'klingon')
    ensure
      I18n.config.enforce_available_locales = false
      I18n.config.available_locales = old_locales
    end
  end

  test "available_locales_set should return a set" do
    assert_equal Set, I18n.config.available_locales_set.class
    assert_equal I18n.config.available_locales.size * 2, I18n.config.available_locales_set.size
  end

  test "exists? given an existing key will return true" do
    assert_equal true, I18n.exists?(:currency)
  end

  test "exists? given a non-existing key will return false" do
    assert_equal false, I18n.exists?(:bogus)
  end

  test "exists? given an existing dot-separated key will return true" do
    assert_equal true, I18n.exists?('currency.format.delimiter')
  end

  test "exists? given a non-existing dot-separated key will return false" do
    assert_equal false, I18n.exists?('currency.format.bogus')
  end

  test "exists? given an existing key and an existing locale will return true" do
    assert_equal true, I18n.exists?(:currency, :nl)
  end

  test "exists? given a non-existing key and an existing locale will return false" do
    assert_equal false, I18n.exists?(:bogus, :nl)
  end

  test "localize given nil raises an I18n::ArgumentError" do
    assert_raise(I18n::ArgumentError) { I18n.l nil }
  end

  test "localize given an Object raises an I18n::ArgumentError" do
    assert_raise(I18n::ArgumentError) { I18n.l Object.new }
  end

  test "localize given an unavailable locale rases an I18n::InvalidLocale" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.l(Time.now, :locale => 'klingon') }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "can use a lambda as an exception handler" do
    begin
      previous_exception_handler = I18n.exception_handler
      I18n.exception_handler = Proc.new { |exception, locale, key, options| key }
      assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
    ensure
      I18n.exception_handler = previous_exception_handler
    end
  end

  test "can use an object responding to #call as an exception handler" do
    begin
      previous_exception_handler = I18n.exception_handler
      I18n.exception_handler = Class.new do
        def call(exception, locale, key, options); key; end
      end.new
      assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
    ensure
      I18n.exception_handler = previous_exception_handler
    end
  end

  test "I18n.with_locale temporarily sets the given locale" do
    store_translations(:en, :foo => 'Foo in :en')
    store_translations(:de, :foo => 'Foo in :de')
    store_translations(:pl, :foo => 'Foo in :pl')

    I18n.with_locale      { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
    I18n.with_locale(:de) { assert_equal [:de, 'Foo in :de'], [I18n.locale, I18n.t(:foo)] }
    I18n.with_locale(:pl) { assert_equal [:pl, 'Foo in :pl'], [I18n.locale, I18n.t(:foo)] }
    I18n.with_locale(:en) { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }

    assert_equal I18n.default_locale, I18n.locale
  end

  test "I18n.with_locale resets the locale in case of errors" do
    assert_raise(I18n::ArgumentError) { I18n.with_locale(:pl) { raise I18n::ArgumentError } }
    assert_equal I18n.default_locale, I18n.locale
  end

  test "I18n.translitarate handles I18n::ArgumentError exception" do
    I18n::Backend::Transliterator.stubs(:get).raises(I18n::ArgumentError)
    I18n.exception_handler.expects(:call).raises(I18n::ArgumentError)
    assert_raise(I18n::ArgumentError) {
      I18n.transliterate("ąćó")
    }
  end

  test "I18n.translitarate raises I18n::ArgumentError exception" do
    I18n::Backend::Transliterator.stubs(:get).raises(I18n::ArgumentError)
    I18n.exception_handler.expects(:call).never
    assert_raise(I18n::ArgumentError) {
      I18n.transliterate("ąćó", :raise => true)
    }
  end

  test "transliterate given an unavailable locale rases an I18n::InvalidLocale" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.transliterate('string', :locale => 'klingon') }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "I18n.locale_available? returns true when the passed locale is available" do
    I18n.available_locales = [:en, :de]
    assert_equal true, I18n.locale_available?(:de)
  end

  test "I18n.locale_available? returns true when the passed locale is a string and is available" do
    I18n.available_locales = [:en, :de]
    assert_equal true, I18n.locale_available?('de')
  end

  test "I18n.locale_available? returns false when the passed locale is unavailable" do
    assert_equal false, I18n.locale_available?(:klingon)
  end

  test "I18n.enforce_available_locales! raises an I18n::InvalidLocale when the passed locale is unavailable" do
    begin
      I18n.config.enforce_available_locales = true
      assert_raise(I18n::InvalidLocale) { I18n.enforce_available_locales!(:klingon) }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "I18n.enforce_available_locales! does nothing when the passed locale is available" do
    I18n.available_locales = [:en, :de]
    begin
      I18n.config.enforce_available_locales = true
      assert_nothing_raised { I18n.enforce_available_locales!(:en) }
    ensure
      I18n.config.enforce_available_locales = false
    end
  end

  test "I18n.enforce_available_locales config can be set to false" do
    begin
      I18n.config.enforce_available_locales = false
      assert_equal false, I18n.config.enforce_available_locales
    ensure
      I18n.config.enforce_available_locales = false
    end
  end
  
  test 'I18n.reload! reloads the set of locales that are enforced' do
    begin
      # Clear the backend that affects the available locales and somehow can remain
      # set from the last running test.
      # For instance, it contains enough translations to cause a false positive with
      # this test when ran with --seed=50992
      I18n.backend = I18n::Backend::Simple.new
      
      assert !I18n.available_locales.include?(:de), "Available locales should not include :de at this point"
      
      I18n.enforce_available_locales = true

      assert_raise(I18n::InvalidLocale) { I18n.default_locale = :de }
      assert_raise(I18n::InvalidLocale) { I18n.locale = :de }

      store_translations(:de, :foo => 'Foo in :de')

      assert_raise(I18n::InvalidLocale) { I18n.default_locale = :de }
      assert_raise(I18n::InvalidLocale) { I18n.locale = :de }

      I18n.reload!

      store_translations(:en, :foo => 'Foo in :en')
      store_translations(:de, :foo => 'Foo in :de')
      store_translations(:pl, :foo => 'Foo in :pl')
      
      assert I18n.available_locales.include?(:de), ":de should now be allowed"
      assert I18n.available_locales.include?(:en), ":en should now be allowed"
      assert I18n.available_locales.include?(:pl), ":pl should now be allowed"
      
      assert_nothing_raised { I18n.default_locale = I18n.locale = :en }
      assert_nothing_raised { I18n.default_locale = I18n.locale = :de }
      assert_nothing_raised { I18n.default_locale = I18n.locale = :pl }
    ensure
      I18n.enforce_available_locales = false
    end
  end
end