faker: Faker::Lorem.paragraph raises I18n::MissingTranslationData without manual locale override

I18n::MissingTranslationData: translation missing: en.faker.lorem.words

This was introduced in v1.4.0 and is still a problem in v1.4.3. <del>If I manually set the local to “en,” it works as expected, but prior to v1.4.0, manually setting the locale wasn’t necessary (tested with v1.2.0 and v1.3.0).</del>

UPDATE: I was wrong about the locale override working. This appears to exhibit itself only when Faker is used inside an ERB file and when that ERB file is loaded from a rake file.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 2
  • Comments: 37 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Anyone have ideas on a workaround? Getting this issue from rake and from the console, and for many methods, not just words:

± bin/rails c
Loading development environment (Rails 4.1.8)
[1] pry(main)> require 'faker'
=> true
[2] pry(main)> Faker::Lorem.words(2)
I18n::MissingTranslationData: translation missing: en.faker.lorem.words
from /Users/thom/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/i18n-0.6.11/lib/i18n.rb:314:in `handle_exception'
[3] pry(main)> Faker::Company.name
I18n::MissingTranslationData: translation missing: en.faker.company.name
from /Users/thom/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/i18n-0.6.11/lib/i18n.rb:314:in `handle_exception'
[4] pry(main)> Faker::VERSION
=> "1.4.3"
...

Calling Faker::Config.locale = 'en' doesn’t seem to make any difference.

EDIT Found workaround: add I18n.reload! sometime after you first require 'faker' in your environment. Source: this super-old issue

After some experimenting, I found for my case that it was the enforce_available_locale that was causing this.

I have only one available locale set in my rails app (:fr), so when Faker fallback to :en locale then it raises MissingTranslation because :en locale was not loaded in a first place :

When faker load its locales, the enforce available locale is defaulted to true by Rails if not set, and so the :en locale file is skipped, raising later a MissingTranslation error in case the fallback to :en is needed.

Here is how I18n filter the given .yml in load_paths :

def store_translations(locale, data, options = EMPTY_HASH)
          if I18n.enforce_available_locales &&
            I18n.available_locales_initialized? &&
            !I18n.available_locales.include?(locale.to_sym) &&
            !I18n.available_locales.include?(locale.to_s)
            return data
          end
          locale = locale.to_sym
          translations[locale] ||= {}
          data = data.deep_symbolize_keys
          translations[locale].deep_merge!(data)
end

Notice how if enforce_available_locale is true then the .yml locale must be specified in available_locales in order to be loaded.

Maybe Faker could force load the :en yml locale files into I18n, because it is needed internally by Faker, even if the whole Rails app doesnt need english at all. This can handle the case where the fallback to :en locale is needed , but the Rails env has no :en in available locale and enforce is true by default.

I noticed Faker already plays with enforce_available_locale so it could be used as well when loading the locales :

I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')]
I18n.reload! if I18n.backend.initialized?

To fix it in the Rails app, I manually set up config.i18n.enforce_available_locales = false in my config\environments\test.rb so when Faker loads the locales, nothing is skipped.

Other solution could be to add :en to available_locale for the test env, before Faker is loading yml locales.

Using I18n.reload! doesn’t seem to fix it for me with rails 5.5.2, faker 1.9.3, rspec-core 3.8.0, and ruby 2.5.1

require 'faker'
I18n.reload!

fixed for me as well

Warning: This error will also occur if you accidentally use .uniq instead of .unique.

Do you think we could possibly add this as an alias?

Any solutions to this 5 year old problem?

I tried I18n.reload! without success Faker::Config.locale also did nothing for me

As @OlecsandrKirpa said. If u have more then one locale available for the application, just let rails know in config/application.rb

config.i18n.available_locales = %i[ur_locale en]

Note the answer from @vbrazo above: The right solution to this error is to change Faker::Name.title to Faker::Job.title in your code.

But if you just want to make the error go away, and I18n.reload! isn’t working for you, you can add the missing translation to your config/locales/en.yml (or other locale) file:

en:
  faker:
    name:
      title: 'Title'

Also seeing this with Faker::Name.title on Ruby 2.6.2

