2010-01-26 07:57:11 -05:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class I18nTest < ActiveSupport::TestCase
|
2010-01-26 07:57:11 -05:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
FileUtils.rm_rf "#{app_path}/config/environments"
|
2010-06-20 08:44:38 -04:00
|
|
|
require "rails/all"
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
def load_app
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
2010-07-01 11:07:48 -04:00
|
|
|
@app ||= Rails.application
|
2010-06-20 08:44:38 -04:00
|
|
|
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
|
2010-01-26 07:57:11 -05:00
|
|
|
test "setting another default locale" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.i18n.default_locale = :de
|
|
|
|
RUBY
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
load_app
|
2010-01-26 07:57:11 -05:00
|
|
|
assert_equal :de, I18n.default_locale
|
|
|
|
end
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
# Load paths
|
2010-01-26 07:57:11 -05:00
|
|
|
test "no config locales dir present should return empty load path" do
|
|
|
|
FileUtils.rm_rf "#{app_path}/config/locales"
|
2010-06-20 08:44:38 -04:00
|
|
|
load_app
|
2010-01-26 07:57:11 -05:00
|
|
|
assert_equal [], Rails.application.config.i18n.load_path
|
|
|
|
end
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
test "locale files should be added to the load path" do
|
2011-06-12 06:44:00 -04:00
|
|
|
app_file "config/another_locale.yml", "en:\nfoo: ~"
|
2010-06-20 08:44:38 -04:00
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
add_to_config <<-RUBY
|
2010-06-20 08:44:38 -04:00
|
|
|
config.i18n.load_path << config.root.join("config/another_locale.yml").to_s
|
2010-01-26 07:57:11 -05:00
|
|
|
RUBY
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
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")
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
|
2011-01-28 07:53:07 -05:00
|
|
|
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
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/i18n', :to => lambda { |env| [200, {}, [Foo.instance_variable_get('@foo')]] }
|
2011-01-28 07:53:07 -05:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
load_app
|
|
|
|
|
|
|
|
get "/i18n"
|
|
|
|
assert_equal "1", last_response.body
|
|
|
|
end
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
test "locales are reloaded if they change between requests" do
|
2010-01-26 07:57:11 -05:00
|
|
|
add_to_config <<-RUBY
|
2010-06-20 08:44:38 -04:00
|
|
|
config.cache_classes = false
|
2010-01-26 07:57:11 -05:00
|
|
|
RUBY
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
app_file "config/locales/en.yml", <<-YAML
|
|
|
|
en:
|
|
|
|
foo: "1"
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/i18n', :to => lambda { |env| [200, {}, [I18n.t(:foo)]] }
|
2010-06-20 08:44:38 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
load_app
|
|
|
|
|
|
|
|
get "/i18n"
|
|
|
|
assert_equal "1", last_response.body
|
|
|
|
|
2011-12-16 07:22:06 -05:00
|
|
|
# Wait a full second so we have time for changes to propagate
|
|
|
|
sleep(1)
|
|
|
|
|
2010-06-20 08:44:38 -04:00
|
|
|
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
|
2011-06-12 06:44:00 -04:00
|
|
|
I18n.backend = Class.new(I18n::Backend::Simple).new
|
2010-06-20 08:44:38 -04:00
|
|
|
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)
|
|
|
|
assert_fallbacks :de => [:de, :en]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.i18n.fallbacks = true initializes I18n.fallbacks with default settings even when backend changes" do
|
|
|
|
I18n::Railtie.config.i18n.fallbacks = true
|
2011-06-12 06:44:00 -04:00
|
|
|
I18n::Railtie.config.i18n.backend = Class.new(I18n::Backend::Simple).new
|
2010-06-20 08:44:38 -04:00
|
|
|
load_app
|
|
|
|
assert I18n.backend.class.included_modules.include?(I18n::Backend::Fallbacks)
|
|
|
|
assert_fallbacks :de => [:de, :en]
|
|
|
|
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
|
|
|
|
assert_fallbacks :de => [:de, :'en-US', :en]
|
|
|
|
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
|
|
|
|
assert_fallbacks :ca => [:ca, :"es-ES", :es, :en]
|
|
|
|
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
|
|
|
|
assert_fallbacks :de => [:de, :'en-US', :en]
|
|
|
|
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
|
|
|
|
assert_fallbacks :ca => [:ca, :"es-ES", :es, :en]
|
|
|
|
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
|
|
|
|
assert_fallbacks :ca => [:ca, :"es-ES", :es, :'en-US', :en]
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
end
|
2010-08-05 16:47:01 -04:00
|
|
|
end
|