i18n_test.rb 5.4 KB
Newer Older
1 2 3
require "isolation/abstract_unit"

module ApplicationTests
4
  class I18nTest < ActiveSupport::TestCase
5 6 7 8 9 10
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
      FileUtils.rm_rf "#{app_path}/config/environments"
11
      require "rails/all"
12 13
    end

14 15 16 17
    def teardown
      teardown_app
    end

18 19 20 21 22
    def load_app
      require "#{app_path}/config/environment"
    end

    def app
23
      @app ||= Rails.application
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    end

    def assert_fallbacks(fallbacks)
      fallbacks.each do |locale, expected|
        actual = I18n.fallbacks[locale]
        assert_equal expected, actual, "expected fallbacks for #{locale.inspect} to be #{expected.inspect}, but were #{actual.inspect}"
      end
    end

    def assert_no_fallbacks
      assert !I18n.backend.class.included_modules.include?(I18n::Backend::Fallbacks)
    end

    # Locales
38 39 40 41 42
    test "setting another default locale" do
      add_to_config <<-RUBY
        config.i18n.default_locale = :de
      RUBY

43
      load_app
44 45 46
      assert_equal :de, I18n.default_locale
    end

47
    # Load paths
48
    test "no config locales directory present should return empty load path" do
49
      FileUtils.rm_rf "#{app_path}/config/locales"
50
      load_app
51 52 53
      assert_equal [], Rails.application.config.i18n.load_path
    end

54
    test "locale files should be added to the load path" do
J
José Valim 已提交
55
      app_file "config/another_locale.yml", "en:\nfoo: ~"
56

57
      add_to_config <<-RUBY
58
        config.i18n.load_path << config.root.join("config/another_locale.yml").to_s
59 60
      RUBY

61 62 63 64 65 66 67
      load_app
      assert_equal [
        "#{app_path}/config/locales/en.yml", "#{app_path}/config/another_locale.yml"
      ], Rails.application.config.i18n.load_path

      assert I18n.load_path.include?("#{app_path}/config/locales/en.yml")
      assert I18n.load_path.include?("#{app_path}/config/another_locale.yml")
68 69
    end

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    test "load_path is populated before eager loaded models" do
      add_to_config <<-RUBY
        config.cache_classes = true
      RUBY

      app_file "config/locales/en.yml", <<-YAML
en:
  foo: "1"
      YAML

      app_file 'app/models/foo.rb', <<-RUBY
        class Foo < ActiveRecord::Base
          @foo = I18n.t(:foo)
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do
88
          get '/i18n',   :to => lambda { |env| [200, {}, [Foo.instance_variable_get('@foo')]] }
89 90 91 92 93 94 95 96 97 98 99
        end
      RUBY

      require 'rack/test'
      extend Rack::Test::Methods
      load_app

      get "/i18n"
      assert_equal "1", last_response.body
    end

100
    test "locales are reloaded if they change between requests" do
101
      add_to_config <<-RUBY
102
        config.cache_classes = false
103 104
      RUBY

105 106 107 108 109 110
      app_file "config/locales/en.yml", <<-YAML
en:
  foo: "1"
      YAML

      app_file 'config/routes.rb', <<-RUBY
111
        AppTemplate::Application.routes.draw do
112
          get '/i18n',   :to => lambda { |env| [200, {}, [I18n.t(:foo)]] }
113 114 115 116 117 118 119 120 121 122
        end
      RUBY

      require 'rack/test'
      extend Rack::Test::Methods
      load_app

      get "/i18n"
      assert_equal "1", last_response.body

123 124 125
      # Wait a full second so we have time for changes to propagate
      sleep(1)

126 127 128 129 130 131 132 133 134 135 136
      app_file "config/locales/en.yml", <<-YAML
en:
  foo: "2"
      YAML

      get "/i18n"
      assert_equal "2", last_response.body
    end

    # Fallbacks
    test "not using config.i18n.fallbacks does not initialize I18n.fallbacks" do
J
José Valim 已提交
137
      I18n.backend = Class.new(I18n::Backend::Simple).new
138 139 140 141 142 143 144 145
      load_app
      assert_no_fallbacks
    end

    test "config.i18n.fallbacks = true initializes I18n.fallbacks with default settings" do
      I18n::Railtie.config.i18n.fallbacks = true
      load_app
      assert I18n.backend.class.included_modules.include?(I18n::Backend::Fallbacks)
146
      assert_fallbacks de: [:de, :en]
147 148 149 150
    end

    test "config.i18n.fallbacks = true initializes I18n.fallbacks with default settings even when backend changes" do
      I18n::Railtie.config.i18n.fallbacks = true
J
José Valim 已提交
151
      I18n::Railtie.config.i18n.backend = Class.new(I18n::Backend::Simple).new
152 153
      load_app
      assert I18n.backend.class.included_modules.include?(I18n::Backend::Fallbacks)
154
      assert_fallbacks de: [:de, :en]
155 156 157 158 159
    end

    test "config.i18n.fallbacks.defaults = [:'en-US'] initializes fallbacks with en-US as a fallback default" do
      I18n::Railtie.config.i18n.fallbacks.defaults = [:'en-US']
      load_app
160
      assert_fallbacks de: [:de, :'en-US', :en]
161 162 163 164 165
    end

    test "config.i18n.fallbacks.map = { :ca => :'es-ES' } initializes fallbacks with a mapping ca => es-ES" do
      I18n::Railtie.config.i18n.fallbacks.map = { :ca => :'es-ES' }
      load_app
166
      assert_fallbacks ca: [:ca, :"es-ES", :es, :en]
167 168 169 170 171
    end

    test "[shortcut] config.i18n.fallbacks = [:'en-US'] initializes fallbacks with en-US as a fallback default" do
      I18n::Railtie.config.i18n.fallbacks = [:'en-US']
      load_app
172
      assert_fallbacks de: [:de, :'en-US', :en]
173 174 175 176 177
    end

    test "[shortcut] config.i18n.fallbacks = [{ :ca => :'es-ES' }] initializes fallbacks with a mapping de-AT => de-DE" do
      I18n::Railtie.config.i18n.fallbacks.map = { :ca => :'es-ES' }
      load_app
178
      assert_fallbacks ca: [:ca, :"es-ES", :es, :en]
179 180 181 182 183
    end

    test "[shortcut] config.i18n.fallbacks = [:'en-US', { :ca => :'es-ES' }] initializes fallbacks with the given arguments" do
      I18n::Railtie.config.i18n.fallbacks = [:'en-US', { :ca => :'es-ES' }]
      load_app
184
      assert_fallbacks ca: [:ca, :"es-ES", :es, :'en-US', :en]
185 186
    end
  end
187
end