Skip to content

Commit

Permalink
[l10n] Support reusing the same ICU variable in message strings
Browse files Browse the repository at this point in the history
This patches in a change from upstream:
GoogleChrome/lighthouse#16159

See the above PR for details.

Bug: None
Change-Id: Id73070ec4c2b4ed5ff227d0a5d486416b67eb5a5
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5807627
Commit-Queue: Simon Zünd <szuend@chromium.org>
Reviewed-by: Adam Raine <asraine@chromium.org>
Reviewed-by: Simon Zünd <szuend@chromium.org>
  • Loading branch information
Connor Clark authored and Devtools-frontend LUCI CQ committed Aug 23, 2024
1 parent 18fd35f commit 895fb50
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions third_party/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ function _processPlaceholderCustomFormattedIcu(icu) {
icu.message = '';
let idx = 0;

const rawNameCache = new Map();

while (parts.length) {
// Seperate out the match into parts.
const [preambleText, rawName, format, formatType] = parts.splice(0, 4);
Expand All @@ -332,8 +334,22 @@ function _processPlaceholderCustomFormattedIcu(icu) {
throw Error(`Unsupported custom-formatted ICU type var "${formatType}" in message "${icu.message}"`);
}

let index;
const previousRawName = rawNameCache.get(rawName);
if (previousRawName) {
const [prevFormat, prevFormatType, prevIndex] = previousRawName;
if (prevFormat !== format || prevFormatType !== formatType) {
throw new Error(`must use same format and formatType for a given name. Invalid for: ${rawName}`);
}

index = prevIndex;
} else {
index = idx++;
rawNameCache.set(rawName, [format, formatType, index]);
}

// Append ICU replacements if there are any.
const placeholderName = `CUSTOM_ICU_${idx++}`;
const placeholderName = `CUSTOM_ICU_${index}`;
icu.message += `$${placeholderName}$`;
let example;

Expand Down Expand Up @@ -414,7 +430,7 @@ function _processPlaceholderDirectIcu(icu, examples) {
throw Error(`Example '${key}' provided, but has not corresponding ICU replacement in message "${icu.message}"`);
}
const eName = `ICU_${idx++}`;
tempMessage = tempMessage.replace(`{${key}}`, `$${eName}$`);
tempMessage = tempMessage.replaceAll(`{${key}}`, `$${eName}$`);

icu.placeholders[eName] = {
content: `{${key}}`,
Expand Down

0 comments on commit 895fb50

Please sign in to comment.