From b139eadff6a575d7c93549168c95479ad1ef5474 Mon Sep 17 00:00:00 2001 From: Benjamin Curtis Date: Tue, 6 Sep 2011 15:46:14 -0700 Subject: [PATCH] Fixed translation fallbacks in ruby 1.8 --- lib/faker.rb | 4 ++-- test/test_locale.rb | 27 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/faker.rb b/lib/faker.rb index 364787afcf..3f366f1b8f 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -49,12 +49,12 @@ def translate(*args) opts = args.last.is_a?(Hash) ? args.pop : {} opts[:locale] ||= Faker::Config.locale opts[:throw] = true - I18n.translate(*args, opts) + I18n.translate(*(args.push(opts))) rescue # Super-simple fallback -- fallback to en if the # translation was missing. If the translation isn't # in en either, then it will raise again. - I18n.translate(*args, opts.merge(:locale => :en)) + I18n.translate(*(args.push(opts.merge(:locale => :en)))) end end end diff --git a/test/test_locale.rb b/test/test_locale.rb index e043dcf5b2..f1a54ad3a0 100644 --- a/test/test_locale.rb +++ b/test/test_locale.rb @@ -1,14 +1,31 @@ -require "test/unit" +require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') -require "faker" +LoadedYaml = ['en', 'en-bork'].inject({}) do |h, locale| + h[locale] = YAML.load_file(File.expand_path(File.dirname(__FILE__) + "/../lib/locales/#{locale}.yml"))[locale]['faker'] + h +end class TestLocale < Test::Unit::TestCase - def test_case_name + def test_locale_separate_from_i18n I18n.locale = :en Faker::Config.locale = :de assert Faker::PhoneNumber.phone_number.match(/\(0\d+\) \d+|\+49-\d+-\d+/) assert Faker::Address.street_name.match(//) end + + def test_configured_locale_translation + Faker::Config.locale = 'en-bork' + assert_equal Faker::Base.translate('faker.lorem.words').first, LoadedYaml['en-bork']['lorem']['words'].first + end + + def test_locale_override_when_calling_translate + Faker::Config.locale = 'en-bork' + assert_equal Faker::Base.translate('faker.lorem.words', :locale => :en).first, LoadedYaml['en']['lorem']['words'].first + end + + def test_translation_fallback + Faker::Config.locale = 'en-bork' + assert_nil LoadedYaml['en-bork']['name'] + assert_equal Faker::Base.translate('faker.name.first_name').first, LoadedYaml['en']['name']['first_name'].first + end end - -# formats: ['(0###) #########', '(0####) #######', '+49-###-#######', '+49-####-########']