-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.ts
195 lines (171 loc) · 5.91 KB
/
convert.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
I18NEntry,
SingleI18NEntry,
PluralI18NEntry,
TranslationJson,
TranslationMeta
} from 'i18n-proto';
export type InitialMeta = {
copyrightSubject?: string,
bugsEmail?: string,
year?: number
};
export function getTzOffset(date: Date) {
const timezoneShift = date.getTimezoneOffset() / -60;
let tz = 'Z';
if (timezoneShift !== 0) {
tz = (timezoneShift > 0 ? '+' : '-') +
(timezoneShift > 9 ? '' : '0')
+ timezoneShift + '00';
}
return tz;
}
export function makeDate(date: Date) {
return date.getFullYear() + '-' +
((date.getMonth() + 1) > 9 ? '' : '0') + (date.getMonth() + 1) + '-' +
(date.getDate() > 9 ? '' : '0') + date.getDate() + ' ' +
(date.getHours() > 9 ? '' : '0') + date.getHours() + ':' +
(date.getMinutes() > 9 ? '' : '0') + date.getMinutes() +
getTzOffset(date);
}
export function makePoHeader({ meta, initialMeta, genDate, hasPluralForms }: {
meta?: TranslationMeta,
initialMeta: InitialMeta,
genDate: string,
hasPluralForms: boolean
}): string {
if (!meta) {
// make POT, use initial meta
return `# Translations template for PROJECT.
# Copyright (C) ${initialMeta.year} ${initialMeta.copyrightSubject}
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, ${initialMeta.year}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\\n"
"Report-Msgid-Bugs-To: ${initialMeta.bugsEmail}\\n"
"POT-Creation-Date: ${genDate}\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language-Team: LANGUAGE <[email protected]>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Generated-By: i18n-json2po\\n"
`;
} else {
// have meta - make po!
let headers = {
projectIdVersion: (v) => `Project-Id-Version: ${v}\n`,
reportMsgidBugsTo: (v) => `Report-Msgid-Bugs-To: ${v}\n`,
potCreationDate: (v) => `POT-Creation-Date: ${v}\n`,
poRevisionDate: (v) => `PO-Revision-Date: ${v}\n`,
lastTranslator: (v) => `Last-Translator: ${v.name} <${v.email}>\n`,
languageTeam: (v) => `Language-Team: ${v}\n`,
mimeVersion: (v) => `MIME-Version: ${v}\n`,
contentType: (v) => `Content-Type: ${v}\n`,
contentTransferEncoding: (v) => `Content-Transfer-Encoding: ${v}\n`,
generatedBy: (v) => `Generated-By: ${v}\n`,
language: (v) => `Language: ${v}\n`,
pluralForms: (v) => `Plural-Forms: ${v}\n`,
};
let items = [
'msgid ""',
'msgstr ""',
];
let pluralFormsHeaderFound = false;
for (let name in meta) {
if (name === 'pluralForms') {
pluralFormsHeaderFound = true;
}
items.push(JSON.stringify(headers[name](meta[name])));
}
if (hasPluralForms && !pluralFormsHeaderFound) {
throw new Error('Translation has some plural forms, but Plural-Forms header was not found');
}
return items.join("\n") + "\n\n"; // additional CRLFs to separated header
}
}
export function convert(json: string, initialMeta: InitialMeta | undefined, printOccurences: boolean): string {
const document: TranslationJson = JSON.parse(json);
let poEntries: PotEntry[] = [];
let hasPluralForms = false;
for (let item of document.items) {
let potEntry = new PotEntry();
if (item.type === 'single') {
potEntry.parseSingleEntry(item, printOccurences, !!document.meta);
}
if (item.type === 'plural') {
potEntry.parsePluralEntry(item, printOccurences, !!document.meta);
hasPluralForms = true;
}
poEntries.push(potEntry);
}
return makePoHeader({
meta: document.meta,
initialMeta,
genDate: makeDate(new Date()),
hasPluralForms
}) + poEntries.map((entry) => entry.asString()).join("\n\n");
}
export class PotEntry {
private items: string[];
protected addComment = (comment: string) => this.items.push('#. ' + comment);
protected addOccurence = (occ: string) => this.items.push('#: ' + occ);
protected addContext = (context: string) => this.items.push('msgctxt ' + JSON.stringify(context));
protected addMsgid = (id: string) => this.items.push('msgid ' + JSON.stringify(id));
protected addMsgidPlural = (id: string) => this.items.push('msgid_plural ' + JSON.stringify(id));
protected addMsgstr = (translation: string = '') => this.items.push('msgstr ' + JSON.stringify(translation));
protected addMsgstrPlural = (translations: string[]) => {
if (!translations.length) { // 2 empty translations by default
this.items.push('msgstr[0] ""');
this.items.push('msgstr[1] ""');
} else {
translations.forEach((val, index) => this.items.push('msgstr[' + index + '] ' + JSON.stringify(val)));
}
};
public asString = () => this.items.join("\n");
public parseSingleEntry(
{ entry, comments, occurences, context, type, translation }: SingleI18NEntry,
printOccurences: boolean,
includeTranslations: boolean
): PotEntry {
this.items = [];
if (comments) {
comments.forEach(this.addComment);
}
if (occurences && printOccurences) {
occurences.forEach(this.addOccurence);
}
if (context) {
this.addContext(context);
}
this.addMsgid(entry);
this.addMsgstr(includeTranslations ? translation : '');
return this;
}
public parsePluralEntry(
{ entry, comments, occurences, context, type, translations }: PluralI18NEntry,
printOccurences: boolean,
includeTranslations: boolean
): PotEntry {
this.items = [];
if (comments) {
comments.forEach(this.addComment);
}
if (occurences && printOccurences) {
occurences.forEach(this.addOccurence);
}
if (context) {
this.addContext(context);
}
this.addMsgid(entry[0]);
// extracted original entries contain only first and
// last plurals forms, which identify the entry
this.addMsgidPlural(entry[1]);
this.addMsgstrPlural(includeTranslations ? translations : []);
return this;
}
}