Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't include default_locale in list of fallbacks #338

Closed
kirahowe opened this issue Apr 22, 2016 · 6 comments · Fixed by #415
Closed

Don't include default_locale in list of fallbacks #338

kirahowe opened this issue Apr 22, 2016 · 6 comments · Fixed by #415
Milestone

Comments

@kirahowe
Copy link

kirahowe commented Apr 22, 2016

Hi everyone,

Is it currently possible to not include the default locale in the list of fallbacks? I'm working on a Rails app and right now I have this in my app config:

config.i18n.fallbacks = {'en-CA' => 'en',
                         'en-US' => 'en',
                         'fr-CA' => 'fr',
                         'ja-JP' => 'ja'}
config.default_locale = 'en-CA'

But the the fallback for 'fr-CA' is actually:

[
    [0] :"fr-CA",
    [1] :fr,
    [2] :"en-CA",
    [3] :en
]

I don't want en-CA or en to be there, because I want i18n to raise a MissingTranslationData exception so that I know where I have translations missing (actionview wraps these in a .translation-missing span which makes it easy to find the missing translations).

Is this currently possible? Or would it require a custom implementation of the Fallbacks class?

@ngan
Copy link

ngan commented Oct 16, 2016

@kiramclean a possible solution is to set the default_locale to a dummy/empty locale:

config.i18n.default_locale = :empty

Then create the empty locale file (config/locales/empty.yml):

# This is purposefully empty so that this can be the final fallback locale. Since it's empty, anything hitting this locale would raise a MissingTranslationData exception.
empty:

@aesnyder
Copy link

I tried this and still found that I got an english translation instead of a missing translation exception.

@kirahowe
Copy link
Author

@ngan The problem is I don't actually want the default_locale to be a dummy locale.. if I'm missing a translation in en-CA or en-US, for example, I still want the app to fall back to the actual default I set for those locales, but if I'm missing a translation in, say fr-CA, under no circumstances do I want the fallback to be en.

@ngan
Copy link

ngan commented Oct 18, 2016

$  cat config/locales/dummy.yml 
dummy:

Then set fallbacks to true (instead of what you have)

config.i18n.fallbacks = true
config.i18n.default_locale = :dummy

Then either, specify all your available_locales:

config.i18n.available_locales = [:dummy, :en, :"en-CA", ...]

or just set config.i18n.enforce_available_locales = false.

Then the fallback logic will be automatic:

>> I18n.fallbacks[:"fr-CA"]
=> [:"fr-CA", :fr, :dummy]

>> I18n.fallbacks[:"en-CA"]
=> [:"en-CA", :en, :dummy]

>> I18n.t(:foo)
=> "translation missing: dummy.foo"

@ngan
Copy link

ngan commented Oct 18, 2016

Just a tidbit:

>> I18n.fallbacks[:"en-CA-X1-X2-X3"]
=> [:"en-CA-X1-X2-X3", :"en-CA-X1-X2", :"en-CA-X1", :"en-CA", :en, :dummy]

@radar
Copy link
Collaborator

radar commented Nov 20, 2016

Thanks everyone. I think @ngan's workaround is great. If someone wants to submit a PR to fix this, then I'd be open to that.

@radar radar added this to the 0.9.0 milestone Nov 20, 2016
@radar radar modified the milestones: 0.9.0, 0.10.0 Oct 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants