Skip to content

Commit

Permalink
Fallback translation without available locales enforcement (faker-rub…
Browse files Browse the repository at this point in the history
…y#1377)

* Fallback translation without available locales enforcing

* Available locales can't be edited in place

The previous spec would fail to cover the new code, and would pass
without the changes.

* Add ensures for max safety

* Add tests for `with_locale` method
  • Loading branch information
deivid-rodriguez authored and vbrazo committed Sep 29, 2018
1 parent c52a736 commit 3804df9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,19 @@ def translate(*args)
# 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.push(opts))
disable_enforce_available_locales do
I18n.translate(*args.push(opts))
end
end

# Executes block with given locale set.
def with_locale(tmp_locale = nil)
current_locale = Faker::Config.own_locale
Faker::Config.locale = tmp_locale
I18n.with_locale(tmp_locale) { yield }

disable_enforce_available_locales do
I18n.with_locale(tmp_locale) { yield }
end
ensure
Faker::Config.locale = current_locale
end
Expand Down Expand Up @@ -229,6 +234,14 @@ def rand(max = nil)
0
end
end

def disable_enforce_available_locales
old_enforce_available_locales = I18n.enforce_available_locales
I18n.enforce_available_locales = false
yield
ensure
I18n.enforce_available_locales = old_enforce_available_locales
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/test_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ def test_translation_fallback
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
end

def test_translation_fallback_without_en_in_available_locales
I18n.available_locales -= [:en]
Faker::Config.locale = 'en-BORK'
assert_nil LoadedYaml['en-BORK']['name']
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
ensure
I18n.available_locales += [:en]
end

def test_with_locale_does_not_fail_without_the_locale_in_available_locales
I18n.available_locales -= [:en]
Faker::Base.with_locale(:en) do
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
end
ensure
I18n.available_locales += [:en]
end

def test_with_locale_changes_locale_temporarily
Faker::Config.locale = 'en-BORK'
I18n.with_locale(:en) do
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
end
assert_equal 'en-BORK', Faker::Config.locale
end

def test_regex
Faker::Config.locale = 'en-GB'
re = /[A-PR-UWYZ]([A-HK-Y][0-9][ABEHMNPRVWXY0-9]?|[0-9][ABCDEFGHJKPSTUW0-9]?) [0-9][ABD-HJLNP-UW-Z]{2}/
Expand Down

0 comments on commit 3804df9

Please sign in to comment.