77 */
88import * as fs from 'fs' ;
99
10- export type TranslationLoader = ( path : string ) => { translation : unknown ; format : string } ;
10+ export type TranslationLoader = (
11+ path : string ,
12+ ) => {
13+ translation : unknown ;
14+ format : string ;
15+ // tslint:disable-next-line: no-implicit-dependencies
16+ diagnostics : import ( '@angular/localize/src/tools/src/diagnostics' ) . Diagnostics ;
17+ } ;
1118
1219export async function createTranslationLoader ( ) : Promise < TranslationLoader > {
13- const parsers = await importParsers ( ) ;
20+ const { parsers, diagnostics } = await importParsers ( ) ;
1421
1522 return ( path : string ) => {
1623 const content = fs . readFileSync ( path , 'utf8' ) ;
1724
1825 for ( const [ format , parser ] of Object . entries ( parsers ) ) {
1926 if ( parser . canParse ( path , content ) ) {
20- return { format, translation : parser . parse ( path , content ) . translations } ;
27+ const result = parser . parse ( path , content ) ;
28+
29+ return { format, translation : result . translations , diagnostics } ;
2130 }
2231 }
2332
@@ -27,8 +36,11 @@ export async function createTranslationLoader(): Promise<TranslationLoader> {
2736
2837async function importParsers ( ) {
2938 try {
30- // In @angular /localize version 9.0.0-next.15 the parsers were located in the below locations.
31- return {
39+ // tslint:disable-next-line: no-implicit-dependencies
40+ const localizeDiag = await import ( '@angular/localize/src/tools/src/diagnostics' ) ;
41+ const diagnostics = new localizeDiag . Diagnostics ( ) ;
42+
43+ const parsers = {
3244 json : new ( await import (
3345 // tslint:disable-next-line:trailing-comma no-implicit-dependencies
3446 '@angular/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser'
@@ -41,25 +53,17 @@ async function importParsers() {
4153 // tslint:disable-next-line:trailing-comma no-implicit-dependencies
4254 '@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser'
4355 ) ) . Xliff2TranslationParser ( ) ,
44- } ;
45- } catch {
46- // Prior to @angular /localize version 9.0.0-next.15 the parsers were located in the below locations.
47- return {
48- json : new ( await import (
49- // @ts -ignore
50- // tslint:disable-next-line:trailing-comma no-implicit-dependencies
51- '@angular/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json/simple_json_translation_parser'
52- ) ) . SimpleJsonTranslationParser ( ) ,
53- xlf : new ( await import (
54- // @ts -ignore
55- // tslint:disable-next-line:trailing-comma no-implicit-dependencies
56- '@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff1/xliff1_translation_parser'
57- ) ) . Xliff1TranslationParser ( ) ,
58- xlf2 : new ( await import (
59- // @ts -ignore
56+ // The name ('xmb') needs to match the AOT compiler option
57+ xmb : new ( await import (
6058 // tslint:disable-next-line:trailing-comma no-implicit-dependencies
61- '@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff2/xliff2_translation_parser '
62- ) ) . Xliff2TranslationParser ( ) ,
59+ '@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xtb_translation_parser '
60+ ) ) . XtbTranslationParser ( diagnostics ) ,
6361 } ;
62+
63+ return { parsers, diagnostics } ;
64+ } catch {
65+ throw new Error (
66+ `Unable to load translation file parsers. Please ensure '@angular/localize' is installed.` ,
67+ ) ;
6468 }
6569}
0 commit comments