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

Fallback to the primary locale #135

Open
pablobfonseca opened this issue Jun 21, 2017 · 1 comment
Open

Fallback to the primary locale #135

pablobfonseca opened this issue Jun 21, 2017 · 1 comment

Comments

@pablobfonseca
Copy link

Hi,

Is there a way to fallback the missing translations to the primary_locale?

Let's say I've the following primary locale:

foo:
  example: "Baz"

When I create a new locale file dynamically, let's say:

bar: {}

I'd like to fallback example from foo locale when it's called in the context of bar

<%= t('.bar.example') %>
# I want this to return "Baz"

Does anybody know if this is possible?

Thanks in advance

@westonganger
Copy link

westonganger commented Feb 28, 2021

Using a custom exception handler

module I18n
  class CustomExceptionHandler < ExceptionHandler
    def call(exception, locale, key, options)
      if exception.is_a?(MissingTranslation) && key.to_s != 'i18n.plural.rule'
        if I18n.default_locale.to_s == locale.to_s
          raise exception.to_exception
        else
          I18n.t(key, locale: I18n.default_locale)
        end
      else
        super
      end
    end
  end
end


I18n.exception_handler = I18n::CustomExceptionHandler.new

OR using built in fallbacks functionality

https://github.com/ruby-i18n/i18n/wiki/Fallbacks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants