Faker: Faker::Address.country does not work if your apps does not use :en as a locale

Created on 22 Oct 2014  路  12Comments  路  Source: faker-ruby/faker

Rail 3.2
Faker 1.4.3

When I use Faker::Address.country with config.i18n.available_locales = [:fr,:it] I have the following error :

Failure/Error: let(:user) { FactoryGirl.create(:user_with_recurring_order_to_generate)}
 I18n::InvalidLocale:
   :en is not a valid locale

It stop if I remove Faker::Address.country from my factory or if I ad :en to available locales.

BuFixes

Most helpful comment

I recently faced it again with Faker::Name.last_name

All 12 comments

Same here, adding :en to available_locales seems to do it.

Had the same issue with Faker::Company.name. Adding this to your config/environments/test.rb resolves the issue for now:

config.i18n.available_locales += %w(en)

The issue is in faker.rb, over here: https://github.com/stympy/faker/blob/master/lib/faker.rb#L128 -- For apps that don't have 'en' as a valid locale, this won't work.

Same with Faker::Internet.mail

This issue is still open and Faker does not work for apps which don't support english. I am stuck at version 1.5.0

@bsylvain @coorasse I'm a new collaborator, and I'm trying to solve and close the issues.

I know it's been a while since you guys discussed about this issue. I would like to ask you if we still need to work on this issue and how I could help.

Thanks 馃憤

Hello @vbrazo, yes. the issue is still open and valid. I haven't investigated what is causing it so I cannot help you on which direction to follow to solve it. I think the expected behaviour is that the generator works also if english is not available.

I still have to add :en on the test env on my apps using Faker. It makes the test env different from production.

This was fixed by #1377.

It doesn't seem like. If I use the latest version on master I obtain the following:

irb(main):002:0> Faker::Address.country
Traceback (most recent call last):
        1: from (irb):2
I18n::MissingTranslationData (translation missing: en.faker.address.country)

Oops, sorry for the false notice then! :S

I recently faced it again with Faker::Name.last_name

I worked around it by dropping this in my test_helper.rb (must run after faker is loaded).

# Workaround for optimization where I18n::Backend::Simple doesn't load
# translations for locales not in available_locales, thus making them
# unavailable even though faker disables enforce_available_locales while
# looking up translations..
i18n_did_enforce_available_locales = I18n.enforce_available_locales
begin
  I18n.enforce_available_locales = false
  I18n.reload!
  I18n.eager_load!
ensure
  I18n.enforce_available_locales = i18n_did_enforce_available_locales
end

It's not really pretty, but gets the job done and since available_locales are enforced by I18n on both lookup and loading of translations, it should hopefully not change behaviour for other users.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tagliala picture tagliala  路  5Comments

lassiter picture lassiter  路  3Comments

kevgathuku picture kevgathuku  路  4Comments

supiash1 picture supiash1  路  5Comments

huyderman picture huyderman  路  4Comments