7
7
*/
8
8
import * as fs from 'fs' ;
9
9
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
+ } ;
11
18
12
19
export async function createTranslationLoader ( ) : Promise < TranslationLoader > {
13
- const parsers = await importParsers ( ) ;
20
+ const { parsers, diagnostics } = await importParsers ( ) ;
14
21
15
22
return ( path : string ) => {
16
23
const content = fs . readFileSync ( path , 'utf8' ) ;
17
24
18
25
for ( const [ format , parser ] of Object . entries ( parsers ) ) {
19
26
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 } ;
21
30
}
22
31
}
23
32
@@ -27,8 +36,11 @@ export async function createTranslationLoader(): Promise<TranslationLoader> {
27
36
28
37
async function importParsers ( ) {
29
38
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 = {
32
44
json : new ( await import (
33
45
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
34
46
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser'
@@ -41,25 +53,17 @@ async function importParsers() {
41
53
// tslint:disable-next-line:trailing-comma no-implicit-dependencies
42
54
'@angular/localize/src/tools/src/translate/translation_files/translation_parsers/xliff2_translation_parser'
43
55
) ) . 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 (
60
58
// 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 ) ,
63
61
} ;
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
+ ) ;
64
68
}
65
69
}
0 commit comments