convert.js
3.39 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
106
107
108
109
110
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args))
const bidiFactory = require('bidi-js')
const bidi = bidiFactory()
const fs = require('fs')
const fileExtension = 'txt'
async function main() {
try {
const tsvRes = await fetch('https://docs.google.com/spreadsheets/d/1FoYdyEraEQuWofzbYCDPKN7EdKgS_2ZrsDrOA8scgwQ/export?format=tsv')
const tsv = await tsvRes.text()
const lines = tsv.split('\n')
// First line contains full language names
// Second line contains file extensions
const codes = lines[1].split('\t')
if (codes[0] == '')
codes[0] = 'i18n'
const outputFiles = []
for (let j = 2; j < lines.length; j++) {
const entries = lines[j].split('\t')
if (entries.length > 1) {
const key = entries[0]
if (key) {
for (let i = 0; i < codes.length; i++) {
if (codes[i].trim()) {
if (outputFiles[i] == null) {
outputFiles[i] = '# *DO NOT DIRECTLY EDIT THIS FILE, IT IS AUTOMATICALLY GENERATED AND IT IS BASED ON:*\n'
+ '# https://docs.google.com/spreadsheet/ccc?key=0AmQEO36liL4FdDJLWVNMaVV2UmRKSnpXU09MYkdGbEE\n'
}
if (codes[i] == 'i18n') {
outputFiles[i] += `${key}=${key}\n`
}
else {
let value = (entries.length > i) ? entries[i] : ''
// Empty entries will be translated to English
if (value == '')
value = entries[1]
value = value.trim()
// TODO Add encoding support
/* if (PropGen.encodeValues)
{
value = encodeString(value);
} */
const lang = codes[i].toLowerCase()
let rtl = false
if (lang == 'ar' || lang == 'fa' || lang == 'he') {
const embeddingLevels = bidi.getEmbeddingLevels(value)
function isMixed(levels) {
for (let i = 0; i < levels.length; i++)
if (levels[i] == 1) return true
return false
};
if (embeddingLevels.paragraphs[0].level == 1 || isMixed(embeddingLevels.levels)) {
console.log(value)
rtl = true
}
}
if (rtl) {
outputFiles[i] += `${key}=` + `\u202B${value}\u202C` + '\n'
}
else {
// Checks for HTML entities in output
if (value.includes('<') || value.includes('>'))
console.log(`**** WARNING: HTML Entities in ${lang}/${key}=${value}`)
outputFiles[i] += `${key}=${value}\n`
}
}
}
}
}
}
}
const outDir = './resources'
if (!fs.existsSync(outDir))
fs.mkdirSync(outDir)
for (let i = 0; i < codes.length; i++) {
if (codes[i].trim()) {
const ext = (codes[i] == 'en') ? '' : `_${codes[i].toLowerCase()}`
const filename = `${outDir}/dia${ext}.${fileExtension}`
fs.writeFileSync(filename, outputFiles[i], 'utf8')
console.log(`${filename} created`)
}
}
}
catch (e) {
console.log(e)
}
}
main()