-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
simple_json_translation_parser.ts
82 lines (78 loc) 路 2.86 KB
/
simple_json_translation_parser.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @license
* Copyright Google LLC 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 {傻MessageId, 傻ParsedTranslation, 傻parseTranslation} from '@angular/localize';
import {extname} from 'path';
import {Diagnostics} from '../../../diagnostics';
import {ParseAnalysis, ParsedTranslationBundle, TranslationParser} from './translation_parser';
/**
* A translation parser that can parse JSON that has the form:
*
* ```
* {
* "locale": "...",
* "translations": {
* "message-id": "Target message string",
* ...
* }
* }
* ```
*
* @see SimpleJsonTranslationSerializer
* @publicApi used by CLI
*/
export class SimpleJsonTranslationParser implements TranslationParser<Object> {
/**
* @deprecated
*/
canParse(filePath: string, contents: string): Object|false {
const result = this.analyze(filePath, contents);
return result.canParse && result.hint;
}
analyze(filePath: string, contents: string): ParseAnalysis<Object> {
const diagnostics = new Diagnostics();
// For this to be parsable, the extension must be `.json` and the contents must include "locale"
// and "translations" keys.
if (extname(filePath) !== '.json' ||
!(contents.includes('"locale"') && contents.includes('"translations"'))) {
diagnostics.warn('File does not have .json extension.');
return {canParse: false, diagnostics};
}
try {
const json = JSON.parse(contents);
if (json.locale === undefined) {
diagnostics.warn('Required "locale" property missing.');
return {canParse: false, diagnostics};
}
if (typeof json.locale !== 'string') {
diagnostics.warn('The "locale" property is not a string.');
return {canParse: false, diagnostics};
}
if (json.translations === undefined) {
diagnostics.warn('Required "translations" property missing.');
return {canParse: false, diagnostics};
}
if (typeof json.translations !== 'object') {
diagnostics.warn('The "translations" is not an object.');
return {canParse: false, diagnostics};
}
return {canParse: true, diagnostics, hint: json};
} catch (e) {
diagnostics.warn('File is not valid JSON.');
return {canParse: false, diagnostics};
}
}
parse(_filePath: string, contents: string, json?: Object): ParsedTranslationBundle {
const {locale: parsedLocale, translations} = json || JSON.parse(contents);
const parsedTranslations: Record<傻MessageId, 傻ParsedTranslation> = {};
for (const messageId in translations) {
const targetMessage = translations[messageId];
parsedTranslations[messageId] = 傻parseTranslation(targetMessage);
}
return {locale: parsedLocale, translations: parsedTranslations, diagnostics: new Diagnostics()};
}
}