-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsyncTranslations.js
More file actions
106 lines (100 loc) · 3.09 KB
/
syncTranslations.js
File metadata and controls
106 lines (100 loc) · 3.09 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* eslint-disable */
const fs = require('fs');
const path = require('path')
const { google } = require('googleapis');
const sanitize = require('sanitize-html');
const { exit } = require('process');
require('dotenv').config()
const apiClient = new google.auth.GoogleAuth({
keyFile: path.resolve(__dirname, 'creds.json'),
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'],
})
addTranslations(apiClient)
const languages = ['fi', 'en', 'ru', 'it', 'zh', 'sv', 'uk']
async function addTranslations(auth) {
const sheets = google.sheets({version: 'v4', auth});
Promise.all([
sheets.spreadsheets.values.get({
spreadsheetId: '1OVtLSEpLA6gmwS1LSRGQ1P6MwmhU1xAxOe6fsetCRZk',
range: "C:K",
}),
sheets.spreadsheets.values.get({
spreadsheetId: '1qUitRG9RYZALDQId0CGM6tWOdmZmqCKEw6iEQcSyBMk',
range: "'master'!C:K",
}),
sheets.spreadsheets.values.get({
spreadsheetId: '1qUitRG9RYZALDQId0CGM6tWOdmZmqCKEw6iEQcSyBMk',
range: "'DDLANG UI'!C:K",
}),
]).then(results => {
const translations = {}
for (res of results){
const keyMap = res.data.values[0].reduce((acc, key, i) => {
acc[key] = i
return acc
}
, {})
const rows = res.data.values.slice(1);
for (row of rows) {
row.map(e => e.trim())
if (!row[0]) continue
const trimmedRow = row.map(e => e.trim())
translations[trimmedRow[0]] = {}
languages.map(lang => {
translations[trimmedRow[0]][lang] = trimmedRow[keyMap[lang]] || trimmedRow[keyMap['en']] || ''
})
}
}
makeTranslations(translations)
}).catch(err => {
console.log('The API returned an error: ' + err);
});
}
// validate html strings
// check if tags are closed properly
function validateHtml(html) {
const config = {
allowedAttributes: {
a: ['href'],
span: ['style']
},
'*': {
// Match HEX and RGB
'color': [/^.?$/],
'text-align': [/^left$/, /^right$/, /^center$/],
// Match any number with px, em, or %
'font-size': [/^\d+(?:px|em|%)$/],
'font-weight': [/^.+$/],
},
}
// console.log(sanitize(html, config))
// console.log(html)
// console.log()
// console.log()
return html === sanitize(html, config)
}
function makeTranslations(translations) {
for (lang of languages) {
let changes = 0
let news = 0
const fileName = path.resolve(__dirname, `./client/util/translations/revita/${lang}/LC_MESSAGES/messages.json`)
const file = require(fileName)
for ([key, langs] of Object.entries(translations)) {
const orig = file[key]
file[key] = langs[lang]
if (!validateHtml(file[key].replace(/\s*\/>/g, ' />'))) {
console.log(`invalid html in ${lang} ${key} -> ${file[key]}`)
exit(1)
}
else if (orig === undefined) {
news++
}
else if (orig != file[key]) {
changes++}
}
fs.writeFile(fileName, JSON.stringify(file, null, ' '), (err) => {
if (err) return console.log(err)
console.log(`writing ${news} new translations, ${changes} changes to ${fileName}`)
})
}
}