-
Notifications
You must be signed in to change notification settings - Fork 9.6k
i18n: fix custom formatted ICU within plurals #9460
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8b4f0f
plural ICU recursive preprocessing
exterkamp 844e38a
Merge branch 'master' into pluralICU
exterkamp c1956bb
fix ts, add comments, make loops more clear, add tests.
exterkamp 9c1f763
fixed spacing
exterkamp 2b5d09f
Fix spelling
exterkamp 068d4d8
patrick feedback
exterkamp 28a5f24
feedback +1, massively simplify recursion, add new test.
exterkamp 9f6f5f3
rename vars
exterkamp 1bcbaa3
clonedValues
exterkamp 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 |
|---|---|---|
|
|
@@ -130,10 +130,61 @@ function lookupLocale(locale) { | |
| * @param {Record<string, string | number>} [values] | ||
| */ | ||
| function _preprocessMessageValues(icuMessage, values = {}) { | ||
| const clonedValues = JSON.parse(JSON.stringify(values)); | ||
| const parsed = MessageParser.parse(icuMessage); | ||
|
|
||
| const elements = _collectAllCustomElementsFromICU(parsed.elements); | ||
|
|
||
| return _processParsedElements(Array.from(elements.values()), JSON.parse(JSON.stringify(values))); | ||
| } | ||
|
|
||
| /** | ||
| * Function to retrieve all 'argumentElement's from an ICU message. An argumentElement | ||
| * is an ICU element with an argument in it, like '{varName}' or '{varName, number, bytes}'. This | ||
| * differs from 'messageElement's which are just arbitrary text in a message. | ||
| * | ||
| * Notes: | ||
| * This function will recursively inspect plural elements for nested argumentElements. | ||
| * | ||
| * We need to find all the elements from the plural format sections, but | ||
| * they need to be deduplicated. I.e. "=1{hello {icu}} =other{hello {icu}}" | ||
| * the variable "icu" would appear twice if it wasn't de duplicated. And they cannot | ||
| * be stored in a set because they are not equal since their locations are different, | ||
| * thus they are stored via a Map keyed on the "id" which is the ICU varName. | ||
| * | ||
| * @param {Array<import('intl-messageformat-parser').Element>} elementsList | ||
|
||
| * @param {Map<string, import('intl-messageformat-parser').Element>} seenElelementsById | ||
exterkamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| function _collectAllCustomElementsFromICU(elementsList, seenElelementsById = new Map()) { | ||
| for (const el of elementsList) { | ||
| // We are only interested in elements that need ICU formatting (argumentElements) | ||
| if (el.type !== 'argumentElement') continue; | ||
| // @ts-ignore - el.id is always defined when el.format is defined | ||
| seenElelementsById.set(el.id, el); | ||
|
|
||
| // Plurals need to be inspected recursively | ||
| if (!el.format || el.format.type !== 'pluralFormat') continue; | ||
| // Look at all options of the plural (=1{} =other{}...) | ||
| for (const option of el.format.options) { | ||
| // Run collections on each option's elements | ||
| _collectAllCustomElementsFromICU(option.value.elements, | ||
| seenElelementsById); | ||
| } | ||
| } | ||
|
|
||
| return seenElelementsById; | ||
| } | ||
|
|
||
| /** | ||
| * This function takes a list of ICU argumentElements and a map of values and | ||
| * will apply Lighthouse custom formatting to the values based on the argumentElement | ||
| * format style. | ||
| * | ||
| * @param {Array<import('intl-messageformat-parser').Element>} icuElementsList | ||
exterkamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param {Record<string, string | number>} [values] | ||
| */ | ||
| function _processParsedElements(icuElementsList, values = {}) { | ||
| // Throw an error if a message's value isn't provided | ||
| parsed.elements | ||
| icuElementsList | ||
| .filter(el => el.type === 'argumentElement') | ||
| .forEach(el => { | ||
| if (el.id && (el.id in values) === false) { | ||
|
|
@@ -142,24 +193,24 @@ function _preprocessMessageValues(icuMessage, values = {}) { | |
| }); | ||
|
|
||
| // Round all milliseconds to the nearest 10 | ||
| parsed.elements | ||
| icuElementsList | ||
| .filter(el => el.format && el.format.style === 'milliseconds') | ||
| // @ts-ignore - el.id is always defined when el.format is defined | ||
| .forEach(el => (clonedValues[el.id] = Math.round(clonedValues[el.id] / 10) * 10)); | ||
| .forEach(el => (values[el.id] = Math.round(values[el.id] / 10) * 10)); | ||
|
|
||
| // Convert all seconds to the correct unit | ||
| parsed.elements | ||
| icuElementsList | ||
| .filter(el => el.format && el.format.style === 'seconds' && el.id === 'timeInMs') | ||
| // @ts-ignore - el.id is always defined when el.format is defined | ||
| .forEach(el => (clonedValues[el.id] = Math.round(clonedValues[el.id] / 100) / 10)); | ||
| .forEach(el => (values[el.id] = Math.round(values[el.id] / 100) / 10)); | ||
|
|
||
| // Replace all the bytes with KB | ||
| parsed.elements | ||
| icuElementsList | ||
| .filter(el => el.format && el.format.style === 'bytes') | ||
| // @ts-ignore - el.id is always defined when el.format is defined | ||
| .forEach(el => (clonedValues[el.id] = clonedValues[el.id] / 1024)); | ||
| .forEach(el => (values[el.id] = values[el.id] / 1024)); | ||
|
|
||
| return clonedValues; | ||
| return values; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.