Skip to content

FEATURE: Localization fallbacks (server-side)#2

Open
ShashankFC wants to merge 1 commit into
localization-system-prefrom
localization-system-post
Open

FEATURE: Localization fallbacks (server-side)#2
ShashankFC wants to merge 1 commit into
localization-system-prefrom
localization-system-post

Conversation

@ShashankFC

@ShashankFC ShashankFC commented Feb 13, 2026

Copy link
Copy Markdown

User description

Test 9

Summary by CodeRabbit

Release Notes

  • Refactor

    • Restructured internationalization fallback system with optimized on-demand locale loading for improved efficiency.
    • Enhanced locale fallback resolution to intelligently cascade through requested language, site default, and English.
    • Consolidated locale initialization logic for better maintainability.
  • Chore

    • Removed deprecated pluralization backend integration from environment configurations.

✏️ 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

  • Adds a custom fallback order so the app shows translations from the user's locale, then the site default, then English when a string is missing
  • Loads pluralization and fallback support into the translation backend so plural forms and fallbacks are applied consistently
  • Loads locale files on demand and ensures the current user's locale is loaded when setting the locale, reducing missing translation lookups after startup
  • Removes redundant environment-level i18n.fallbacks flags that previously had no effect

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:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

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:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

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.

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*
@ShashankFC

Copy link
Copy Markdown
Author

@CodeAnt-AI: review

@codeant-ai

codeant-ai Bot commented Feb 13, 2026

Copy link
Copy Markdown

CodeAnt AI is running the review.

@codeant-ai codeant-ai Bot added the size:M This PR changes 30-99 lines, ignoring generated files label Feb 13, 2026
@codeant-ai

codeant-ai Bot commented Feb 13, 2026

Copy link
Copy Markdown

Nitpicks 🔍

🔒 No security issues identified
⚡ Recommended areas for review

  • Possible Nil Error
    The code calls SiteSetting.default_locale.to_sym without guarding for a nil/blank default_locale. If SiteSetting.default_locale is nil (or not set) this will raise a NoMethodError at runtime. Convert safely before calling to_sym or guard the value.

  • Inconsistent API Usage
    An ensure_loaded!(locale) helper was added but existing code still calls load_locale(...) unless @loaded_locales.include?(...). Prefer using the new helper everywhere to centralize initialization (it also ensures @loaded_locales is initialized). Also verify thread-safety expectations remain consistent.

  • Potential Performance Impact
    I18n.fallbacks.ensure_loaded! is invoked on every request in set_locale. That triggers loading of fallback locales (which may load translation files if not already loaded). Even though loading is guarded, this call could still add request overhead; consider limiting when it's necessary or ensure it's effectively no-op after initial load.

Comment on lines +17 to +21
[locale, SiteSetting.default_locale.to_sym, :en].uniq.compact
end

def ensure_loaded!
self[I18n.locale].each { |l| I18n.ensure_loaded! l }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Suggested change
[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

codeant-ai Bot commented Feb 13, 2026

Copy link
Copy Markdown

CodeAnt AI finished running the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants