diff --git a/packages/angular_devkit/build_angular/src/builders/application/i18n.ts b/packages/angular_devkit/build_angular/src/builders/application/i18n.ts index e76244c140ab..e7a2b63e3238 100644 --- a/packages/angular_devkit/build_angular/src/builders/application/i18n.ts +++ b/packages/angular_devkit/build_angular/src/builders/application/i18n.ts @@ -52,10 +52,13 @@ export async function inlineI18n( try { for (const locale of options.i18nOptions.inlineLocales) { // A locale specific set of files is returned from the inliner. - const localeOutputFiles = await inliner.inlineForLocale( + const localeInlineResult = await inliner.inlineForLocale( locale, options.i18nOptions.locales[locale].translation, ); + const localeOutputFiles = localeInlineResult.outputFiles; + inlineResult.errors.push(...localeInlineResult.errors); + inlineResult.warnings.push(...localeInlineResult.warnings); const baseHref = getLocaleBaseHref(options.baseHref, options.i18nOptions, locale) ?? options.baseHref; diff --git a/packages/angular_devkit/build_angular/src/builders/application/tests/options/i18n-missing-translation_spec.ts b/packages/angular_devkit/build_angular/src/builders/application/tests/options/i18n-missing-translation_spec.ts new file mode 100644 index 000000000000..93b90a6fc1ec --- /dev/null +++ b/packages/angular_devkit/build_angular/src/builders/application/tests/options/i18n-missing-translation_spec.ts @@ -0,0 +1,224 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import { buildApplication } from '../../index'; +import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; + +describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { + describe('Option: "i18nMissingTranslation"', () => { + beforeEach(() => { + harness.useProject('test', { + root: '.', + sourceRoot: 'src', + cli: { + cache: { + enabled: false, + }, + }, + i18n: { + locales: { + 'fr': 'src/locales/messages.fr.xlf', + }, + }, + }); + }); + + it('should warn when i18nMissingTranslation is undefined (default)', async () => { + harness.useTarget('build', { + ...BASE_OPTIONS, + localize: true, + i18nMissingTranslation: undefined, + }); + + await harness.writeFile( + 'src/app/app.component.html', + ` +
Hello {{ title }}!
+ `, + ); + + await harness.writeFile('src/locales/messages.fr.xlf', MISSING_TRANSLATION_FILE_CONTENT); + + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); + + expect(result?.success).toBeTrue(); + expect(logs).toContain( + jasmine.objectContaining({ + level: 'warn', + message: jasmine.stringMatching('No translation found for'), + }), + ); + }); + + it('should warn when i18nMissingTranslation is set to warning', async () => { + harness.useTarget('build', { + ...BASE_OPTIONS, + localize: true, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + i18nMissingTranslation: 'warning' as any, + }); + + await harness.writeFile( + 'src/app/app.component.html', + ` +Hello {{ title }}!
+ `, + ); + + await harness.writeFile('src/locales/messages.fr.xlf', MISSING_TRANSLATION_FILE_CONTENT); + + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); + + expect(result?.success).toBeTrue(); + expect(logs).toContain( + jasmine.objectContaining({ + level: 'warn', + message: jasmine.stringMatching('No translation found for'), + }), + ); + }); + + it('should error when i18nMissingTranslation is set to error', async () => { + harness.useTarget('build', { + ...BASE_OPTIONS, + localize: true, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + i18nMissingTranslation: 'error' as any, + }); + + await harness.writeFile( + 'src/app/app.component.html', + ` +Hello {{ title }}!
+ `, + ); + + await harness.writeFile('src/locales/messages.fr.xlf', MISSING_TRANSLATION_FILE_CONTENT); + + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); + + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + level: 'error', + message: jasmine.stringMatching('No translation found for'), + }), + ); + }); + + it('should not error or warn when i18nMissingTranslation is set to ignore', async () => { + harness.useTarget('build', { + ...BASE_OPTIONS, + localize: true, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + i18nMissingTranslation: 'ignore' as any, + }); + + await harness.writeFile( + 'src/app/app.component.html', + ` +Hello {{ title }}!
+ `, + ); + + await harness.writeFile('src/locales/messages.fr.xlf', MISSING_TRANSLATION_FILE_CONTENT); + + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); + + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('No translation found for'), + }), + ); + }); + + it('should not error or warn when i18nMissingTranslation is set to error and all found', async () => { + harness.useTarget('build', { + ...BASE_OPTIONS, + localize: true, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + i18nMissingTranslation: 'error' as any, + }); + + await harness.writeFile( + 'src/app/app.component.html', + ` +Hello {{ title }}!
+ `, + ); + + await harness.writeFile('src/locales/messages.fr.xlf', GOOD_TRANSLATION_FILE_CONTENT); + + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); + + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('No translation found for'), + }), + ); + }); + + it('should not error or warn when i18nMissingTranslation is set to warning and all found', async () => { + harness.useTarget('build', { + ...BASE_OPTIONS, + localize: true, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + i18nMissingTranslation: 'warning' as any, + }); + + await harness.writeFile( + 'src/app/app.component.html', + ` +Hello {{ title }}!
+ `, + ); + + await harness.writeFile('src/locales/messages.fr.xlf', GOOD_TRANSLATION_FILE_CONTENT); + + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); + + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('No translation found for'), + }), + ); + }); + }); +}); + +const GOOD_TRANSLATION_FILE_CONTENT = ` + +