Skip to content

Commit

Permalink
Fixed translation fallbacks in ruby 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
stympy committed Sep 6, 2011
1 parent be92191 commit b139ead
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/faker.rb
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions 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-####-########']

0 comments on commit b139ead

Please sign in to comment.