Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type ɵParsedTranslation,
ɵisMissingTranslationError,
ɵmakeTemplateObject,
ɵparseMessage,
ɵtranslate,
} from '@angular/localize';
import { MagicString } from 'magic-string';
Expand Down Expand Up @@ -335,11 +336,20 @@ type DiagnosticMessage = { type: 'error' | 'warning'; message: string };
*/
function translateMessage(
diagnostics: DiagnosticMessage[],
translations: Record<string, ɵParsedTranslation>,
translations: Record<string, ɵParsedTranslation> | undefined,
messageParts: TemplateStringsArray,
substitutions: readonly number[],
missingTranslation: 'error' | 'warning' | 'ignore',
): [TemplateStringsArray, readonly number[]] {
// Fast path: untranslated locale (e.g. source locale without a translation dictionary).
// Directly parse the message parts to strip metadata and placeholder markers without
// throwing and catching a MissingTranslationError exception.
if (translations === undefined) {
const message = ɵparseMessage(messageParts, substitutions);

return [ɵmakeTemplateObject(message.messageParts, message.messageParts), substitutions];
}
Comment thread
clydin marked this conversation as resolved.
Resolved

try {
return ɵtranslate(translations, messageParts, substitutions) as [
TemplateStringsArray,
Expand Down Expand Up @@ -512,7 +522,7 @@ async function inlineLocalize(
for (const callSite of metadata.callSites) {
const [translatedParts, translatedSubstitutions] = translateMessage(
diagnostics,
translation || {},
translation,
callSite.messageParts,
callSite.expressionIndexes,
translation === undefined ? 'ignore' : missingTranslation,
Expand Down
14 changes: 14 additions & 0 deletions packages/angular/build/src/tools/i18n/i18n-inliner_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ describe('I18nInliner', () => {
expect(findFile(outputFiles, 'main.js').text).toContain('"Hello"');
});

it('retains the original messages with placeholders for a locale without translations', async () => {
const source = 'export const greeting = $localize`:@@greeting:Hello ${name}:NAME:!`;\n';
const { outputFiles, errors, warnings } = await createInliner().inlineForLocale(
[browserFile('main.js', source)],
'en-US',
undefined,
);

expect(errors).toEqual([]);
expect(warnings).toEqual([]);
expect(findFile(outputFiles, 'main.js').text).toContain('`Hello ${name}!`');
expect(findFile(outputFiles, 'main.js').text).not.toContain('$localize');
});

it('warns and retains the original message when a locale is missing a translation', async () => {
const { outputFiles, errors, warnings } = await createInliner().inlineForLocale(
[browserFile('main.js', GREETING_SOURCE)],
Expand Down
Loading