fix(ivy): i18n - start generated placeholder name at PH (#32493)

Currently the expressions used in a template string are automatically named
`PH_1`, `PH_2`, etc. Whereas interpolations used in i18n templates generate
placeholders automatically named `INTERPOLATION`, `INTERPOLATION_1`, etc.

This commit aligns the behaviors by starting the generated placeholder
names for expressions at `PH`, then `PH_1`, etc.

It also documents this behavior in the documentation of `$localize` as
it was not mentioned before.

PR Close #32493
This commit is contained in:
cexbrayat
2019-09-05 15:11:31 +02:00
committed by Andrew Kushnir
parent 4c168ed9ba
commit f1b1de9a3d
6 changed files with 103 additions and 52 deletions

View File

@ -16,10 +16,10 @@ describe('$localize tag with translations', () => {
beforeEach(() => {
loadTranslations(computeIds({
'abc': 'abc',
'abc{$ph_1}': 'abc{$ph_1}',
'abc{$ph_1}def': 'abc{$ph_1}def',
'abc{$ph_1}def{$ph_2}': 'abc{$ph_1}def{$ph_2}',
'Hello, {$ph_1}!': 'Hello, {$ph_1}!',
'abc{$PH}': 'abc{$PH}',
'abc{$PH}def': 'abc{$PH}def',
'abc{$PH}def{$PH_1}': 'abc{$PH}def{$PH_1}',
'Hello, {$PH}!': 'Hello, {$PH}!',
}));
});
afterEach(() => { clearTranslations(); });
@ -38,10 +38,10 @@ describe('$localize tag with translations', () => {
beforeEach(() => {
loadTranslations(computeIds({
'abc': 'ABC',
'abc{$ph_1}': 'ABC{$ph_1}',
'abc{$ph_1}def': 'ABC{$ph_1}DEF',
'abc{$ph_1}def{$ph_2}': 'ABC{$ph_1}DEF{$ph_2}',
'Hello, {$ph_1}!': 'HELLO, {$ph_1}!',
'abc{$PH}': 'ABC{$PH}',
'abc{$PH}def': 'ABC{$PH}DEF',
'abc{$PH}def{$PH_1}': 'ABC{$PH}DEF{$PH_1}',
'Hello, {$PH}!': 'HELLO, {$PH}!',
}));
});
afterEach(() => { clearTranslations(); });
@ -59,7 +59,7 @@ describe('$localize tag with translations', () => {
describe('to reverse expressions', () => {
beforeEach(() => {
loadTranslations(computeIds({
'abc{$ph_1}def{$ph_2} - Hello, {$ph_3}!': 'abc{$ph_3}def{$ph_2} - Hello, {$ph_1}!',
'abc{$PH}def{$PH_1} - Hello, {$PH_2}!': 'abc{$PH_2}def{$PH_1} - Hello, {$PH}!',
}));
});
afterEach(() => { clearTranslations(); });
@ -74,7 +74,7 @@ describe('$localize tag with translations', () => {
describe('to remove expressions', () => {
beforeEach(() => {
loadTranslations(computeIds({
'abc{$ph_1}def{$ph_2} - Hello, {$ph_3}!': 'abc{$ph_1} - Hello, {$ph_3}!',
'abc{$PH}def{$PH_1} - Hello, {$PH_2}!': 'abc{$PH} - Hello, {$PH_2}!',
}));
});
afterEach(() => { clearTranslations(); });

View File

@ -44,13 +44,13 @@ describe('messages utils', () => {
it('should compute the translation key, inferring placeholder names if not given', () => {
const message = parseMessage(makeTemplateObject(['a', 'b', 'c'], ['a', 'b', 'c']), [1, 2]);
expect(message.messageId).toEqual('3269094494609300850');
expect(message.messageId).toEqual('8107531564991075946');
});
it('should compute the translation key, ignoring escaped placeholder names', () => {
const message = parseMessage(
makeTemplateObject(['a', ':one:b', ':two:c'], ['a', '\\:one:b', '\\:two:c']), [1, 2]);
expect(message.messageId).toEqual('529036009514785949');
expect(message.messageId).toEqual('2623373088949454037');
});
it('should compute the translation key, handling empty raw values', () => {
@ -67,7 +67,7 @@ describe('messages utils', () => {
it('should build a map of implied placeholders to expressions', () => {
const message = parseMessage(makeTemplateObject(['a', 'b', 'c'], ['a', 'b', 'c']), [1, 2]);
expect(message.substitutions).toEqual({ph_1: 1, ph_2: 2});
expect(message.substitutions).toEqual({PH: 1, PH_1: 2});
});
});

View File

@ -83,10 +83,10 @@ describe('utils', () => {
it('(with identity translations) should render template literals as-is', () => {
const translations = {
'abc': 'abc',
'abc{$ph_1}': 'abc{$ph_1}',
'abc{$ph_1}def': 'abc{$ph_1}def',
'abc{$ph_1}def{$ph_2}': 'abc{$ph_1}def{$ph_2}',
'Hello, {$ph_1}!': 'Hello, {$ph_1}!',
'abc{$PH}': 'abc{$PH}',
'abc{$PH}def': 'abc{$PH}def',
'abc{$PH}def{$PH_1}': 'abc{$PH}def{$PH_1}',
'Hello, {$PH}!': 'Hello, {$PH}!',
};
expect(doTranslate(translations, parts `abc`)).toEqual(parts `abc`);
expect(doTranslate(translations, parts `abc${1 + 2 + 3}`)).toEqual(parts `abc${1 + 2 + 3}`);
@ -103,10 +103,10 @@ describe('utils', () => {
() => {
const translations = {
'abc': 'ABC',
'abc{$ph_1}': 'ABC{$ph_1}',
'abc{$ph_1}def': 'ABC{$ph_1}DEF',
'abc{$ph_1}def{$ph_2}': 'ABC{$ph_1}DEF{$ph_2}',
'Hello, {$ph_1}!': 'HELLO, {$ph_1}!',
'abc{$PH}': 'ABC{$PH}',
'abc{$PH}def': 'ABC{$PH}DEF',
'abc{$PH}def{$PH_1}': 'ABC{$PH}DEF{$PH_1}',
'Hello, {$PH}!': 'HELLO, {$PH}!',
};
expect(doTranslate(translations, parts `abc`)).toEqual(parts `ABC`);
expect(doTranslate(translations, parts `abc${1 + 2 + 3}`))
@ -123,7 +123,7 @@ describe('utils', () => {
it('(with translations to reverse expressions) should render template literals with expressions reversed',
() => {
const translations = {
'abc{$ph_1}def{$ph_2} - Hello, {$ph_3}!': 'abc{$ph_3}def{$ph_2} - Hello, {$ph_1}!',
'abc{$PH}def{$PH_1} - Hello, {$PH_2}!': 'abc{$PH_2}def{$PH_1} - Hello, {$PH}!',
};
const getName = () => 'World';
expect(doTranslate(
@ -134,7 +134,7 @@ describe('utils', () => {
it('(with translations to remove expressions) should render template literals with expressions removed',
() => {
const translations = {
'abc{$ph_1}def{$ph_2} - Hello, {$ph_3}!': 'abc{$ph_1} - Hello, {$ph_3}!',
'abc{$PH}def{$PH_1} - Hello, {$PH_2}!': 'abc{$PH} - Hello, {$PH_2}!',
};
const getName = () => 'World';
expect(doTranslate(