So that's the problem. In my application globalize2 returns a NIL string if there's no translation on some record, instead of falling back to default_locale. I wonder how to enable thin functionality? Does anyone figured that out?
Install sven fuchs's i18n library from http://github.com/svenfuchs/i18n
Then, in your environment.rb :
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
using :"en-US" as a default locale:
I18n.default_locale = :"en-US"
I18n.fallbacks[:ca] # => [:ca, :"en-US", :en]
I18n.fallbacks :dk => [:"se-FI", :"fi-FI"] # => [:dk, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en]
In latest i18n gem (0.7.0) I have found it necessary to define fallback locales like this (in config/application.rb):
# Custom I18n fallbacks
config.after_initialize do
I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US")
end
You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.
This worked for me (i18n gem version 0.4x didn't work).
# config/environment.rb
config.gem 'i18n', :version => '0.3.7'
# config/initializers/i18n.rb
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
Related
In our Rails 4.2 app views, there is I18n error whenever there is no key found in zh-CN file:
translation missing: zh-CN.no key
Here is in local.rb under config/initializers/:
I18n.default_locale = 'zh-CN' if Rails.env.production? || Rails.env.development?
We would like to turn off this error and prevent it from showing on views. There is a post about the error for Rails 3. However the solutions are not working with Rails 4.2. Also config.i18n.fallbacks = false/true does not do the trick.
You must add both a fallback locale and a fallback language.
config.i18n.default_locale = :en
config.i18n.fallbacks = true
This should force I18n to fallback to english. I use it extensively on a Rails 4.1 app, I'm not sure if something changed over it for 4.2 though.
We can config I18n callbacks in Rails application. For example, when zh-CN translation missing, I18n will fallback to en. Configuration in my Rails 4.2.2 application like below:
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
config.i18n.default_locale = :"zh-CN"
config.i18n.fallbacks = true
config/initializers/i18n.rb
Rails.configuration.after_initialize do
I18n.fallbacks.map(:"zh-CN" => :en)
end
Reference doc:
https://github.com/svenfuchs/i18n/wiki/Fallbacks
http://web.archive.org/web/20151019133539/paulgoscicki.com/archives/2015/02/enabling-i18n-locale-fallbacks-in-rails/
I am working on a rails app (3.2.13) that is being translated into several languages, including (one of the 3 available flavours of) Norwegian. On public pages, the app uses the browser's language settings to set locale.
Most browsers offer 3 separate Norwegian shortcodes: no, nb & nn. The translations we have are in nb, but I think it would be best if no & nn also defaulted to nb. That way, if a user's browser language preferences were set to no then en, the app would try to supply nb Norwegian first, instead of skipping straight to English.
Is it possible to configure a list of "language aliases" for the i18n gem, something like this?
config.i18n.available_locales = [:sv, :en, :nb, :da, :fi]
config.i18n.aliased_locales = [:nb <= :no, :nb <= :nn]
Short answer
Take a look to the fallbacks
Create a file in initializers like i18n_fallbacks.rb
config.i18n.fallbacks = {:no => [:nb], :nn => [:nb]}
Here the reference
Things related
You can even set multiple fallbacks, and they will be taken in the same order as specified:
for instance:
config.i18n.default_locale = :de
config.i18n.fallbacks = {:de => [:en,:es]}
de.yml
:de:
greeting: Hallo
en.yml
:en:
foo: bar
es.yml
:es:
bar: baz
You get the following:
I18n.t :greeting # found in de.yml, no fallback
# => 'Hallo'
I18n.t :foo # not in :de, try in :en and found
# => "bar"
I18n.t :bar # not in :de, try in :en and in :es
# => "baz"
I81n.t :other # not found anywhere, note the message delivers not found for the current locale:
# => "translation missing: de.other"
In latest i18n gem (0.7.0) I have found it necessary to define fallback locales like this (in config/application.rb):
# Custom I18n fallbacks
config.after_initialize do
I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US")
end
You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.
When I run Rspec on my models, I get this warning:
[deprecated] I18n.enforce_available_locales will default to true in
the future. If you really want to skip validation of your locale you
can set I18n.enforce_available_locales = false to avoid this message.
I saw a similar question where the solution was to set config.i18n.enforce_available_locales or
I18n.config.enforce_available_locales in my config/application.rb file. I tried both, but I still get the warning.
The test that gives me the deprecation warning does not use any Rails except for ActiveModel. Instead of requiring the default spec_helper, I created my own spec_helper that does not involve any Rails at all. I also tried setting enforce_available_locales in my custom spec_helper, but I got an uninitialized constant error.
How do I get rid of the deprecation warning?
Edit:
Here's the exact code in my config/application.rb from one of my attempts with enforce_available_locales
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module Microblog
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
I18n.config.enforce_available_locales = true
end
end
There's also a bug reported on i18n in Github regarding this (svenfuchs/i18n#223) and it was said to be fixed in i18n gem version 0.6.9.
So the solution I think is to require '>= 0.6.9' in our Gemfile.
gem 'i18n', '>= 0.6.9'
and do a bundle update.
Then do as below:
config/application.rb
I18n.enforce_available_locales = true
# If you set the default_locale option you should do it after the previous line
# config.i18n.default_locale = :de
Ref: https://github.com/rails/rails/issues/13159
Hope it helps :)
What seems to work is adding these lines to my spec_helper:
require 'i18n'
I18n.config.enforce_available_locales = true
Because my tests don't use Rails, the Application class goes untouched, so enforce_available_locales must go in the spec_helper itself. The first line gets rid of the uninitialized constant error.
I have tried everything to make the i18n Fallback on Heroku but I couldn't.
I don't want to get the "translation missing messages". It work OK on developement mode.
Please help!
thanks
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.fallbacks.map(:es => [:en])
config.i18n.fallbacks = true
I have this in my application.rb file:
config.i18n.fallbacks = [:en]
and I just commented this code in production.rb:
config.i18n.fallbacks = true
and it's working ok for me in heroku.
In config/enviroments/production.rb
class Appname::Application.configure do
# 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
end
how to use rails i18n fallback features
I'm using Rails 3 with Globalize3 0.2.0.beta4
Ideally I need :fr to fallback to :en and vice versa.
There are cases when only a French translation is available and I need to show it even if the locale is :en.
I tried
config.i18n.fallbacks = { :fr => :en, :en => :fr }
but somewhat unsurprisingly it causes a stack level too deep error.
I'm changing my answer.
To enable fallbacks, add the following to your environment.rb file:
#support for locale fallbacks
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
Then, you can enable circular fallbacks like you were trying to, eg:
config.i18n.fallbacks = {'en' => 'fr', 'fr' => 'en'}
In this case, if something is missing in the en locale, it'll check the fr locale, and then the other way around. I don't get any errors running this.
Source: http://batsov.com/articles/2012/09/12/setting-up-fallback-locale-s-in-rails-3/
If you pass an array of locales they will be set as default fallbacks for all locales.
config.i18n.fallbacks = [:en, :fr]
Unfortunately, I haven't found a way to set up just two locales to fall back to each other.
In the end I monkey patched Globalize3. Not great as I have to update the patch whenever the site needs a new locale, but hey, it worked.
module Globalize
class << self
def fallbacks(locale = self.locale)
case locale
when :en then [:en, :fr]
when :fr then [:fr, :en]
end
end
end
end
This seems to have changed to this:
Globalize.fallbacks = {:en => [:en, :fr], :fr => [:fr, :en]}
Got from the official docs:
https://github.com/globalize/globalize#fallback-locales-to-each-other
In latest i18n gem (0.7.0) I have found it necessary to define fallback locales like this (in config/application.rb):
# Custom I18n fallbacks
config.after_initialize do
I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US")
end
You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.