Skip to content

Cannot read anything other than en in I18n locale #263

@tmimura39

Description

@tmimura39

Assumption

  • Without config.eager_load = true ( I18n.eager_load! don't run ).
  • config.i18n.default_locale = :ja ( Locale other than en )

Steps to reproduce

  1. Access the Mission Controle Jobs dashboard (/jobs). I18n initial read is performed.
  2. Run MyApplicationCode. For example, I18n.l(Time.current)
  3. Error occurred Translation missing: ja.time.formats.default (I18n::MissingTranslationData)

Smaller reproduction code

(dev)> I18n.available_locales
=> [:ja, :en]
(dev)> I18n.backend.translations.keys
=> []
(dev)> I18n.with(config: MissionControl::Jobs::I18nConfig.new) { I18n.with_locale(:en) { I18n.l(Time.current) } }
=> "Fri, 23 May 2025 01:01:47 +0900"
(dev)> I18n.backend.translations.keys
=> [:en]
(dev)> I18n.l(Time.current)
(core):5:in '<main>': Translation missing: ja.time.formats.default (I18n::MissingTranslationData)

        raise exception.respond_to?(:to_exception) ? exception.to_exception : exception
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I think it is a bug related to ca09138.

First, I18n file reading is performed only once.
https://github.com/ruby-i18n/i18n/blob/4dddd855039b0c5a0b5b3b2df69783374058a7c9/lib/i18n/backend/simple.rb#L28-L30

Next, non-valid locale files are ignored.
https://github.com/ruby-i18n/i18n/blob/4dddd855039b0c5a0b5b3b2df69783374058a7c9/lib/i18n/backend/simple.rb#L37-L40

In other words, if the first read is performed with available_locales_set set to only en, non-en locales files will be ignored.
And no further reading of locale files will be performed.

Workaround

Run I18n.eager_load! before available_locales_set is updated.

# config/initializers/i18n.rb
Rails.application.config.after_initialize { I18n.eager_load! }

# or config.eager_load = true
(dev)> I18n.available_locales
=> [:ja, :en]
(dev)> I18n.backend.translations.keys
=> []
(dev)> I18n.eager_load!
=> true
(dev)> I18n.backend.translations.keys
=> [:en, :ja]
(dev)> I18n.with(config: MissionControl::Jobs::I18nConfig.new) { I18n.with_locale(:en) { I18n.l(Time.current) } }
=> "Fri, 23 May 2025 01:02:44 +0900"
(dev)> I18n.backend.translations.keys
=> [:en, :ja]
(dev)> I18n.l(Time.current)
=> "2025年05月23日(金) 01時02分52秒 +0900"```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions