i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Fallbacks for an array of symbolized keys given as default to I18n.t are not retrieved from backend, only the initial key

Open debreczeni opened this issue 11 years ago • 1 comments

# Gemfile
# gem 'rails' etc...
gem 'devise'

# app/models/user.rb
class User < ActiveRecord::Base
  devise database_authenticatable, :registerable, :recoverable, :validatable
end

# config/locales/devise.en.yml
en:
  errors:
    messages:
      not_found: "not found"

# config/initializers/i18n.rb
require 'i18n/backend/active_record'
require "i18n/backend/fallbacks"

I18n.default_locale = :'en-US'
I18n.available_locales = [I18n.default_locale]
I18n.fallbacks = true
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Fallbacks)
I18n.backend = I18n::Backend::ActiveRecord.new

# in rails console
User.new.errors.add :email, :not_found

# Generates the following SQL queries in the log file
   (0.3ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('activerecord.errors.models.user.attributes.email.not_found') OR "key" LIKE 'activerecord.errors.models.user.attributes.email.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('activerecord.errors.models.user.not_found') OR "key" LIKE 'activerecord.errors.models.user.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('activerecord.errors.messages.not_found') OR "key" LIKE 'activerecord.errors.messages.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('errors.attributes.email.not_found') OR "key" LIKE 'errors.attributes.email.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('errors.messages.not_found') OR "key" LIKE 'errors.messages.not_found.%')
   (0.4ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en' AND ("key" IN ('activerecord.errors.models.user.attributes.email.not_found') OR "key" LIKE 'activerecord.errors.models.user.attributes.email.not_found.%')

# Console returns with output
[ "translation missing: en-US.activerecord.errors.models.user.attributes.email.not_found" ]

I think it should query all I18n keys given as defaults on the fallback locales too, not just the original, most significant key of the error message.

Whether the fallback queries should happen between each query for a default, or after all default keys have been tried for the original locale then the fallbacks, I think it should try the fallback for each fallback locale for the most significant key, then gradually going the least significant default key symbol and trying the fallback locales on the way.

So the queries should look something like this instead of the above:

# Generates the following SQL queries in the log file
   (0.3ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('activerecord.errors.models.user.attributes.email.not_found') OR "key" LIKE 'activerecord.errors.models.user.attributes.email.not_found.%')
   (0.3ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en' AND ("key" IN ('activerecord.errors.models.user.attributes.email.not_found') OR "key" LIKE 'activerecord.errors.models.user.attributes.email.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('activerecord.errors.models.user.not_found') OR "key" LIKE 'activerecord.errors.models.user.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en' AND ("key" IN ('activerecord.errors.models.user.not_found') OR "key" LIKE 'activerecord.errors.models.user.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('activerecord.errors.messages.not_found') OR "key" LIKE 'activerecord.errors.messages.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en' AND ("key" IN ('activerecord.errors.messages.not_found') OR "key" LIKE 'activerecord.errors.messages.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('errors.attributes.email.not_found') OR "key" LIKE 'errors.attributes.email.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en' AND ("key" IN ('errors.attributes.email.not_found') OR "key" LIKE 'errors.attributes.email.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en-US' AND ("key" IN ('errors.messages.not_found') OR "key" LIKE 'errors.messages.not_found.%')
   (0.2ms)  SELECT COUNT(*) FROM "translations"  WHERE "translations"."locale" = 'en' AND ("key" IN ('errors.messages.not_found') OR "key" LIKE 'errors.messages.not_found.%')

debreczeni avatar Oct 17 '14 15:10 debreczeni

Hi there,

Please submit a PR to fix this issue.

Thanks!

radar avatar Nov 20 '16 23:11 radar