ruby-i18n / ruby-i18n/i18n

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

Open
#293 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pr-required
Dominant language
Ruby
Stars
1k
Forks
415
Avg merge
2d 3h
Merged PRs (30d)
1

Description

# 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.%')

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at I18n::Backend::Fallbacks and I18n::Backend::ActiveRecord, using the initializer and Rails console example in the issue to reproduce the lookup. Verify that each default key is tried across the configured fallback locales, with the fallback queries visible in the generated SQL.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend, internationalization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.