This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (48 loc) · 1.54 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
import { cosmicSync } from "@anandchowdhary/cosmic";
import axios from "axios";
import { ensureDir, fstat, writeFile } from "fs-extra";
import { join } from "path";
const config = cosmicSync("i18n");
const sheet = config.sheet || 1;
export const i18n = async () => {
console.log("Starting...");
const { data } = await axios.get(
`https://spreadsheets.google.com/feeds/list/${config.sheetId}/${sheet}/public/values?alt=json`
);
console.log("Fetched data");
const rows: any[] = [];
if (data && data.feed && data.feed.entry) {
console.log(data.feed.entry);
for (let i = 0; i < data.feed.entry.length; i++) {
const entry = data.feed.entry[i];
const keys = Object.keys(entry);
const newRow: any = {};
for (let j = 0; j < keys.length; j++) {
const gsxCheck = keys[j].indexOf("gsx$");
if (gsxCheck > -1) {
const key = keys[j];
const name = key.substring(4);
const content = entry[key];
const value = content.$t;
newRow[name] = value;
}
}
rows.push(newRow);
}
}
await ensureDir(join(".", "locales"));
const languages = Object.keys(rows[0]).filter(
(key) => key.toLocaleLowerCase().trim() !== "key"
);
for await (const language of languages) {
const content: any = {};
rows.forEach((row) => (content[row.key] = row[language]));
await writeFile(
join(".", "locales", `${language}.json`),
JSON.stringify(content, null, 2)
);
console.log("Created", language);
}
console.log("Done!");
};
i18n();