Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
debreczeni opened this issue Oct 17, 2014 · 1 comment
Milestone

Comments

@debreczeni
Copy link

# 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 debreczeni changed the title Fallbacks for an array of symbolized keys as defaults are not retrieved from backend, only the initial key Fallbacks for an array of symbolized keys given as default to I18n.t are not retrieved from backend, only the initial key Oct 17, 2014
@radar radar self-assigned this Nov 15, 2016
@radar radar added this to the 0.8.0 milestone Nov 15, 2016
@radar radar removed their assignment Nov 20, 2016
@radar radar modified the milestones: 0.9.0, 0.8.0 Nov 20, 2016
@radar
Copy link
Collaborator

radar commented Nov 20, 2016

Hi there,

Please submit a PR to fix this issue.

Thanks!

@radar radar modified the milestones: 0.9.0, 0.10.0 Oct 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants