[rails] devise-in18n messages not localized

142 views
Skip to first unread message

Cédric Lefebvre

unread,
Dec 26, 2019, 6:17:23 PM12/26/19
to Ruby on Rails: Talk
I have deployed devise & devise-i18n to internationalize devise. Everything works well - including all the i18n - except that flash messages generated by devise do not get translated

=> messages generated by devise and accessed via resource.errors.full_messages are localized
e.g. try to "sign_up" with no information filled in


=> flashes generated by devise are not localized
e.g. try to "sign_in" with no information filled in


Any idea why? Any idea on how to fix this?

fugee ohu

unread,
Dec 28, 2019, 8:18:21 AM12/28/19
to Ruby on Rails: Talk
Have you created settings for i18n in application.rb ?

Here's what application.rb looks like on one of my sites that uses locales:
    Rails.application.config.i18n.available_locales = ["ko", "zh-TW", "ja", "en-US"]
    Rails.application.config.i18n.default_locale = "en-US"
    ISO3166.configure do |config|
       config.locales = ['zh-TW', 'en-US', 'ko', 'ja']

Cédric Lefebvre

unread,
Dec 28, 2019, 12:30:31 PM12/28/19
to rubyonra...@googlegroups.com
Sure, my application.rb contains the following:

    config.i18n.available_locales = [:en, :fr]
    config.i18n.default_locale = :en
    config.i18n.fallbacks = [I18n.default_locale]

The last line of your application.rb comes from heroes Gem, which I do not use.

On your website, are you sure the flash messages are localised? And if yes, which version of the devise and devise-i18n are you using?

Thanks

Cedric
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/526f03ab-5181-45e1-9a8d-622e1ec17fb6%40googlegroups.com.

Ariel Juodziukynas

unread,
Dec 28, 2019, 1:19:00 PM12/28/19
to rubyonra...@googlegroups.com

You can do something like this on an initializer so override the I18n lookup method to print each key it tries to find, maybe you can debug what's going on:

# config/initializers/debug_18n.rb

module I18n
module Backend
class Simple
# Monkey-patch-in localization debugging
# Enable with ENV['I18N_DEBUG']=1 on the command line in server startup, or ./config/environments/*.rb file.
#
def lookup(locale, key, scope = [], options = {})
init_translations unless initialized?
keys = I18n.normalize_keys(locale, key, scope, options[:separator])

puts "I18N keys: #{keys}"

keys.inject(translations) do |result, _key|
_key = _key.to_sym
return nil unless result.is_a?(Hash) && result.has_key?(_key)
result = result[_key]
result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)

puts "\t\t => " + result.to_s + "\n" if (result.class == String)

result
end
end
end
end
end

I don't rember where I took that code from to give the credit.

Now you can check that's actually trying to translate (from the code it's: some_resource_name.some_message)

Cédric Lefebvre

unread,
Dec 28, 2019, 5:25:18 PM12/28/19
to rubyonra...@googlegroups.com
Thank you very much for your message. I discovered actually that someone had developed an i18n debug Gem that is very helpful to troubleshoot this.

I discovered actually that some keys where missing and that for some reason i18n was first relying on the fallback locale instead of looking in the translation file.
Filling in the missing locale entries allowed me to fix this.

Cedric
Reply all
Reply to author
Forward
0 new messages