-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchTranslations.ts
More file actions
34 lines (31 loc) · 965 Bytes
/
fetchTranslations.ts
File metadata and controls
34 lines (31 loc) · 965 Bytes
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
import * as dotenv from "dotenv";
dotenv.config({ path: ".env.local" });
import { writeFile } from "fs/promises";
import createClient from "./lib/cmsClient";
import { readItems } from "@directus/sdk";
export async function fetchTranslations() {
const client = createClient();
try {
const translations = await client.request(readItems("Translation"));
// go from [{key: "hello", fi: "Hei", sv: "Hej", en: "Hello"}] to {"hello": {fi: "Hei", sv: "Hej", en: "Hello"}}
const mappedTranslations = translations.reduce(
(map, translation) => ({
...map,
[translation.key]: {
fi: translation.fi,
en: translation.en,
sv: translation.sv,
},
}),
{},
);
await writeFile(
"./hooks/translations.json",
JSON.stringify(mappedTranslations),
);
console.log("Downloaded and saved translations");
} catch (error) {
console.log(error);
}
}
fetchTranslations();