-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.ts
48 lines (46 loc) · 1.41 KB
/
maps.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
import { expandGlob, xmlParse as parse } from "./deps.ts";
export async function parseStrings(
path: string,
): Promise<Record<string, string>> {
const strings: Record<string, string> = {};
for await (const file of expandGlob(`${path}/**/*.xml`)) {
const content = await Deno.readTextFile(file.path);
const xml = parse(content).resources as any;
if (!xml || !xml.hasOwnProperty("string")) {
continue;
}
for (const n of xml.string) {
strings[`@string/${n["@name"]}`] = n["#text"];
}
}
return strings;
}
export async function parseStringArrays(
path: string,
strings: Record<string, string>,
): Promise<Record<string, string[]>> {
const arrays: Record<string, string[]> = {};
for await (const file of expandGlob(`${path}/**/*.xml`)) {
const content = await Deno.readTextFile(file.path);
const xml = parse(content).resources as any;
if (!xml || !xml.hasOwnProperty("string-array")) {
continue;
}
for (const n of xml["string-array"]) {
const key = `@array/${n["@name"]}`;
arrays[key] = [];
for (const i of (n["item"] as string[])) {
try {
if ((i + "").startsWith("@string/")) {
arrays[key].push(strings[i]);
} else {
arrays[key].push(i);
}
} catch (err) {
throw new Error("Failed to parse " + i + " - " + err.message);
}
}
}
}
return arrays;
}