FEATURE: Localization fallbacks (server-side)#2
Conversation
The FallbackLocaleList object tells I18n::Backend::Fallbacks what order the languages should be attempted in. Because of the translate_accelerator patch, the SiteSetting.default_locale is *not* guaranteed to be fully loaded after the server starts, so a call to ensure_loaded! is added after the locale is set for the current user. The declarations of config.i18n.fallbacks = true in the environment files were actually garbage, because the I18n.default_locale was SiteSetting.default_locale, so there was nothing to fall back to. *derp*
|
@CodeAnt-AI: review |
|
CodeAnt AI is running the review. |
Nitpicks 🔍
|
| [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact | ||
| end | ||
|
|
||
| def ensure_loaded! | ||
| self[I18n.locale].each { |l| I18n.ensure_loaded! l } |
There was a problem hiding this comment.
Suggestion: Fallback translations are only preloaded when the controller explicitly calls I18n.fallbacks.ensure_loaded!, so in other contexts (e.g. background jobs, rake tasks, or code using I18n.with_locale with a different locale) fallback locales' YAML files are never loaded, causing valid fallback translations to be missed; by loading the fallback locales whenever the fallback list is computed, fallback behavior will work consistently everywhere. [logic error]
Severity Level: Major ⚠️
- ⚠️ Background jobs using I18n miss translations from fallback locales.
- ⚠️ Services using I18n.with_locale bypass controller fallback preloading.
- ⚠️ Inconsistent translations between web and non-controller code.| [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact | |
| end | |
| def ensure_loaded! | |
| self[I18n.locale].each { |l| I18n.ensure_loaded! l } | |
| locales = [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact | |
| locales.each { |l| I18n.ensure_loaded!(l) } | |
| locales | |
| end | |
| def ensure_loaded!(locale = I18n.locale) | |
| self[locale] |
Steps of Reproduction ✅
1. Note the fallback configuration in `config/initializers/i18n.rb:12-23`:
`FallbackLocaleList#[]` only returns `[locale, SiteSetting.default_locale.to_sym,
:en].uniq.compact` without calling `I18n.ensure_loaded!` on any of these locales, and
`ensure_loaded!` explicitly loads fallbacks only for `I18n.locale`.
2. Observe that `ApplicationController#set_locale` in
`app/controllers/application_controller.rb:152-160` is the only place calling
`I18n.fallbacks.ensure_loaded!`, so fallback locales are proactively loaded only for web
requests that pass through this controller filter.
3. Check the lazy-loading implementation in
`lib/freedom_patches/translate_accelerator.rb:45-65`: `I18n.ensure_loaded!(locale)` and
`load_locale` load translations on demand per locale and track them in `@loaded_locales`,
but this is only invoked when some code explicitly calls `I18n.ensure_loaded!(locale)` (or
`I18n.translate` for the current locale), not automatically for fallback locales.
4. In any non-controller context that uses I18n with fallbacks—for example, service/lib
code such as `PostDestroyer.mark_for_deletion` in `lib/post_destroyer.rb:100-109` wrapped
in `I18n.with_locale(SiteSetting.default_locale)` or any background job that calls
`I18n.t` without going through
`ApplicationController#set_locale`—`I18n::Backend::Fallbacks` will consult
`I18n.fallbacks[locale]`, which currently just returns the list of fallback locales
without loading their YAML files; if a translation key exists only in a fallback locale
(e.g., in `:en` but not in the current locale), lookup will fail because that fallback
locale's files were never loaded in this process. The proposed change makes
`FallbackLocaleList#[]` load each locale via `I18n.ensure_loaded!` whenever the fallback
list is computed, so fallback behavior works consistently even in these non-controller
contexts.Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** config/initializers/i18n.rb
**Line:** 17:21
**Comment:**
*Logic Error: Fallback translations are only preloaded when the controller explicitly calls `I18n.fallbacks.ensure_loaded!`, so in other contexts (e.g. background jobs, rake tasks, or code using `I18n.with_locale` with a different locale) fallback locales' YAML files are never loaded, causing valid fallback translations to be missed; by loading the fallback locales whenever the fallback list is computed, fallback behavior will work consistently everywhere.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.|
CodeAnt AI finished running the review. |
User description
Test 9
Summary by CodeRabbit
Release Notes
Refactor
Chore
✏️ Tip: You can customize this high-level summary in your review settings.
Replicated from ai-code-review-evaluation/discourse-coderabbit#9
CodeAnt-AI Description
Server-side localization fallbacks with on-demand locale loading
What Changed
Impact
✅ Fewer missing translations in the UI✅ Consistent fallback to site default then English when a translation is absent✅ Fewer startup-related locale errors💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.