1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.createTranslationLoader = void 0;
|
---|
30 | const crypto_1 = require("crypto");
|
---|
31 | const fs = __importStar(require("fs"));
|
---|
32 | async function createTranslationLoader() {
|
---|
33 | const { parsers, diagnostics } = await importParsers();
|
---|
34 | return (path) => {
|
---|
35 | const content = fs.readFileSync(path, 'utf8');
|
---|
36 | const unusedParsers = new Map();
|
---|
37 | for (const [format, parser] of Object.entries(parsers)) {
|
---|
38 | const analysis = analyze(parser, path, content);
|
---|
39 | if (analysis.canParse) {
|
---|
40 | const { locale, translations } = parser.parse(path, content, analysis.hint);
|
---|
41 | const integrity = 'sha256-' + crypto_1.createHash('sha256').update(content).digest('base64');
|
---|
42 | return { format, locale, translations, diagnostics, integrity };
|
---|
43 | }
|
---|
44 | else {
|
---|
45 | unusedParsers.set(parser, analysis);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | const messages = [];
|
---|
49 | for (const [parser, analysis] of unusedParsers.entries()) {
|
---|
50 | messages.push(analysis.diagnostics.formatDiagnostics(`*** ${parser.constructor.name} ***`));
|
---|
51 | }
|
---|
52 | throw new Error(`Unsupported translation file format in ${path}. The following parsers were tried:\n` +
|
---|
53 | messages.join('\n'));
|
---|
54 | };
|
---|
55 | // TODO: `parser.canParse()` is deprecated; remove this polyfill once we are sure all parsers provide the `parser.analyze()` method.
|
---|
56 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
57 | function analyze(parser, path, content) {
|
---|
58 | if (parser.analyze !== undefined) {
|
---|
59 | return parser.analyze(path, content);
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | const hint = parser.canParse(path, content);
|
---|
63 | return { canParse: hint !== false, hint, diagnostics };
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | exports.createTranslationLoader = createTranslationLoader;
|
---|
68 | async function importParsers() {
|
---|
69 | try {
|
---|
70 | const localizeDiag = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/diagnostics')));
|
---|
71 | const diagnostics = new localizeDiag.Diagnostics();
|
---|
72 | const parsers = {
|
---|
73 | arb: new (await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/translate/translation_files/translation_parsers/arb_translation_parser')))).ArbTranslationParser(),
|
---|
74 | json: new (await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser')))).SimpleJsonTranslationParser(),
|
---|
75 | xlf: new (await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser')))).Xliff1TranslationParser(),
|
---|
76 | xlf2: new (await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser')))).Xliff2TranslationParser(),
|
---|
77 | // The name ('xmb') needs to match the AOT compiler option
|
---|
78 | xmb: new (await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xtb_translation_parser')))).XtbTranslationParser(),
|
---|
79 | };
|
---|
80 | return { parsers, diagnostics };
|
---|
81 | }
|
---|
82 | catch {
|
---|
83 | throw new Error(`Unable to load translation file parsers. Please ensure '@angular/localize' is installed.`);
|
---|
84 | }
|
---|
85 | }
|
---|