fix(ivy): i18n - better translation warnings (#32867)
The missing translation and invalid placeholder warnings now contain the "meaning" if one was provided. PR Close #32867
This commit is contained in:
parent
601f87c2ec
commit
97d5700456
|
@ -6,7 +6,8 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import {BLOCK_MARKER} from './constants';
|
import {BLOCK_MARKER} from './constants';
|
||||||
import {MessageId, TargetMessage, parseMessage} from './messages';
|
import {MessageId, ParsedMessage, TargetMessage, parseMessage} from './messages';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A translation message that has been processed to extract the message parts and placeholders.
|
* A translation message that has been processed to extract the message parts and placeholders.
|
||||||
|
@ -49,13 +50,12 @@ export function translate(
|
||||||
return message.substitutions[placeholder];
|
return message.substitutions[placeholder];
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`No placeholder found with name ${placeholder} in message "${message.messageId}" ("${message.messageString}").`);
|
`No placeholder found with name ${placeholder} in message ${describeMessage(message)}.`);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(`No translation found for ${describeMessage(message)}.`);
|
||||||
`No translation found for "${message.messageId}" ("${message.messageString}").`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,3 +90,9 @@ export function makeTemplateObject(cooked: string[], raw: string[]): TemplateStr
|
||||||
Object.defineProperty(cooked, 'raw', {value: raw});
|
Object.defineProperty(cooked, 'raw', {value: raw});
|
||||||
return cooked as any;
|
return cooked as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function describeMessage(message: ParsedMessage): string {
|
||||||
|
const meaningString = message.meaning && ` - "${message.meaning}"`;
|
||||||
|
return `"${message.messageId}" ("${message.messageString}"${meaningString})`;
|
||||||
|
}
|
|
@ -83,6 +83,8 @@ describe('utils', () => {
|
||||||
it('should throw an error if there is no matching translation', () => {
|
it('should throw an error if there is no matching translation', () => {
|
||||||
expect(() => doTranslate({}, parts `abc`))
|
expect(() => doTranslate({}, parts `abc`))
|
||||||
.toThrowError('No translation found for "2674653928643152084" ("abc").');
|
.toThrowError('No translation found for "2674653928643152084" ("abc").');
|
||||||
|
expect(() => doTranslate({}, parts `:meaning|:abc`))
|
||||||
|
.toThrowError('No translation found for "1071947593002928768" ("abc" - "meaning").');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error if the translation contains placeholders that are not in the message',
|
it('should throw an error if the translation contains placeholders that are not in the message',
|
||||||
|
|
Loading…
Reference in New Issue