38

I am getting an "translation missing" error message from Rails 3:

activerecord:
  notices:
    messages:
      success: 
        create: "Something was created"
    models:
      user:
        success:
          create: "Thanks for registration"

I18n.t("activerecord.notices.models.user.success.create")
# => "Thanks for registration"


I18n.t("activerecord.notices.models.book.success.create") 
# => "translation missing: de, activerecord, notices, models, book, success, create"

I don't know why the book model doesn't get the fallback massage. I have set config.i18n.fallbacks = true.

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
antpaw
  • 15,444
  • 11
  • 59
  • 88
  • the key book is not present in file. Look at the same and try again by adding book key. – Naren Sisodiya Nov 22 '10 at 08:47
  • yes its not there, but thats the main reason to use a fallback. you dont need fallbacks if you define everything. but i cant do that. its to much useless work for me. – antpaw Nov 22 '10 at 09:48
  • 1
    Would be nice if you edit the question to clarify more. Which locale are you in now, and which locale do you want to fallback to? (seems like you have your own German locale, but why do you write English in it?) – lulalala May 22 '13 at 04:23

6 Answers6

105

I set in config/application.rb usually

    config.i18n.fallbacks = [:de, :fr, :en]

So you can declare the order of the fallback.

But keep attention in some environments/*.rb the configuration is overwritten.

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
raskhadafi
  • 1,342
  • 2
  • 14
  • 19
  • 3
    This **should not** be the accepted answer, the OP asked for key fallback (AKA defaults), not for language fallback. – rewritten Jul 15 '15 at 21:46
  • @rewritten can you clarify a bit more? I felt I can't distinguish these two very clearly, and can't find discussion online about it either. – lulalala Sep 15 '15 at 03:25
  • 1
    @lulalala key fallback (default) means that if a specific key is not found in translations, another key is searched instead: `I18n.t("activerecord.notices.models.book.success.create", :default => I18n.t("activerecord.notices.messages.success.create"))`. Language fallback allows to skip some translations in one language and the translation in another language (same key) is used. – rewritten Sep 15 '15 at 13:27
  • Caution: The order of fallback in this array is incorrect! The fallbacks are searched left to right! – Christopher Oezbek Mar 17 '21 at 22:32
45

When a :default option is given, its value will be returned if the translation is missing:

I18n.t :missing, :default => 'Not here'
# => 'Not here'

More info here

egze
  • 3,200
  • 23
  • 23
  • ok thanks, this seems to work for me "I18n.t("activerecord.notices.models.#{object.to_s.downcase}.#{type}", :default => I18n.t("activerecord.notices.messages.#{type}"))" – antpaw Nov 22 '10 at 11:14
18

[Answer is for Rails 2]

Have you enabled fallbacks for your backend? Assuming it's Simple(based on yml in example):

Put this in an initializer:

require "i18n/backend/fallbacks" 
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

https://github.com/svenfuchs/i18n/wiki/Fallbacks

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
user456733
  • 351
  • 1
  • 3
18

In rails 3+, this is set in the config/environments files :

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true
Alain Beauvois
  • 5,896
  • 3
  • 44
  • 26
13

I believe the best way to handle a missing string, is to display a default locale, rather than an error message.

Add this line in application.rb to fallback to the english locale.

config.i18n.fallbacks = [:en]

In case you want to specify locale-specific fallbacks, you can use the following:

config.i18n.fallbacks = {:de => [:de,:en], :en => [:en,:de]}

Also, note that you can enable and disable fallbacks based on your environment. So while on development it might make sense to have an error displayed, you can instead enable fallbacks in your environments/production.rb with the following:

config.i18n.fallbacks = true
pastullo
  • 4,171
  • 3
  • 30
  • 36
  • 1
    Currently I disagree. If a fellow developer changes or deletes a translation key and the app later deploys, you may get the default locale string and have no knowledge of the error, then later the customer complains. It could be better to create a test spec to ensure all keys for a certain language are in place – Rui Nunes Jan 14 '16 at 13:13
  • Using a fallback on itself doesn't make any sense: `:de => [:de,:en]` is just the same as `:de => [:en]` or just `de: :en`. – Christopher Oezbek Mar 17 '21 at 22:41
7

There is a misunderstanding with the I18n Fallback feature.

This feature causes that when there is a missing translation exception (in this case, it happens when I18n fails to find the value associated with the "activerecord.notices.models.book.success.create" key in the locale files of your current language) I18n will lookup in the predefined list of fallbacks languages the value of the key that generated the missing translation exception, if it's found I18n will returned that value, but if it's not found in any of those other locale files I18n will return the missing translation exception.

So when you defined config.i18n.fallbacks = true, that doesn't mean that when a missing translation exception occurs, in this case:

I18n.t("activerecord.models.book.success.create")
# => "translation missing: de, activerecord, notices, models, book, success, create"

I18n will lookup a similar key in your locale files to return his value, could be:

I18n.t("activerecord.models.user.success.create")
# => "Thanks for registration"

What will happens it's that I18n will lookup in yours defaults fallback languages for the specific language where the missing translation exception has occurred.


A good example of usage will be:

# using :"en-US" as a default locale:
I18n.default_locale = :"en-US" 
I18n.fallbacks[:es] # => [:es, :"en-US", :en]

Locales files:

es:
  activerecord:
    notices:
      messages:
        success: 
          create: "Algo fue creado"
      models:
        user:
          success:
            create: "Gracias por registrarte"
en-US:
  activerecord:
      ...
      models:
        books:
          success:
            create: "The model was created" 

Call in English site:

I18n.t("activerecord.models.books.success.create")
# => "The model was created"

Call in Spanish site:

#with config.i18n.fallbacks = false
I18n.t("activerecord.models.books.success.create")
# => "translation missing: es, activerecord, models, book, success, create"

#with config.i18n.fallbacks = true
I18n.t("activerecord.models.books.success.create")
# => "The model was created"

For more information check: https://github.com/ruby-i18n/i18n

LeFranck
  • 89
  • 1
  • 5