/
translation_file_loader.ts
34 lines (32 loc) 路 1.19 KB
/
translation_file_loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileUtils} from '../../file_utils';
import {TranslationBundle} from '../translator';
import {TranslationParser} from './translation_parsers/translation_parser';
/**
* Use this class to load a collection of translation files from disk.
*/
export class TranslationLoader {
constructor(private translationParsers: TranslationParser[]) {}
/**
* Load and parse the translation files into a collection of `TranslationBundles`.
*
* @param translationFilePaths A collection of absolute paths to the translation files.
*/
loadBundles(translationFilePaths: string[]): TranslationBundle[] {
return translationFilePaths.map(filePath => {
const fileContents = FileUtils.readFile(filePath);
for (const translationParser of this.translationParsers) {
if (translationParser.canParse(filePath, fileContents)) {
return translationParser.parse(filePath, fileContents);
}
}
throw new Error(`Unable to parse translation file: ${filePath}`);
});
}
}