Skip to content
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

Support falling back to 2 letter language code locales and fix locale leaks across requests #1711

Merged
merged 3 commits into from
Aug 22, 2023

Conversation

nickpith
Copy link
Contributor

@nickpith nickpith commented Aug 11, 2023

What this PR does

In the ShopifyApp::Localization concern, an app must specify the available locales exactly the way they expect them to be requested. If an app specifies that only the 2 letter language codes (i.e. es) are available, it won't get I18n.locale set to that language if given fully qualified locale (i.e. es-ES).

Also, Rails does not recommend using I18n.locale = to set the locale for controllers because it can leak in multi-threaded applications across requests. See https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests. Instead, I18n.with_locale should be used with an around_action.

Fixes #635.

Reviewer's guide to testing

Unit tests verify this change.

Things to focus on

Is there any reason why we would not want to fallback to the 2 letter language code locales?

  • It seems better than getting the default locale.

Checklist

Before submitting the PR, please consider if any of the following are needed:

  • Update CHANGELOG.md if the changes would impact users
  • Update README.md, if appropriate.
  • Update any relevant pages in /docs, if necessary
  • For security fixes, the Disclosure Policy must be followed.

@nickpith nickpith self-assigned this Aug 11, 2023
@nickpith nickpith changed the title Support falling back to 2 letter language code locales Support falling back to 2 letter language code locales and fix locale leaks across requests Aug 16, 2023
@nickpith nickpith force-pushed the fallback_to_2_letter_language_code branch from 0d5d653 to 881e76b Compare August 16, 2023 17:43
Copy link
Contributor

@mllemango mllemango left a comment

Choose a reason for hiding this comment

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

thank you!

In the `ShopifyApp::Localization` concern, an app must specify the available locales exactly the way they expect them to be requested. If an app specifies that only the 2 letter language codes (i.e. `es`) are available, it won't get `I18n.locale` set to that language if given fully qualified locale (i.e. `es-ES`).
Rails does not recommend using `I18n.locale =` to set the locale for controllers because it can leak in multi-threaded applications across requests. See https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests. Instead, `I18n.with_locale` should be used with an `around_action`.

Fixes #635.
@nickpith nickpith force-pushed the fallback_to_2_letter_language_code branch from 881e76b to 93a96a7 Compare August 22, 2023 19:43
@nickpith nickpith merged commit f8b4685 into main Aug 22, 2023
9 checks passed
@nickpith nickpith deleted the fallback_to_2_letter_language_code branch August 22, 2023 21:25
@shopify-shipit shopify-shipit bot temporarily deployed to rubygems October 12, 2023 14:27 Inactive
if params[:locale]
session[:locale] = params[:locale]
else
session[:locale] ||= I18n.default_locale
Copy link
Contributor

Choose a reason for hiding this comment

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

@nickpith

Noticed this being removed means the locale is not stored in the session.

Single page apps like (Hotwire-turbo) do not include the locale param on in-app page navigation. The locale is only included when the shopify admin page is reloaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it's been too long since this change but the session[:locale] is still being set down on line 21?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep - it is being set there.

Think the issue is more with single page apps like react / Hotwire-turbo that don't reload the Shopify iframe for embedded apps. Meaning the locale is not included after the initial page load. The session is overridden on the next page request - set to default.

Previous version was only setting when provided via params, and only setting the session[:locale] if it was not already set via ||=

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I see that now. Thanks for pointing that out.

That definitely was not intended. If you want to fix that, go ahead and put up a PR and I'll gladly review. Otherwise, I'll get a fix out tomorrow if that is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI - I've fixed the issue and opening a PR shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix is up at #1878

nickpith added a commit that referenced this pull request Jul 10, 2024
The changes in #1711 introduced a bug where single-page apps that don't include the locale param in subsequent requests were unable to properly set the locale that was persisted in the user session. Instead they would use the default locale.

See https://github.com/Shopify/shopify_app/pull/1711/files#r1664570299 for background.
nickpith added a commit that referenced this pull request Jul 10, 2024
The changes in #1711 introduced a bug where single-page apps that don't include the locale param in subsequent requests were unable to properly set the locale that was persisted in the user session. Instead they would use the default locale.

See https://github.com/Shopify/shopify_app/pull/1711/files#r1664570299 for background.
nickpith added a commit that referenced this pull request Jul 11, 2024
The changes in #1711 introduced a bug where single-page apps that don't include the locale param in subsequent requests were unable to properly set the locale that was persisted in the user session. Instead they would use the default locale.

See https://github.com/Shopify/shopify_app/pull/1711/files#r1664570299 for background.
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.

Warn people that this gem is changing the locale in the background
4 participants