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*
Codoki PR ReviewSummary: Centralize I18n fallbacks, ensure lazy-loading Issues (Medium)
Showing up to 2 medium issue(s). See inline suggestions for more details. Key Feedback (click to expand)
Confidence: 4/5 — Looks good; minor fixes (2 medium) Sequence DiagramsequenceDiagram
participant Controller
participant I18nFallbacks as I18n.fallbacks
participant I18n
Controller->>I18nFallbacks: ensure_loaded!()
I18nFallbacks->>I18n: ensure_loaded!(locale)
loop each fallback locale
I18n->>I18n: load_locale(locale)
end
React with 👍 or 👎 if you found this review useful. |
| # user locale, site locale, english | ||
| # TODO - this can be extended to be per-language for a better user experience | ||
| # (e.g. fallback zh_TW to zh_CN / vice versa) | ||
| [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact |
There was a problem hiding this comment.
🔷 Medium: Calling to_sym on a nil default locale will raise NoMethodError (e.g., during boot or misconfiguration), preventing fallbacks from resolving. Guard the conversion to keep this path safe.
| [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact | |
| [locale, (SiteSetting.default_locale && SiteSetting.default_locale.to_sym), :en].compact.uniq |
| [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact | ||
| end | ||
|
|
||
| def ensure_loaded! |
There was a problem hiding this comment.
🔷 Medium: ensure_loaded! depends on the global I18n.locale; if called before I18n.locale is set in set_locale, it may preload fallbacks for the previous locale and miss the intended one for the current request. Accept an optional locale to remove ordering dependence.
| def ensure_loaded! | |
| def ensure_loaded!(locale = I18n.locale) | |
| self[locale].each { |l| I18n.ensure_loaded! l } | |
| end |
No description provided.