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

i18n: add better support for the default locale in bundles #13211

Merged
merged 3 commits into from
Oct 14, 2021

Conversation

brendankenny
Copy link
Member

@brendankenny brendankenny commented Oct 13, 2021

Recently we've been circling around a few different ways to strip the locale data out of bundles, each slightly different, so it seemed like a good time to settle on one (this also makes the brfs replacement easier).

This PR sets us up so we can always replace locales.js with {}. Everything works without reaching too deep and/or custom shimming. There are no messages available for localization unless the client using the bundle brings them, either via registerLocaleData when running lighthouse-core or via rendererFormattedStrings equivalents in the report. Viewer locale swapping already works like this and it's working well :)

changes in format.js/i18n.js:

  • Lighthouse core works even with an empty {} for our locales because the default fallbacks (provided by each file's UIStrings) are always available, however there are a few places where LOCALE_MESSAGES[requestedLocale] is checked for truthiness, which would fail with this approach. Added a new condition where if the default locale is being requested, the caller now gets a return value that allows the default fallback behavior to happen.
  • format.js now defines the DEFAULT_LOCALE, which is now en-US, not en. I don't remember the discussion with how we landed on en for the theoretical default, but we actually default to en-US so I'm treating that as the thing to use for now. @paulirish if you remember how we were supposed to resolve this

changes in build/build-bundle.js
DevTools currently uses a slightly different transform, where each locale in locales.js is replaced with an empty object ({'ar': {}, 'ar-XB': {}, 'bg': {}, ...}). To verify the new approach, we go back to the approach before #12921 and shim just locale.js with a {}. nevermind for the moment: #13211 (comment)

@brendankenny brendankenny requested a review from a team as a code owner October 13, 2021 20:24
@brendankenny brendankenny requested review from connorjclark and removed request for a team October 13, 2021 20:24
@google-cla google-cla bot added the cla: yes label Oct 13, 2021
@@ -283,6 +283,7 @@ function resolveSettings(settingsJson = {}, overrides = undefined) {
// If a locale is requested in flags or settings, use it. A typical CLI run will not have one,
// however `lookupLocale` will always determine which of our supported locales to use (falling
// back if necessary).
// TODO: could do more work to sniff out the user's locale
Copy link
Member Author

Choose a reason for hiding this comment

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

longtime TODO in i18n.js but seemed better here where the locale is actually chosen

* is to replace the default export of this file with `{}` so that no locale messages
* are included. Strings will work normally through the IcuMessage.formattedDefault
* fallback, and locale messages can be added on demand (e.g. dynamically fetched)
* through `format.registerLocaleData()`.
Copy link
Member Author

Choose a reason for hiding this comment

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

not sure who will ever look in here, but worth documenting somewhere :)

Copy link
Collaborator

@connorjclark connorjclark left a comment

Choose a reason for hiding this comment

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

LGTM!

@paulirish
Copy link
Member

  • format.js now defines the DEFAULT_LOCALE, which is now en-US, not en. I don't remember the discussion with how we landed on en for the theoretical default, but we actually default to en-US so I'm treating that as the thing to use for now. @paulirish if you remember how we were supposed to resolve this

#9520 (comment) is a previous discussion about it. the linked blog post sez:

The default locale for en is up to the implementation, but you’ll usually find en-US. I expect en-001 will take the place of en-US in at least some implementations in the not so distant future.

That does line up with the plan here.

Anyway, for the moment, english inheritance is kinda broken in CLDR. so.. there's no "right" answer for our default being either 'en' or 'en-US'. So it's fine going with whatever's more straightforward. :)

@brendankenny
Copy link
Member Author

Ok, the devtools error is real, so I'm going to back out the build-bundle change and open a separate PR for it.

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

Successfully merging this pull request may close these issues.

4 participants