-
Notifications
You must be signed in to change notification settings - Fork 9.7k
i18n: introduce script to swap in new locale to LHR #8755
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a8ff2a0
i18n: introduce script to swap in new locale to LHR
paulirish 81b0dc4
rename
paulirish e12f714
add start of a test.
paulirish 30bb475
revert now build changes.
paulirish 059bc0f
feedback
paulirish 54c4833
rename to fallbackMessage
paulirish bdd29b4
fix espanol string
paulirish fdf96e8
Merge branch 'master' into swaplocale
paulirish 5255aee
Update lighthouse-core/lib/i18n/swap-locale.js
paulirish 627f7be
Update lighthouse-core/lib/i18n/swap-locale.js
paulirish 3531318
feedback
paulirish 5654af1
console time
paulirish a225504
Merge branch 'master' into swaplocale
paulirish 853bfd2
drop casts
paulirish e3f2d3f
return warnings. feedback
paulirish 5efe7eb
Merge branch 'master' into swaplocale
paulirish cf0d3b7
remove lherror dep
paulirish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * @license Copyright 2019 Google Inc. All Rights Reserved. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| const _set = require('lodash.set'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😍 |
||
|
|
||
| const i18n = require('./i18n.js'); | ||
|
|
||
| /** | ||
| * @fileoverview Use the lhr.i18n.icuMessagePaths object to change locales | ||
| * | ||
| * `icuMessagePaths` is an object keyed by `icuMessageId`s. Within each is either | ||
| * 1) an array of strings, which are just object paths to where that message is used in the LHR | ||
| * 2) an array of `LH.I18NMessageValuesEntry`s which include both a `path` and a `values` object | ||
| * which will be used in the replacement within `i18n._formatIcuMessage()` | ||
| * | ||
| * An example: | ||
| "icuMessagePaths": { | ||
| "lighthouse-core/audits/metrics/first-contentful-paint.js | title": [ | ||
| "audits[first-contentful-paint].title" | ||
| ], | ||
| "lighthouse-core/audits/time-to-first-byte.js | displayValue": [ | ||
| { | ||
| "values": { | ||
| "timeInMs": 570.5630000000001 | ||
| }, | ||
| "path": "audits[time-to-first-byte].displayValue" | ||
| } | ||
| ], | ||
| "lighthouse-core/lib/i18n/i18n.js | columnTimeSpent": [ | ||
| "audits[mainthread-work-breakdown].details.headings[1].text", | ||
| "audits[network-rtt].details.headings[1].text", | ||
| "audits[network-server-latency].details.headings[1].text" | ||
| ], | ||
| ... | ||
| */ | ||
|
|
||
| /** | ||
| * Returns a new LHR with all strings changed to the new `requestedLocale`. | ||
| * @param {LH.Result} lhr | ||
| * @param {LH.Locale} requestedLocale | ||
| * @return {{lhr: LH.Result, missingIcuMessageIds: string[]}} | ||
| */ | ||
| function swapLocale(lhr, requestedLocale) { | ||
| // Copy LHR to avoid mutating provided LHR. | ||
| lhr = JSON.parse(JSON.stringify(lhr)); | ||
|
|
||
| const locale = i18n.lookupLocale(requestedLocale); | ||
| const {icuMessagePaths} = lhr.i18n; | ||
| const missingIcuMessageIds = /** @type {string[]} */([]); | ||
|
|
||
| Object.entries(icuMessagePaths).forEach(([icuMessageId, messageInstancesInLHR]) => { | ||
| for (const instance of messageInstancesInLHR) { | ||
| // The path that _formatPathAsString() generated | ||
| let path; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| let values; | ||
| if (typeof instance === 'string') { | ||
| path = instance; | ||
| } else { | ||
| path = instance.path; | ||
| // `values` are the string template values to be used. eg. `values: {wastedBytes: 9028}` | ||
| values = instance.values; | ||
| } | ||
| // If we couldn't find the new replacement message, keep things as is. | ||
| try { | ||
| // Get new formatted strings in revised locale | ||
| const formattedStr = i18n.getFormattedFromIdAndValues(locale, icuMessageId, values); | ||
| // Write string back into the LHR | ||
| _set(lhr, path, formattedStr); | ||
| } catch (err) { | ||
| if (err.message === i18n._ICUMsgNotFoundMsg) { | ||
| missingIcuMessageIds.push(icuMessageId); | ||
| } else { | ||
| throw err; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| lhr.i18n.rendererFormattedStrings = i18n.getRendererFormattedStrings(locale); | ||
| // Tweak the config locale | ||
| lhr.configSettings.locale = locale; | ||
| return { | ||
| lhr, | ||
| missingIcuMessageIds, | ||
| }; | ||
| } | ||
|
|
||
| module.exports = swapLocale; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * @license Copyright 2019 Google Inc. All Rights Reserved. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| const swapLocale = require('../../../lib/i18n/swap-locale.js'); | ||
|
|
||
| const lhr = require('../../results/sample_v2.json'); | ||
|
|
||
| /* eslint-env jest */ | ||
| describe('swap-locale', () => { | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| it('can change golden LHR english strings into spanish', () => { | ||
| const lhrEn = /** @type {LH.Result} */ (JSON.parse(JSON.stringify(lhr))); | ||
| const lhrEs = swapLocale(lhrEn, 'es').lhr; | ||
|
|
||
| // Basic replacement | ||
| expect(lhrEn.audits.plugins.title).toEqual('Document avoids plugins'); | ||
| expect(lhrEs.audits.plugins.title).toEqual('El documento no usa complementos'); | ||
|
|
||
| // With ICU string argument values | ||
| expect(lhrEn.audits['dom-size'].displayValue).toEqual('31 elements'); | ||
| expect(lhrEs.audits['dom-size'].displayValue).toEqual('31 elementos'); | ||
|
|
||
| // Renderer formatted strings | ||
| expect(lhrEn.i18n.rendererFormattedStrings.labDataTitle).toEqual('Lab Data'); | ||
| expect(lhrEs.i18n.rendererFormattedStrings.labDataTitle).toEqual('Datos de prueba'); | ||
| }); | ||
|
|
||
| it('can roundtrip back to english correctly', () => { | ||
| const lhrEn = /** @type {LH.Result} */ (JSON.parse(JSON.stringify(lhr))); | ||
|
|
||
| // via Spanish | ||
| const lhrEnEsRT = swapLocale(swapLocale(lhrEn, 'es').lhr, 'en-US').lhr; | ||
| expect(lhrEnEsRT).toEqual(lhrEn); | ||
|
|
||
| // via Arabic | ||
| const lhrEnArRT = swapLocale(swapLocale(lhrEn, 'ar').lhr, 'en-US').lhr; | ||
| expect(lhrEnArRT).toEqual(lhrEn); | ||
| }); | ||
|
|
||
| it('leaves alone messages where there is no translation available', () => { | ||
| const miniLHR = { | ||
| audits: { | ||
| redirects: { | ||
| id: 'redirects', | ||
| title: 'Avoid multiple page redirects', | ||
| }, | ||
| fakeaudit: { | ||
| id: 'fakeaudit', | ||
| title: 'An audit without translations', | ||
| }, | ||
| }, | ||
| configSettings: { | ||
| locale: 'en-US', | ||
| }, | ||
| i18n: { | ||
| icuMessagePaths: { | ||
| 'lighthouse-core/audits/redirects.js | title': ['audits.redirects.title'], | ||
| 'lighthouse-core/audits/redirects.js | doesntExist': ['audits.redirects.doesntExist'], | ||
| 'lighthouse-core/audits/fakeaudit.js | title': ['audits.fakeaudit.title'], | ||
| }, | ||
| }, | ||
| }; | ||
| const {missingIcuMessageIds} = swapLocale(miniLHR, 'es'); | ||
|
|
||
| // Updated strings are not found, so these remain in the original language | ||
| expect(missingIcuMessageIds).toMatchInlineSnapshot(` | ||
| Array [ | ||
| "lighthouse-core/audits/redirects.js | doesntExist", | ||
| "lighthouse-core/audits/fakeaudit.js | title", | ||
| ] | ||
| `); | ||
| }); | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickhulce note this change.. it was using the fallback message instead of the one pulled from the locales files. which seemed odd. right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is weird. As long as there isn't an old mismatched translation for the locale it shouldn't matter, but agreed that checking that the
valueswill actually be able to go into the string we want them to (and preparing them to do so) is the right thing to do.Mismatched translations could become a problem at some point. If we've updated a string in
en-US.jsonand it has differentvaluesthan the not-yet-updated strings in all the other locales, I'm pretty sure that will either throw in_preprocessMessageValuesor below in the formatter.Maybe we should have a check in string collection that deletes strings in other locales if the expected
valuesdon't match anymore.