Heroku: to_datetime does not take into account the rails' i18n.locale - ruby-on-rails

Edit: (to be clear and to the point)
I want the following to work on heroku: "11/13/2011".to_datetime, meaning I need the dates to be in english format. Thanks
I've set this the i18n locale in my application.rb
config.i18n.default_locale = :en
config.i18n.locale = :en
In the heroku console:
I18n.locale
=> :en
So it seems like it's set correctly.
But when I do:
"11/13/2011".to_datetime
=> ArgumentError: invalid date
Noooo!
Do you have any idea of what I'm doing wrong here?
Thanks.

The i18n config had nothing to do with my problem.
Just a ruby version "problem".
I was working with ruby 1.8.7 in my local env, and heroku was at 1.9.x.
Rails 1.9 assumes EU format, 1.8.7 does not.

Related

I18n.t falls back to key instead to default locale

In my frontend non-translated strings are replaced with word form of their key instead of the translation of base language.
That means that changing locale to :en or :tr both shows Summaries Header for t('activerecord.misc.badge.summaries_header') instead of Auswertungen from German locale. So it's obviously build from the last part of the key. Summaries Header does not appear as Label in any locale.
Other in :en/:tr translated strings are shown correctly.
# config/application.rb
config.i18n.default_locale = :de
config.i18n.available_locales = [
:de, # Deutsch
:pt, # Portugiesisch
:en, # Englisch
:tr, # Türkisch
]
config.i18n.fallbacks = true
# Versions
ruby 2.7.2
rails 5.2.4.4
i18n 1.8.6
rails-i18n 5.1.3
I can't find the reason for that behavior and I can't figure out which update at which time broke this.
How can I fix this?
TL;DR: Update to i18n v1.8.7 or higher with bundle update i18n
i18n v1.8.6 was broken regarding getting fallbacks from config.i18n.* into Thread variables.
See i18n issue #546 and changelog for v1.8.7

Rails 4.2 - how to turn off I18n error `translation missing` on views

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/

How to set a cn locale to rails app?

I trying to add a chinese language.I was used gem rails i18n, and when I make that
I18n.locale = :cn
console show me an error
I18n::InvalidLocale: :cn is not a valid locale
or that
I18n.locale = :"zh-CN"
locale do not working
how fix it?
You need to change the following line to false in application.rb
config.i18n.enforce_available_locales = false

heroku I18n locale is different from default_locale, works fine locally

So, everything works fine locally, but at heroku it changes to :en
Fire up console on both environments:
Heroku:
heroku console --app myapp
Local:
rails c
Then play around
Heroku: > I18n.l Time.now
=> "Tue, 01 Mar 2011 06:43:58 -0800"
Local: > I18n.l Time.now
=> "tirsdag, 1. mars 2011, 15:43"
Heroku: > I18n.default_locale
=> :nb
Local: > I18n.default_locale
=> :nb
#after a lot more trial and error, I find this:
Heroku: > I18n.locale
=> :en
Local: > I18n.locale
=> :nb
Just doing I18n.locale = :nb in the console fixed the problem ATM, and when I refresh in different browsers, it works ok. But on redeploy, it's back to :en.
Do I have to set locale in initilizer too? I'm confused.
FYI: I don't programatically set I18n.locale anywhere. staging.rb is plain.
It helped setting locale directly:
config.i18n.default_locale = :nb
#Adding the below makes it work as expected at heroku
config.i18n.locale = :nb
What version of Ruby on Rails are you using? I'm not positive about 2.x, but in Rails 3 you can set the default locale in config/application.rb using config.i18n.default_locale = :en. (This is the line, commented out by default.)

Heroku and i18n Problems

I'm having trouble getting i18n to work on heroku.
I set:
I18n.default_locale = :de
in my environment.rb
and the translation is in config/locales/de.yml
works perfect on my local machines but not so on Heroku.
On heroku everything is in english.
I don't think I need a special gem like i18n gem, cause I don't have it on my local machine either.
Maybe someone has a solution to this?
Try to set the default local like this in your config.environment.rb:
Rails::Initializer.run do |config|
# ...
config.i18n.default_locale = :de
end
The only solution I found that worked for me, on heroku, was setting it manually in the application controller.
application_controller.rb
before_filter :set_locale
def set_locale
I18n.locale = 'fr-QC' || I18n.default_locale
end
cheers

Resources