Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): support XMB/XTB translation file …
Browse files Browse the repository at this point in the history
…parser

Closes #16100
  • Loading branch information
clydin authored and mgechev committed Nov 12, 2019
1 parent 74ca6a2 commit 2a5d051
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
10 changes: 10 additions & 0 deletions packages/angular_devkit/build_angular/src/utils/i18n-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
if (i18n.inlineLocales.has(locale) && desc.file) {
const result = loader(path.join(projectRoot, 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}`);
}
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@
*/
import * as fs from 'fs';

export type TranslationLoader = (path: string) => { translation: unknown; format: string };
export type TranslationLoader = (
path: string,
) => {
translation: unknown;
format: string;
// tslint:disable-next-line: no-implicit-dependencies
diagnostics: import('@angular/localize/src/tools/src/diagnostics').Diagnostics;
};

export async function createTranslationLoader(): Promise<TranslationLoader> {
const parsers = await importParsers();
const { parsers, diagnostics } = await importParsers();

return (path: string) => {
const content = fs.readFileSync(path, 'utf8');

for (const [format, parser] of Object.entries(parsers)) {
if (parser.canParse(path, content)) {
return { format, translation: parser.parse(path, content).translations };
const result = parser.parse(path, content);

return { format, translation: result.translations, diagnostics };
}
}

Expand All @@ -27,8 +36,11 @@ export async function createTranslationLoader(): Promise<TranslationLoader> {

async function importParsers() {
try {
// In @angular/localize version 9.0.0-next.15 the parsers were located in the below locations.
return {
// tslint:disable-next-line: no-implicit-dependencies
const localizeDiag = await import('@angular/localize/src/tools/src/diagnostics');
const diagnostics = new localizeDiag.Diagnostics();

const parsers = {
json: new (await import(
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser'
Expand All @@ -41,25 +53,17 @@ async function importParsers() {
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser'
)).Xliff2TranslationParser(),
};
} catch {
// Prior to @angular/localize version 9.0.0-next.15 the parsers were located in the below locations.
return {
json: new (await import(
// @ts-ignore
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json/simple_json_translation_parser'
)).SimpleJsonTranslationParser(),
xlf: new (await import(
// @ts-ignore
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff1/xliff1_translation_parser'
)).Xliff1TranslationParser(),
xlf2: new (await import(
// @ts-ignore
// The name ('xmb') needs to match the AOT compiler option
xmb: new (await import(
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff2/xliff2_translation_parser'
)).Xliff2TranslationParser(),
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xtb_translation_parser'
)).XtbTranslationParser(diagnostics),
};

return { parsers, diagnostics };
} catch {
throw new Error(
`Unable to load translation file parsers. Please ensure '@angular/localize' is installed.`,
);
}
}

0 comments on commit 2a5d051

Please sign in to comment.