Skip to content

Use I18n.locale as the only source of truth#71930

Merged
artem-vavilov merged 9 commits into
stagingfrom
i18n-locale-fix
Apr 9, 2026
Merged

Use I18n.locale as the only source of truth#71930
artem-vavilov merged 9 commits into
stagingfrom
i18n-locale-fix

Conversation

@artem-vavilov

@artem-vavilov artem-vavilov commented Apr 6, 2026

Copy link
Copy Markdown
Member

Previously, in many places we used request.locale as if it were the application's actual locale. That is misleading because request.locale represents only the requested or inferred locale for the current request, not necessarily the final locale the app renders with.

The real source of truth for rendering is I18n.locale, since that is what the I18n backend actually uses to resolve translations and locale dependent behavior. When we rely on request.locale in controllers, views, helpers, or other app logic, we risk using a locale value that does not match the one ultimately used for rendering.

This change makes locale handling more consistent by using I18n.locale for locale dependent application behavior and treating request.locale only as the request level input used to determine that final locale.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8ff72772ee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread dashboard/app/controllers/application_controller.rb Outdated
Comment thread dashboard/legacy/middleware/files_api.rb Outdated
@artem-vavilov artem-vavilov changed the title refactor(i18n): make I18n.locale the only source of truth Use I18n.locale as the only source of truth Apr 6, 2026
@artem-vavilov artem-vavilov requested review from a team and wilkie April 6, 2026 15:04

@Hamms Hamms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great cleanup, thank you! I do have a couple of questions, but the only one I'm actually concerned about is the js_locale removal; everything else is just curiosity.

Speaking of curiosity, do you happen to know how Rails determines i18n.locale internally? I'm specifically interested to know more about precisely in what situation it deviates from value we end up with in request.locale, given all the work we do in the middleware that sets it.

@@ -1,3 +1,2 @@
- js_locale = request.locale.to_s.downcase.tr('-', '_')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why don't we need this anymore? Looks to me like it's still being referenced in the webpack_asset_path call just below

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We already have a helper method with the same name:

# String representing the Locale code for the Blockly client code.
def js_locale(locale_code = locale)
Cdo::I18n.js_locale(locale_code)
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great!


protected def with_locale(&block)
I18n.with_locale(locale, &block)
I18n.with_locale(request.locale, &block)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why don't we want the change to locale_helper.rb to apply here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure I fully understand the question. Could you please clarify what you mean?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we want this to continue to use request.locale, rather than I18n.locale?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

request.locale determines which locale should be used for the current request based on the language cookie and, soon, also on the browser language. We then need to pass that resolved locale to the I18n backend so it can render the site using the correct locale. The next step would be to assign the language cookie value or browser language value directly through I18n.with_locale and remove request.locale entirely

Comment thread dashboard/config/initializers/devise.rb Outdated
# end
require 'devise/custom_failure'
config.warden do |manager|
manager.failure_app = Devise::CustomFailure

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I love that we can finally remove this! I would slightly prefer to see and the devise gem update bundled together in their own PR, but that's not a blocking concern

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@artem-vavilov artem-vavilov merged commit 38fb7c2 into staging Apr 9, 2026
8 checks passed
@artem-vavilov artem-vavilov deleted the i18n-locale-fix branch April 9, 2026 01:16
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

Successfully merging this pull request may close these issues.

2 participants