Skip to content

Commit f907f13

Browse files
petebacondarwinfilipesilva
authored andcommitted
refactor(@angular-devkit/build-angular): display translation-file diagnostics
If no translation parsers could parse a translation-file we displayed a generic error, which makes it difficult to track down if there is a problem with the file. angular/angular#37909 introduces a new `parser.analyze()` API that allows us to get hold of the diagnostic messages from trying to parse the translation-files. This commit will render these messages if none of the translation parsers are succcesful. Note that in order to maintain compatibility with versions of `@angular/localize` that do not have the `analyze()` method, the commit includes a polyfill, which can be removed after a release where we can guarantee that the method will be available.
1 parent a14c80a commit f907f13

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/angular_devkit/build_angular/src/utils/load-translations.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,37 @@ export async function createTranslationLoader(): Promise<TranslationLoader> {
2424
return (path: string) => {
2525
const content = fs.readFileSync(path, 'utf8');
2626

27+
const unusedParsers = new Map();
2728
for (const [format, parser] of Object.entries(parsers)) {
28-
if (parser.canParse(path, content)) {
29-
const result = parser.parse(path, content);
29+
const analysis = analyze(parser, path, content);
30+
if (analysis.canParse) {
31+
const translationBundle = parser.parse(path, content, analysis.hint);
3032
const integrity = 'sha256-' + createHash('sha256').update(content).digest('base64');
3133

32-
return { format, translation: result.translations, diagnostics, integrity };
34+
return { format, translation: translationBundle.translations, diagnostics, integrity };
35+
} else {
36+
unusedParsers.set(parser, analysis);
3337
}
3438
}
3539

36-
throw new Error('Unsupported translation file format.');
40+
const messages: string[] = [];
41+
for (const [parser, analysis] of unusedParsers.entries()) {
42+
messages.push(analysis.diagnostics.formatDiagnostics(`*** ${parser.constructor.name} ***`));
43+
}
44+
throw new Error(`Unsupported translation file format in ${path}. The following parsers were tried:\n` + messages.join('\n'));
3745
};
46+
47+
// TODO: `parser.canParse()` is deprecated; remove this polyfill once we are sure all parsers provide the `parser.analyze()` method.
48+
// tslint:disable-next-line: no-any
49+
function analyze(parser: any, path: string, content: string) {
50+
if (parser.analyze !== undefined) {
51+
return parser.analyze(path, content);
52+
} else {
53+
const hint = parser.canParse(path, content);
54+
55+
return {canParse: hint !== false, hint, diagnostics};
56+
}
57+
}
3858
}
3959

4060
async function importParsers() {

0 commit comments

Comments
 (0)