-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-languages-sheets.js
executable file
·77 lines (68 loc) · 2.42 KB
/
fetch-languages-sheets.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
74
75
76
77
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const process = require('process');
const fs = require('fs');
const XLSX = require('xlsx');
const google = require('googleapis');
const credentialPath = `${__dirname}/credentials.json`;
const auth = new google.Auth.GoogleAuth({
keyFile: credentialPath,
scopes: 'https://www.googleapis.com/auth/spreadsheets',
});
async function processLanguageSheet() {
const spreadsheetId = '1UFfNikfLuo8bSromE34uWDuJrMPFiJG3VpoQKdCGkII';
const googleSheets = new google.sheets_v4.Sheets();
const rows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
range: 'Translations',
});
const rowsJSON = XLSX.utils.sheet_to_json(XLSX.utils.aoa_to_sheet(rows.data.values), {
defval: '',
});
const data = {};
for (let phrase of rowsJSON) {
const {language, ...translations} = phrase;
if (language.includes('T_') || language.includes('EE_') || language.includes('RC_'))
data[language] = translations;
}
for (let phrase in data) {
for (let lang in data[phrase]) {
if (data[phrase][lang].includes('XX'))
data[phrase][lang] = data[phrase][lang].replace(/XXX/g, 'xxx').replace(/XX/g, 'xx');
}
}
const exportWarning = `/*
Do not modify this file! Run npm \`npm run phrases\` at ROOT of this project to fetch from the Google Sheets.
Phrases should be read using the "readi18nPhrases" function from "components/readPhrases", to prevent silent breaking errors.
https://docs.google.com/spreadsheets/d/1UFfNikfLuo8bSromE34uWDuJrMPFiJG3VpoQKdCGkII/edit#gid=0
*/\n\n`;
const exportHandle = `export const phrases = `;
fs.writeFile(
`${__dirname}/i18n.js`,
exportWarning + exportHandle + JSON.stringify(data) + '\n',
error => {
if (error) {
console.log("Error! Couldn't write to the file.", error);
} else {
console.log('EasyEyes International Phrases fetched and written into files successfully.');
}
}
);
}
require('dns').resolve('www.google.com', function (err) {
if (err) {
console.log('No internet connection. Skip fetching phrases.');
} else {
try {
if (fs.existsSync(credentialPath)) {
console.log('Fetching up-to-date phrases...');
processLanguageSheet();
} else {
console.log(':( Failed to fetch PHRASES. No credentials.json found.');
}
} catch (error) {
console.error(err);
}
}
});