Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure source locale data is inje…
Browse files Browse the repository at this point in the history
…cted when localizing

Fixes angular#16389
  • Loading branch information
clydin committed Dec 9, 2019
1 parent b6863b6 commit 6f76d74
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
60 changes: 33 additions & 27 deletions packages/angular_devkit/build_angular/src/utils/i18n-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,40 +202,46 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
const loader = await createTranslationLoader();
const usedFormats = new Set<string>();
for (const [locale, desc] of Object.entries(i18n.locales)) {
if (i18n.inlineLocales.has(locale) && desc.file) {
const result = loader(path.join(context.workspaceRoot, desc.file));

for (const diagnostics of result.diagnostics.messages) {
if (diagnostics.type === 'error') {
throw new Error(
`Error parsing translation file '${desc.file}': ${diagnostics.message}`,
);
} else {
context.logger.warn(`WARNING [${desc.file}]: ${diagnostics.message}`);
}
}
if (!i18n.inlineLocales.has(locale)) {
continue;
}

usedFormats.add(result.format);
if (usedFormats.size > 1 && tsConfig.options.enableI18nLegacyMessageIdFormat !== false) {
// This limitation is only for legacy message id support (defaults to true as of 9.0)
throw new Error(
'Localization currently only supports using one type of translation file format for the entire application.',
);
}
const localeDataPath = findLocaleDataPath(locale, localeDataBasePath);
if (!localeDataPath) {
context.logger.warn(
`Locale data for '${locale}' cannot be found. No locale data will be included for this locale.`,
);
} else {
desc.dataPath = localeDataPath;
}

desc.format = result.format;
desc.translation = result.translation;
desc.integrity = result.integrity;
if (!desc.file) {
continue;
}

const result = loader(path.join(context.workspaceRoot, desc.file));

const localeDataPath = findLocaleDataPath(locale, localeDataBasePath);
if (!localeDataPath) {
context.logger.warn(
`Locale data for '${locale}' cannot be found. No locale data will be included for this locale.`,
for (const diagnostics of result.diagnostics.messages) {
if (diagnostics.type === 'error') {
throw new Error(
`Error parsing translation file '${desc.file}': ${diagnostics.message}`,
);
} else {
desc.dataPath = localeDataPath;
context.logger.warn(`WARNING [${desc.file}]: ${diagnostics.message}`);
}
}

usedFormats.add(result.format);
if (usedFormats.size > 1 && tsConfig.options.enableI18nLegacyMessageIdFormat !== false) {
// This limitation is only for legacy message id support (defaults to true as of 9.0)
throw new Error(
'Localization currently only supports using one type of translation file format for the entire application.',
);
}

desc.format = result.format;
desc.translation = result.translation;
desc.integrity = result.integrity;
}

// Legacy message id's require the format of the translations
Expand Down
46 changes: 46 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-sourcelocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright Google Inc. 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 { expectFileToMatch } from '../../utils/fs';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { externalServer, langTranslations, setupI18nConfig } from './legacy';

export default async function() {
// Setup i18n tests and config.
await setupI18nConfig(true);

// Update angular.json
await updateJsonFile('angular.json', workspaceJson => {
const appProject = workspaceJson.projects['test-project'];
// tslint:disable-next-line: no-any
const i18n: Record<string, any> = appProject.i18n;

i18n.sourceLocale = 'fr';

delete i18n.locales['fr'];
});

// Build each locale and verify the output.
await ng('build');
for (const { lang, outputPath } of langTranslations) {
// does not exist in this test due to the source locale change
if (lang === 'en-US') {
continue;
}

await expectFileToMatch(`${outputPath}/vendor-es5.js`, lang);
await expectFileToMatch(`${outputPath}/vendor-es2015.js`, lang);

// Verify the locale data is registered using the global files
await expectFileToMatch(`${outputPath}/vendor-es5.js`, '.ng.common.locales');
await expectFileToMatch(`${outputPath}/vendor-es2015.js`, '.ng.common.locales');

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);
}
}

0 comments on commit 6f76d74

Please sign in to comment.