$ irb
2.6.2 :001 > require 'faker'
 => true 
2.6.2 :002 > Faker::VERSION
 => "1.9.3" 
2.6.2 :003 > Faker::Name.title
Traceback (most recent call last):
       11: from /Users/mskazmie/.rvm/rubies/ruby-2.6.2/bin/irb:23:in `<main>'
       10: from /Users/mskazmie/.rvm/rubies/ruby-2.6.2/bin/irb:23:in `load'
        9: from /Users/mskazmie/.rvm/rubies/ruby-2.6.2/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        8: from (irb):3
        7: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:192:in `method_missing'
        6: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:151:in `translate'
        5: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:163:in `rescue in translate'
        4: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:244:in `disable_enforce_available_locales'
        3: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:164:in `block in translate'
        2: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/i18n-1.6.0/lib/i18n.rb:195:in `translate'
        1: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/i18n-1.6.0/lib/i18n.rb:357:in `handle_exception'
I18n::MissingTranslationData (translation missing: en.faker.name.title)
2.6.2 :004 > Faker::Config.locale
 => :en 
2.6.2 :005 > I18n.reload!
 => nil 
2.6.2 :006 > Faker::Name.title
Traceback (most recent call last):
       11: from /Users/mskazmie/.rvm/rubies/ruby-2.6.2/bin/irb:23:in `<main>'
       10: from /Users/mskazmie/.rvm/rubies/ruby-2.6.2/bin/irb:23:in `load'
        9: from /Users/mskazmie/.rvm/rubies/ruby-2.6.2/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        8: from (irb):6
        7: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:192:in `method_missing'
        6: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:151:in `translate'
        5: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:163:in `rescue in translate'
        4: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:244:in `disable_enforce_available_locales'
        3: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/faker-1.9.3/lib/faker.rb:164:in `block in translate'
        2: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/i18n-1.6.0/lib/i18n.rb:195:in `translate'
        1: from /Users/mskazmie/.rvm/gems/ruby-2.6.2/gems/i18n-1.6.0/lib/i18n.rb:357:in `handle_exception'
I18n::MissingTranslationData (translation missing: en.faker.name.title)

I’m getting it when using faker from FactoryGirl factories, but those are driven by rake tasks.

It would be helpful if someone could provide a sample app and info on the environment that can reliably reproduce this error.

@DaKaZ Faker doesn’t provide Faker::Name.title anymore. It was deprecated last year and removed recently. Please use Faker::Job: https://github.com/stympy/faker/blob/master/doc/default/job.md.

I juste never call require ‘faker’ directly in rails. Where should I add the I18n.reload! to make it work with Rspec ?

Hello we 're having the same problem with this setup: rails 3.2.22.2, ruby 2.3.7, faker 1.9.1 In the Gemfile i tried gem ‘faker’ with and without require: false Our default locale is :de. In config/environments/test.rb i specified config.i18n.default_locale = :en. The setting of Faker::Config.locale = :fr in spec_helper.rb is ignored.

$ rails c test             
Loading test environment (Rails 3.2.22.2)
irb(main):001:0> I18n.locale
=> :en
irb(main):002:0> require 'faker'
=> true
irb(main):003:0> I18n.reload!
=> nil
irb(main):004:0> Faker::StarWars.quote
I18n::MissingTranslationData: translation missing: en.faker.star_wars.quotes
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/i18n-0.8.6/lib/i18n.rb:311:in `handle_exception'
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/i18n-0.8.6/lib/i18n.rb:161:in `translate'
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/faker-1.9.1/lib/faker.rb:162:in `rescue in translate'
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/faker-1.9.1/lib/faker.rb:150:in `translate'
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/faker-1.9.1/lib/faker/star_wars.rb:65:in `quote'
        from (irb):4
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-3.2.22.2/lib/rails/commands/console.rb:47:in `start'
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-3.2.22.2/lib/rails/commands/console.rb:8:in `start'
        from /home/name/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-3.2.22.2/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

How can i tell Faker to not try to translate the phrases?

This is still an issue for me on 1.9.1 using Faker::Beer.brand. None of the above workarounds worked.

yeah the I18n.reload! got it working for me too.