-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.js
73 lines (64 loc) · 1.84 KB
/
i18n.js
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
const Airtable = require('airtable');
const fs = require('fs');
// import .env config
require('dotenv').config();
const AIRTABLE_KEY = process.env.AIRTABLE_KEY;
const AIRTABLE_I18N_BASE_COMMON = process.env.AIRTABLE_I18N_BASE_COMMON;
const AIRTABLE_I18N_TABLE_COMMON = process.env.AIRTABLE_I18N_TABLE_COMMON;
const AIRTABLE_I18N_VIEW_COMMON = process.env.AIRTABLE_I18N_VIEW_COMMON;
if (!AIRTABLE_I18N_BASE_COMMON || !AIRTABLE_I18N_TABLE_COMMON || !AIRTABLE_I18N_VIEW_COMMON) {
console.error('[ERROR] should provide third argument in env: AIRTABLE_XXX');
process.exit(-1);
}
const langObject = {
'zh-TW': {},
'en-US': {},
};
Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: AIRTABLE_KEY,
});
const baseCommon = Airtable.base(AIRTABLE_I18N_BASE_COMMON);
(async () => {
fetchAirtable(baseCommon, AIRTABLE_I18N_TABLE_COMMON, AIRTABLE_I18N_VIEW_COMMON)
.then((common) => {
console.log(common);
Object.entries(common).forEach(([key, value]) => {
fs.writeFile(
`./locales/${key}.json`,
JSON.stringify({ ...value }),
'utf8',
() => {
console.log('write file', key);
},
);
});
})
.catch((err) => {
console.error(err);
process.exit(-1);
});
})();
function fetchAirtable(basesss, table, view) {
return new Promise((res) =>
basesss(table)
.select({ view })
.eachPage(
(records, fetchNextPage) => {
records.forEach((record) => {
Object.entries(langObject).forEach(([key, value]) => {
value[record.get('key')] = record.get(key);
});
});
fetchNextPage();
},
(err) => {
if (err) {
console.error(err);
process.exit(-1);
}
res(langObject);
},
),
);
}