Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
patrick feedback
  • Loading branch information
exterkamp committed Aug 15, 2019
commit 068d4d88061d364839fb33408f1cee4fbe2062e3
40 changes: 17 additions & 23 deletions lighthouse-core/lib/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,42 +147,36 @@ function _preprocessMessageValues(icuMessage, values = {}) {
*
* Note: This function will recursively inspect plural elements for nested arguementElements.
*
* @param {Array<import('intl-messageformat-parser').Element>} parsedIcu
* @param {Map<string, import('intl-messageformat-parser').Element>} elements
* @param {Array<import('intl-messageformat-parser').Element>} elementsList
Copy link
Contributor

Choose a reason for hiding this comment

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

typically we just use plurals for arrays of things unless the list gives extra info about the value. I like @patrickhulce's earlier suggestion of icuElements (or messageElements...plain old elements is confusing in web-related terminology :) for this, personally.

* @param {Map<string, import('intl-messageformat-parser').Element>} seenElelementsById
*/
function _collectAllCustomElementsFromICU(parsedIcu, elements = new Map()) {
function _collectAllCustomElementsFromICU(elementsList, seenElelementsById = new Map()) {
// add argumentElements
elementsList
.filter(el => el.type === 'argumentElement')
// @ts-ignore - el.id is always defined when el.format is defined
.forEach(el => seenElelementsById.set(el.id, el));

// Rescurse into Plurals
for (const el of parsedIcu) {
for (const el of elementsList) {
if (!el.format || el.format.type !== 'pluralFormat') continue;
// 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.
const elemSet = new Map();
let childElementsById = new Map();
// Look at all options of the plural (=1{} =other{}...)
for (const option of el.format.options) {
// Look at each element of each plural option
for (const element of option.value.elements) {
if (el.type !== 'argumentElement') continue;
// If the element is an argument, then add it to the de-dupe map
elemSet.set(element.id, element);
}
// Run collections on each option's elements
childElementsById = _collectAllCustomElementsFromICU(option.value.elements,
seenElelementsById);
}
const e = Array.from(elemSet.values());
// Add the nested plural elements to the main elements set
const rElements = _collectAllCustomElementsFromICU(e, elements);

for (const [key, value] of rElements) {
elements.set(key, value);
for (const [key, value] of childElementsById) {
seenElelementsById.set(key, value);
}
}

// add other arguementElements
parsedIcu
.filter(el => el.type === 'argumentElement')
// @ts-ignore - el.id is always defined when el.format is defined
.forEach(el => elements.set(el.id, el));

return elements;
return seenElelementsById;
}

/**
Expand Down