Skip to content

Cannot read anything other than en in I18n locale #263

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

Open
tmimura39 opened this issue May 22, 2025 · 0 comments
Open

Cannot read anything other than en in I18n locale #263

tmimura39 opened this issue May 22, 2025 · 0 comments

Comments

@tmimura39
Copy link

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"```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant