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

WIP: i18n support for plugins #9799

Closed

Conversation

jburger424
Copy link
Contributor

@jburger424 jburger424 commented Oct 7, 2019

This is just a start but wanted to get initial thoughts.

This allows for a plugin to define a directory containing their locale files, we assume that all files follow the format ${locale}.json.

I've introduced aliases.js which should be able to be used for both core and plugin locales, however I may be missing something in the purpose of locales.js.

Bug: #9641

Add TODO.
lighthouse-core/lib/i18n/aliases.js Outdated Show resolved Hide resolved
lighthouse-core/lib/i18n/aliases.js Show resolved Hide resolved
lighthouse-core/lib/i18n/i18n.js Outdated Show resolved Hide resolved
const locale = tempPath.replace(/\.json$/, '');
const relativePrefix = path.relative(LH_ROOT, rootPath).replace(/\\/g, '/');
if (LOCALES[locale]) {
const localeToMerge = require(`${fullLocalePath}/${tempPath}`);
Copy link
Member

Choose a reason for hiding this comment

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

can you separate this inner bit into its own function?

appendStringsToLocale ?

lighthouse-core/config/config.js Outdated Show resolved Hide resolved
const rawPluginJson = require(pluginPath);
const pluginJson = ConfigPlugin.parsePlugin(rawPluginJson, pluginName);

if (rawPluginJson['localePath']) {
const pluginParentPath =
pluginPath.replace(new RegExp(`/${pluginName}/.*`,'g'), '');
Copy link
Member

Choose a reason for hiding this comment

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

there's gotta be a better way to do this.. ;) probably with path module?

const rawPluginJson = require(pluginPath);
const pluginJson = ConfigPlugin.parsePlugin(rawPluginJson, pluginName);

if (rawPluginJson['localePath']) {
Copy link
Member

Choose a reason for hiding this comment

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

naming bikeshed time?!!?!

first inclination is to call it localesPath (since theres a lot of files in there)

but i wonder if there's a more industry standard term for a directory full of i18n strings. @exterkamp ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

switched to localesPath for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now localeStrings due to updated config

@paulirish
Copy link
Member

paulirish commented Oct 11, 2019

should be said:

The overall approach -- a plugin providing a relative path where LH can find a bunch of .json files with LHL messages -- SGTM. 👍

@paulirish
Copy link
Member

paulirish commented Oct 12, 2019

The overall approach -- a plugin providing a relative path where LH can find a bunch of .json files with LHL messages -- SGTM. 👍

on second thought... :)

@connorjclark points out something... in that model, LH has to readdir and blindly read everything everything off disk.

Perhaps a more explicit contract would be:

/** @type {LH.Config.Plugin} */
module.exports = {
  audits: [{
    path: 'lighthouse-plugin-example/audits/thing.js',
  }],

  localeStrings: {
    'en-US': require('./locales/en-US.json'),
    'es-419': require('./locales/es-419.json'),
  },

  //...

with this model, LH can validate the locale codes very directly. and not rely on filename as locale code.
and this'll also be more straightforward for browserifying.

@connorjclark
Copy link
Collaborator

connorjclark commented Oct 12, 2019

+1 to above. it'd be nice to just have the config provide a path to the locale folder, but that would involve us telling browserify to explicitly bundle everything in those folder for syndicated plugins + expecting the files to be a certain filename (es.json vs es.lhl.json or w/e). Doing the whole thing in the config (like we do in locales.js) makes bundling easy.

We can still merge just the one locale when resolving the config. That's orthogonal to the above.

@connorjclark
Copy link
Collaborator

connorjclark commented Sep 2, 2020

We are i18n'ing the report in DevTools soon (https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2382176), so we should get this plugin translated.

We recently moved stack packs and its strings into its own npm package, and look into our own node_modules to pluck out the i18n string data. We could do the same here. @jburger424 would it be preferable to use our i18n pipeline, instead of you needing to duplicate the same work on your end?

Of course, plugins being able to provide their own locales is a worthy feature, but I think for now we don't need to go there.

@jburger424
Copy link
Contributor Author

We are i18n'ing the report in DevTools soon (chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2382176), so we should get this plugin translated.

We recently moved stack packs and its strings into its own npm package, and look into our own node_modules to pluck out the i18n string data. We could do the same here. @jburger424 would it be preferable to use our i18n pipeline, instead of you needing to duplicate the same work on your end?

@connorjclark Yeah, sorry to have been sitting on this for so long. We actually have an existing pipeline, with translations that just need to be packaged, I should be able to cut a new release including those this week.

@connorjclark
Copy link
Collaborator

an existing pipeline

That's fine, we'll need to do this then. We can't prioritize this at the moment, so it's likely this plugin will initially be the only untranslated category within CDT.

@connorjclark connorjclark added the P2 label Sep 8, 2020
@connorjclark
Copy link
Collaborator

connorjclark commented Sep 8, 2020

To expand:

  1. LH.Config.Plugin.localeStrings of type Record<string, LocaleJson>, where string is the locale code
  2. augment locale data in config.js when resolving plugins
  3. for CDT/LR bundling, we separate the locale json from the code. in CDT we lazily download the locale file for the language we need. So all data for a locale should be in one file, but this model introduces a file per plugin per locale. On bundling, we should drop LH.Config.Plugin.localeStrings. And we need a way to include the bundled plugin strings in the main locale files.
    • Simplest way is to inject them into each locale in collect-strings. They get tracked in git.
    • More complex would involve a new build step for locale files, doing the merging, and rolling from these new built locales in roll-to-devtools.sh (EDIT: @paulirish and I prefer this)
    • (OR....) we figure out how to merge your TC project strings to LH'S TC project, and modify collect-strings.js to handle pubads too? Or just re-do the work in our TC project.

jburger424 added a commit to jburger424/lighthouse that referenced this pull request Oct 27, 2020
@jburger424
Copy link
Contributor Author

Replacing with #11602

@jburger424 jburger424 closed this Oct 27, 2020
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.

None yet

4 participants