-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
279 lines (261 loc) · 8.62 KB
/
index.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import type { Config, EnvironmentFunctions } from "@inlang/core/config";
import type * as ast from "@inlang/core/ast";
import gettextParser, { GetTextTranslation } from "gettext-parser";
import { query } from "@inlang/core/query";
/**
* The plugin configuration.
*/
export type PluginConfig = {
/**
* Defines the path pattern for the resources.
*
* Must include the `{language}` placeholder.
*
* @example
* "./resources/{language}.json"
*/
pathPattern: string;
referenceResourcePath: string | null;
};
export async function getLanguages(
// merging the first argument from config (which contains all arguments)
// with the custom pluginConfig argument
args: EnvironmentFunctions & {
pluginConfig: PluginConfig;
referenceLanguage: string;
}
) {
// splitting a path like "resources/{language}.po" will always return an array with two elements
// namely before and after {language}
const [pathBeforeLanguage, pathAfterLanguage] =
args.pluginConfig.pathPattern.split("{language}");
const pathAfterLanguageisDirectory = pathAfterLanguage.startsWith("/");
// letzte index of / //+1 so that '/' is not taken away from the Path
const paths = await args.$fs.readdir(pathBeforeLanguage);
//
const languages = args.pluginConfig.referenceResourcePath
? []
: [args.referenceLanguage];
for (const language of paths) {
if (pathAfterLanguageisDirectory) {
const files = await args.$fs.readdir(
pathBeforeLanguage +
language +
pathAfterLanguage.substring(0, pathAfterLanguage.lastIndexOf("/") + 1)
);
// somtime are more than 1 file in the folder example: messages.mo and messages.po
for (const file of files) {
if (typeof file === "string" && file.endsWith(".po")) {
//if the
languages.push(language as string);
}
}
} else {
if (typeof language === "string" && language.endsWith(".po")) {
languages.push(language.replace(".po", ""));
} else if (typeof language === "string" && language.endsWith(".pot")) {
languages.push(language.replace(".pot", ""));
}
}
}
return languages;
}
/**
* Reading resources.
*
* The function merges the args from Config['readResources'] with the pluginConfig
* and EnvironmentFunctions.
*/
export async function readResources(
// merging the first argument from config (which contains all arguments)
// with the custom pluginConfig argument
args: Parameters<Config["readResources"]>[0] &
EnvironmentFunctions & {
pluginConfig: PluginConfig;
}
): ReturnType<Config["readResources"]> {
const resources: ast.Resource[] = [];
// Action: this .filter funciton in the forEach expression , filters the referenceLanguage from the array of all languages retrieved by the getLanguages function.
// Reason: because it could be that the reference language is a ".pot" file instead of a ".po" file and we do not want to overwrite a ".pot" or the reference language,
// because the "msgid" in this file is in most cases the ID for all other ".po" files.
for (const language of args.config.languages.filter(
(lang) => lang !== args.config.referenceLanguage
)) {
// Action: replace the word {language} witch could means the languagecode from pathPattern with the languageCode
// Reason: each language is saved in its own file or folder
const resourcePath = args.pluginConfig.pathPattern.replace(
"{language}",
language
);
const poFile = gettextParser.po.parse(
(await args.$fs.readFile(resourcePath, "utf-8")) as string
);
resources.push(parseResource(poFile, language));
}
// Action: if a resource file exists, it will be read and processed in a usual way
// Reason: split off from the others because of the overwriting problem described above.
if (args.pluginConfig.referenceResourcePath) {
const poFile = gettextParser.po.parse(
(await args.$fs.readFile(
args.pluginConfig.referenceResourcePath,
"utf-8"
)) as string
);
resources.push(parseResource(poFile, args.config.referenceLanguage));
}
// Action: Create a ".pot" file if no ".pot" file exists. The ".pot" file would be created from all ".po" files found.
// This ".pot" file only exists for inlang and is not committed to the project/repo.
// Reason: Ensure that all "msgID"s will are found and if nessesary created missing "msgID"s in other ".po"files
else {
// drop "msgID" duplicates with set and create a ".pot" file without duplicate IDs
const ids = [
...new Set(
resources.flatMap((resource) => query(resource).includedMessageIds())
),
];
const potFile: gettextParser.GetTextTranslations = {
headers: { header: "" },
charset: "",
translations: {
[""]: Object.fromEntries(
ids.map((id) => [
id,
{
msgid: id,
msgstr: [id],
},
])
),
},
};
resources.push(parseResource(potFile, args.config.referenceLanguage));
}
return resources;
}
/**
* Writing resources.
*
* The function merges the args from Config['readResources'] with the pluginConfig
* and EnvironmentFunctions.
*/
export async function writeResources(
args: Parameters<Config["writeResources"]>[0] &
EnvironmentFunctions & { pluginConfig: PluginConfig }
): ReturnType<Config["writeResources"]> {
for (const resource of args.resources) {
// ?? This line avoid the write on a pot file
// Action: prevent the user from rewriting a pot file
// Reason: pot Contains the keys for each msgstr
// if (
// args.pluginConfig.referenceResourcePath === null &&
// resource.languageTag.name === args.config.referenceLanguage
// ) {
// continue;
// }
// Action:if a referenceResourcePath is defined, the path differs. Thus, take path from plugin config.
// Reason: referenceResourcePath is defined or null and we need to filter the defineded referenceResourcePath
const resourcePath =
resource.languageTag.name === args.config.referenceLanguage
? args.pluginConfig.referenceResourcePath
: args.pluginConfig.pathPattern.replace(
"{language}",
resource.languageTag.name
);
const poFile = serializeResource(resource);
const text = gettextParser.po.compile(poFile);
if (resourcePath) {
await args.$fs.writeFile(resourcePath, text, { encoding: "utf-8" });
}
}
}
/**
* Parses a resource.
*
* @example
* parseResource({ "test": "Hello world" }, "en")
*/
function parseResource(
poFile: gettextParser.GetTextTranslations,
language: string
): ast.Resource {
return {
metadata: {
headers: poFile.headers,
charset: poFile.charset,
},
type: "Resource",
languageTag: {
type: "LanguageTag",
name: language,
},
body: Object.values(poFile.translations[""])
.filter((translation) => translation.msgid !== "")
.map((translation) => parseMessage(translation)),
};
}
/**
* Parses a message.
*
* @example
* parseMessage("test", "Hello world")
*/
function parseMessage(
translation: gettextParser.GetTextTranslation
): ast.Message {
return {
type: "Message",
metadata: {
comments: translation.comments,
},
id: {
type: "Identifier",
name: translation.msgid,
},
pattern: {
type: "Pattern",
elements: [{ type: "Text", value: translation.msgstr[0] }],
},
};
}
/**
* Serializes a resource.
*
* The function unflattens, and therefore reverses the flattening
* in parseResource, of a given object. The result is a stringified JSON
* that is beautified by adding (null, 2) to the arguments.
*
* @example
* serializeResource(resource)
*/
function serializeResource(
resource: ast.Resource
): gettextParser.GetTextTranslations {
return {
headers: (resource.metadata as any)
.headers as gettextParser.GetTextTranslations["headers"],
charset: (resource.metadata as any)
.charset as gettextParser.GetTextTranslations["charset"],
translations: {
"": Object.fromEntries(
resource.body.map((message) => {
return [message.id.name, serializeMessage(message)];
})
),
},
};
}
/**
* Serializes a message.
*
* Note that only the first element of the pattern is used as inlang, as of v0.3,
* does not support more than 1 element in a pattern.
*/
function serializeMessage(
message: ast.Message
): gettextParser.GetTextTranslation {
return {
comments: message.metadata?.comments as gettextParser.GetTextComment,
msgid: message.id.name,
msgstr: [message.pattern.elements[0].value],
};
}