Skip to content

Commit

Permalink
complex ICU -> custom-formatted ICU
Browse files Browse the repository at this point in the history
  • Loading branch information
exterkamp committed Jul 22, 2019
1 parent 37be64c commit d2a8326
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
20 changes: 10 additions & 10 deletions lighthouse-core/scripts/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function convertMessageToPlaceholders(message, examples = {}) {

_processPlaceholderMarkdownLink(icuDefn);

_processPlaceholderComplexIcu(icuDefn);
_processPlaceholderCustomFormattedIcu(icuDefn);

_processPlaceholderDirectIcu(icuDefn, examples);

Expand Down Expand Up @@ -211,24 +211,24 @@ function _processPlaceholderMarkdownLink(icu) {
}

/**
* Convert complex ICU syntax into placeholders with examples.
* Convert custom-formatted ICU syntax into placeholders with examples.
* Custom formats defined in i18n.js in "format" object.
*
* Before:
* icu: 'This audit took {timeInMs, number, milliseconds} ms.'
* After:
* icu: 'This audit took $COMPLEX_ICU_0' ms.
* icu: 'This audit took $CUSTOM_ICU_0' ms.
* placeholders: {
* COMPLEX_IXU_0 {
* CUSTOM_ICU_0 {
* content: {timeInMs, number, milliseconds},
* example: 499,
* }
* }
*
* @param {ICUMessageDefn} icu
*/
function _processPlaceholderComplexIcu(icu) {
// Split on complex ICU: {var, number, type}
function _processPlaceholderCustomFormattedIcu(icu) {
// Split on custom-formatted ICU: {var, number, type}
const parts = icu.message.split(
/\{(\w+), (\w+), (\w+)\}/g);
icu.message = '';
Expand All @@ -240,18 +240,18 @@ function _processPlaceholderComplexIcu(icu) {
icu.message += preambleText;

if (!rawName || !format || !formatType) continue;
// Check that complex ICU not using non-supported format ex:
// Check that custom-formatted ICU not using non-supported format ex:
// * using a second arg anything other than "number"
// * using a third arg that is not millis, secs, bytes, %, or extended %
if (!format.match(/^number$/)) {
throw Error(`Unsupported Complex ICU format var "${format}" in message "${icu.message}"`);
throw Error(`Unsupported custom-formatted ICU format var "${format}" in message "${icu.message}"`);
}
if (!formatType.match(/milliseconds|seconds|bytes|percent|extendedPercent/)) {
throw Error(`Unsupported Complex ICU type var "${formatType}" in message "${icu.message}"`);
throw Error(`Unsupported custom-formatted ICU type var "${formatType}" in message "${icu.message}"`);
}

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

Expand Down
36 changes: 18 additions & 18 deletions lighthouse-core/test/scripts/i18n/collect-strings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,34 +271,34 @@ describe('Convert Message to Placeholder', () => {
.toThrow(/Bad Link syntax in message "Hello \[World\] \(https:\/\/google\.com\/\)\."/);
});

it('converts complex ICU to placholders', () => {
it('converts custom-formatted ICU to placholders', () => {
const message = 'Hello World took {timeInMs, number, milliseconds} ms, ' +
'{timeInSec, number, seconds} s, used {bytes, number, bytes} KB, ' +
'{perc, number, percent} of {percEx, number, extendedPercent}.';

const res = collect.convertMessageToPlaceholders(message, undefined);
const expectation = 'Hello World took $COMPLEX_ICU_0$ ms, ' +
'$COMPLEX_ICU_1$ s, used $COMPLEX_ICU_2$ KB, ' +
'$COMPLEX_ICU_3$ of $COMPLEX_ICU_4$.';
const expectation = 'Hello World took $CUSTOM_ICU_0$ ms, ' +
'$CUSTOM_ICU_1$ s, used $CUSTOM_ICU_2$ KB, ' +
'$CUSTOM_ICU_3$ of $CUSTOM_ICU_4$.';
expect(res.message).toBe(expectation);
expect(res.placeholders).toEqual({
COMPLEX_ICU_0: {
CUSTOM_ICU_0: {
content: '{timeInMs, number, milliseconds}',
example: '499',
},
COMPLEX_ICU_1: {
CUSTOM_ICU_1: {
content: '{timeInSec, number, seconds}',
example: '2.4',
},
COMPLEX_ICU_2: {
CUSTOM_ICU_2: {
content: '{bytes, number, bytes}',
example: '499',
},
COMPLEX_ICU_3: {
CUSTOM_ICU_3: {
content: '{perc, number, percent}',
example: '54.6%',
},
COMPLEX_ICU_4: {
CUSTOM_ICU_4: {
content: '{percEx, number, extendedPercent}',
example: '37.92%',
},
Expand All @@ -308,36 +308,36 @@ describe('Convert Message to Placeholder', () => {
it('replaces within ICU plural', () => {
const message = '{var, select, male{time: {timeInSec, number, seconds}} ' +
'female{time: {timeInSec, number, seconds}} other{time: {timeInSec, number, seconds}}}';
const expectation = '{var, select, male{time: $COMPLEX_ICU_0$} ' +
'female{time: $COMPLEX_ICU_1$} other{time: $COMPLEX_ICU_2$}}';
const expectation = '{var, select, male{time: $CUSTOM_ICU_0$} ' +
'female{time: $CUSTOM_ICU_1$} other{time: $CUSTOM_ICU_2$}}';
const res = collect.convertMessageToPlaceholders(message, undefined);
expect(res.message).toEqual(expectation);
expect(res.placeholders).toEqual({
COMPLEX_ICU_0: {
CUSTOM_ICU_0: {
content: '{timeInSec, number, seconds}',
example: '2.4',
},
COMPLEX_ICU_1: {
CUSTOM_ICU_1: {
content: '{timeInSec, number, seconds}',
example: '2.4',
},
COMPLEX_ICU_2: {
CUSTOM_ICU_2: {
content: '{timeInSec, number, seconds}',
example: '2.4',
},
});
});

it('errors when using non-supported complex ICU format', () => {
it('errors when using non-supported custom-formatted ICU format', () => {
const message = 'Hello World took {var, badFormat, milliseconds}.';
expect(() => collect.convertMessageToPlaceholders(message, undefined)).toThrow(
/Unsupported Complex ICU format var "badFormat" in message "Hello World took "/);
/Unsupported custom-formatted ICU format var "badFormat" in message "Hello World took "/);
});

it('errors when using non-supported complex ICU type', () => {
it('errors when using non-supported custom-formatted ICU type', () => {
const message = 'Hello World took {var, number, global_int}.';
expect(() => collect.convertMessageToPlaceholders(message, undefined)).toThrow(
/Unsupported Complex ICU type var "global_int" in message "Hello World took "/);
/Unsupported custom-formatted ICU type var "global_int" in message "Hello World took "/);
});

it('converts direct ICU with examples to placeholders', () => {
Expand Down

0 comments on commit d2a8326

Please sign in to comment.