heroku I18n locale is different from default_locale, works fine locally - ruby-on-rails

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.)

Related

How to configure locale aliases using i18n & 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.

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

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.

Is it possible to make rails i18n locales fallback to each other?

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.

I18n using wrong locale in emails, why?

I'm using Rails 2.3.6 and I18n. Everything works fine except that I18n is using the wrong locale when sending emails. In fact it renders the following code using en.yml instead of it.yml, although my default locale is it.
Can you help me?
class UserMailer < ActionMailer::Base
default_url_options[:host] = GAME_CONFIG["domain"]
def password_reset_instructions(user)
subject I18n.translate("email_messages.lost_password.subject")
from "#{GAME_CONFIG["name"]} <no-reply##{GAME_CONFIG["domain"]}>"
recipients user.email
sent_on Time.now
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
end
end
In my production server if I try to check for the current locale I get:
I18n.default_locale
:it
I18n.locale
:it
I discovered that this is normal behavior and I can fix this specifing :locale => ":it"

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