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 registerLocaleData() method #9638

Merged
merged 3 commits into from
Sep 4, 2019
Merged

i18n: add registerLocaleData() method #9638

merged 3 commits into from
Sep 4, 2019

Conversation

paulirish
Copy link
Member

see Audits Panel Localization Design

This method enables us to "install" new locale strings into our i18n string lookup.

Unrelated, but I added a few more tests to getFormatted as a driveby.

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

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

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

LGTM


// And confirm that's what is returned
const formattedStr2 = i18n.getFormatted(str_(UIStrings.title), 'es-419');
expect(formattedStr2).toEqual('es-419 uses https!');
Copy link
Collaborator

Choose a reason for hiding this comment

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

one more assertion that some other string that used to be there is removed? would be nice to clarify in the tests that it's not merging or something it's just totally replacing

/** @typedef {import('./locales').LhlMessages} LhlMessages */

/**
* Populate the i18n string lookup dict with locale data
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we link off to that design and/or mention how this is expected to be used?


it('overwrites existing locale strings', () => {
const filename = 'lighthouse-core/audits/is-on-https.js';
const UIStrings = require('../../../../' + filename).UIStrings;
Copy link
Collaborator

Choose a reason for hiding this comment

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

any reason this is dynamic? seems like a static string would be easier to refactor when file is moved, etc.

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

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

I had lots of feedback 💇‍♂

/** @typedef {import('./locales').LhlMessages} LhlMessages */

/**
* Populate the i18n string lookup dict with locale data
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Populate the i18n string lookup dict with locale data
* Populate the i18n string lookup dict with locale data.

/**
* Populate the i18n string lookup dict with locale data
* Used when the host environment selects the locale and serves lighthouse the intended locale file
* @see https://docs.google.com/document/d/1jnt3BqKB-4q3AE94UWFA0Gqspx8Sd_jivlB7gQMlmfk/edit
Copy link
Member

Choose a reason for hiding this comment

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

none of this is seen by users of the LH module, though. Does that matter?

Copy link
Member Author

Choose a reason for hiding this comment

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

whatcha mean? it's exposed at the module level..

Copy link
Member

Choose a reason for hiding this comment

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

it's exposed at the module level..

The doc strings, I mean. They aren't in the module-level file and tools like vscode don't forward the jsdocs

* Populate the i18n string lookup dict with locale data
* Used when the host environment selects the locale and serves lighthouse the intended locale file
* @see https://docs.google.com/document/d/1jnt3BqKB-4q3AE94UWFA0Gqspx8Sd_jivlB7gQMlmfk/edit
* @param {LH.Locale} locale
Copy link
Member

Choose a reason for hiding this comment

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

this threatens the little LH.Locale house of cards types. We should either limit to overriding existing locales (verify that locale here is actually of type LH.Locale) or get rid of LH.Locale and type all our locale names as string. There might be some side effects of the second option, though, if we depend on that behaving well.

Copy link
Member Author

Choose a reason for hiding this comment

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

We should either limit to overriding existing locales

in reality this will only be used to override existing locales

Copy link
Member

Choose a reason for hiding this comment

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

in reality this will only be used to override existing locales

This is partly what #9638 (comment) is touching on. I don't think we can treat it as a private api

* Used when the host environment selects the locale and serves lighthouse the intended locale file
* @see https://docs.google.com/document/d/1jnt3BqKB-4q3AE94UWFA0Gqspx8Sd_jivlB7gQMlmfk/edit
* @param {LH.Locale} locale
* @param {LhlMessages} lhlMessages
Copy link
Member

Choose a reason for hiding this comment

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

similarly we should shape check this (a la plugins/budgets.json). All our good locale error handling code assumes the type is correct (and isn't such good error handling code if it isn't)

* @param {LhlMessages} lhlMessages
*/
function registerLocaleData(locale, lhlMessages) {
LOCALES[locale] = lhlMessages;
Copy link
Member

Choose a reason for hiding this comment

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

I'm a little afraid to make LOCALES a mutable singleton like this now, but it's hard to think through the consequences and if they matter. #9296 and #9043 (and wherever the field-performance plugin feedback is) point at the fact that people likely will use this outside of devtools, though.

Copy link
Member

Choose a reason for hiding this comment

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

I'm a little afraid to make LOCALES a mutable singleton like this now, but it's hard to think through the consequences and if they matter. #9296 and #9043 (and wherever the field-performance plugin feedback is) point at the fact that people likely will use this outside of devtools, though.

if we don't want to expose this to anyone but devtools (I believe @exterkamp also preferred not to do that), another option (sorry I didn't think of until right now) is something like
https://github.com/GoogleChrome/lighthouse/blob/b2dfb06221249d86dab667c241303b53c66203ed/clients/devtools-report-assets.js
basically rewriting locales.js to instead access the assets in the module. We could be clever instead of having to do it manually

Copy link
Member

Choose a reason for hiding this comment

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

We could be clever instead of having to do it manually

@connorjclark could even get to use a proxy to rewrite any getter on the locales.js object into a request for the resource :)

'message': 'es-419 uses https!',
},
};
i18n.registerLocaleData('es-419', localeData);
Copy link
Member

Choose a reason for hiding this comment

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

this changes es-419 for all tests run after this, FWIW. It's makes for a more fragile test, but should we reset after it's done?

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

Successfully merging this pull request may close these issues.

None yet

5 participants