Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary disable i18n fallback in Rails

I18n fallback is loaded:

 I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

Any idea now to temporary disable it? I have forms, where I want to edit various language versions, and with fallback I am getting fields with default language, if given translation is yet not present.

like image 548
mdrozdziel Avatar asked Sep 10 '10 07:09

mdrozdziel


3 Answers

You can pass the fallback: true option to I18n.t, which will prevent I18n from looking up other locales (see implementation here). But it's probably not part of the public API...

like image 181
iGEL Avatar answered Oct 28 '22 05:10

iGEL


You can pass :fallback => 'false' on your I18n.translate calls, but this is not part of the public API.

Another way you might want to try is the following:

I18n.available_locales.each do
  |al| I18n.fallbacks.merge!({al => [al]})
end

This will basically make the fallback for each available locale to include only itself. So, if the translation is not found in itself, then there is not fallback to fall back to.

However, then you need to find a way to restore to the default fallback.

You can do that for example with a statement like:

I18n.available_locales.each do
  |al| I18n.fallbacks.merge!({al => [al, I18n.default_locale]})
end
like image 36
p.matsinopoulos Avatar answered Oct 28 '22 06:10

p.matsinopoulos


if anyone is still wondering how to do that, you can change the I18n.fallbacks on the fly:

def foo
  I18n.fallbacks[:at] = [:at]
  # do stuff with I18n#t
ensure
  I18n.fallbacks[:at] = [:at, :de] # or whatever is was before
end

Not sure how safe is that though.

like image 36
Max Prokopiev Avatar answered Oct 28 '22 04:10

Max Prokopiev