I'm trying to change the locale that my rails (4.1.4) application is using, but every time I try to change it in the console using commands such as
I18n.locale = :es
I18n.default_locale = :de
an I18n::InvalidLocale error is brought up. This is the case for every locale I test, including regional ones such as :en-US. This is true even if I have a *.yml file set up for that locale. The only one that works is :en, which is the default for me.
I feel like this should be something pretty basic as all the resources I find seem to skip past this as a given, but it's not working for me and I can't figure out where to look for the problem source.
Did you add the new locale in application.rb ?
config.i18n.enforce_available_locales = false
config.i18n.available_locales = [:en, :es, :de]
config.i18n.default_locale = :de
You also have to create the files en.yml , es.yml and de.yml in your config/locale folder.
Thanks! When I looked in that file, I realized that in addition to the answer you gave, Rails provides it's own solution commented out -
# 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]
So basically it takes a translation file such as pt-br.yml and adds that to the list of valid locales.
Make sure you have added es.yml and de.yml to the config/locales folder.
Related
I going to uncomment before action on config/initializers/active_admin.rb
# config.before_action :do_something_awesome
But when I activate this default comment out, test failed for I18n.locale changing.
I'm using :ja but it looks change to en
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
config.i18n.default_locale = :ja
test failed like this.
"XXX"=>"translation missing: en.xxx.yyy"
development environment looks like is not affected.
I don't understand why even if just uncomment below line.
config.before_action :do_something_awesome
Please help me if you know anything that cloud help me.
Environment Information
Rails 5.2.0
ActiveAdmin 1.3.0
I am trying to add a US region specific locale to rails, however tells me that :en_locale is not a valid code for a locale. How can I include this specific region?
In app/config/locales, I have en.yml and en-US.yml.
My application.rb, I have the following:
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
config.i18n.available_locales = [:en, :'en-US']
config.i18n.default_locale = :en
config.i18n.fallbacks = [:en]
# config.i18n.fallbacks = {:'zh-MY' => :'zh-CN'} # If we want to designate a lang to fallback to
Error Message:
I18n::InvalidLocale - :en_us is not a valid locale:
i18n (0.6.9) lib/i18n.rb:288:in `enforce_available_locales!'
i18n (0.6.9) lib/i18n/config.rb:11:in `locale='
actionview (4.1.14.2) lib/action_view/lookup_context.rb:239:in `locale='
actionview (4.1.14.2) lib/action_view/rendering.rb:19:in `locale='
i18n (0.6.9) lib/i18n.rb:35:in `locale='
The i18n library takes a pragmatic approach to locale keys (after some
discussion), including only the locale ("language") part, like :en,
:pl, not the region part, like :en-US or :en-GB, which are
traditionally used for separating "languages" and "regional setting"
or "dialects". Many international applications use only the "language"
element of a locale such as :cs, :th or :es (for Czech, Thai and
Spanish). However, there are also regional differences within
different language groups that may be important. For instance, in the
:en-US locale you would have $ as a currency symbol, while in :en-GB,
you would have £. Nothing stops you from separating regional and other
settings in this way: you just have to provide full "English - United
Kingdom" locale in a :en-GB dictionary. Few gems such as Globalize3
may help you implement it.
~Rails Guides
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 need to set the default locale to spanish on my application.
These are the lines of my file application.rb:
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('config', 'locales', '**', '*.{rb,yml}')]
Rails.application.config.i18n.default_locale = :es
end
But, when I generate the scaffold the inflection is wrong. It´s english locale.
rails g scaffold Preparado nombre:string
I get the wrong plural: Preparadoes, It must be: Preparados.
On the console I tried this:
irb(main):015:0> I18n.locale = 'es'
=> "es"
irb(main):016:0> 'preparado'.pluralize()
=> "preparadoes"
irb(main):017:0> 'preparado'.pluralize(:es)
=> "preparados"
irb(main):018:0>
My gemFile contains:
gem 'inflections'
In summary, I need to create an scaffold with the correct pluralization.
Sorry by my english. I know it ´s not the best.
Thanks.
i18n does not affect pluralization here, rails uses inflections for it, and there is only one method I know to customize it:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'preparado', 'preparados'
end
to config/initializers/inflections.rb
There is Spanish locale for #pluralize, but you need to set it explicitly every time you call it. And generator calls it without parameters.
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.