-
Notifications
You must be signed in to change notification settings - Fork 1
/
dictionary.js
299 lines (277 loc) · 7.86 KB
/
dictionary.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* eslint-disable no-process-env */
/* eslint-disable no-extra-parens */
const Promise = require('bluebird')
const path = require('path')
const pify = require('pify')
const fs = pify(require('fs'))
const _ = require('lodash')
const { Translate } = require('@google-cloud/translate').v2
const aws = require('aws-sdk')
const s3ls = require('s3-ls')
const MODEL = 'model'
const PROPERTY_NAME = 'propertyName'
const DEFAULT = 'Default'
const MAX_LANGUAGES_IN_ONE_SHOT = 1
const BUCKET = 'tradle.io'
const DICTIONARIES_FOLDER = 'dictionaries/'
module.exports = { writeDictionary, writeDictionaries }
const translate = new Translate();
const s3 = new aws.S3()
aws.config.setPromisesDependency(Promise);
async function writeDictionaries ({
// eslint-disable-next-line no-inline-comments
modelsDir, lang, newOnly /* Memo:, all */, domain
}) {
if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
console.log(
'Please set environment variable GOOGLE_APPLICATION_CREDENTIALS ' +
' to allow models translation'
)
return
}
if (!process.env.AWS_PROFILE) {
console.log('Please check if you use the correct AWS_PROFILE')
return
}
let langs
// Memo: if (lang && lang !== 'en')
if (lang)
langs = lang.split(',')
else {
const [languages] = await translate.getLanguages()
langs = languages.map(l => l.code)
}
modelsDir = path.resolve(modelsDir)
let models
const parts = modelsDir.split('/')
const dir = parts[parts.length - 2]
if (modelsDir.indexOf('.json') === -1) {
let files = await fs.readdir(modelsDir)
files = files.filter(file => (/\.json$/).test(file))
models = files.map(file => {
return require(path.join(modelsDir, file))
})
} else {
const ddir = path.resolve(dir)
if (!fs.existsSync(ddir))
fs.mkdirSync(ddir)
models = require(modelsDir)
}
let len
if (langs.length > MAX_LANGUAGES_IN_ONE_SHOT)
len = MAX_LANGUAGES_IN_ONE_SHOT
else
len = langs.length
const s3Dir = (domain && domain.split('.')[0]) || dir
const lister = s3ls({bucket: BUCKET});
const folder = `${DICTIONARIES_FOLDER}${s3Dir}/`
let fileNames
try {
const data = await lister.ls(folder)
fileNames = data.files
} catch (err) {
console.log(err.message, err.stack)
return
}
let i = 0
// eslint-disable-next-line no-constant-condition
while (true) {
const newLangs = []
for (let j = 0; j < len && i < langs.length; i += 1, j += 1) {
const l = langs[i]
const fn = `${folder}dictionary_${l}.json`
if (newOnly && fileNames.includes(fn))
continue
newLangs.push(l)
}
// eslint-disable-next-line no-await-in-loop
await Promise.all(newLangs
.map(newLang => writeDictionary({models, newLang, newOnly, dir: s3Dir}))
)
if (i === langs.length)
break
// eslint-disable-next-line no-await-in-loop
await timeout(60000)
}
}
function timeout (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function writeDictionary ({models, lang, newOnly, dir}) {
let fn = `/dictionary_${lang.replace('-', '')}.json`
if (dir)
fn = `${dir}${fn}`
else
fn = `.${fn}`
let dfile
const paramsGet = {
Bucket: BUCKET,
Key: `${DICTIONARIES_FOLDER}${fn}`
};
try {
const res = await s3.getObject(paramsGet).promise()
dfile = JSON.parse(Buffer.from(res.Body).toString('utf8'))
if (dfile && newOnly)
return
if (!dfile)
dfile = []
} catch (err) {
console.log(err.message)
if (err.statusCode !== 404)
return
if (!newOnly)
newOnly = true
dfile = []
}
const currentIds = {}
// eslint-disable-next-line no-inline-comments
dfile.forEach(({ type, model, name, en /* Passed in: , description */ }) => {
const id = (type === MODEL)
? [type, name, en].join('_')
: [model, name, en].join('_')
// Memo: if (description)
// Memo: id += `_${description}`
currentIds[id] = true
})
console.log(`Translating to ${lang}`)
const keys = Object.keys(models)
const result = await Promise.all(keys.map(id => translateModel({
model: models[id],
lang,
dictionary: dfile,
currentIds
})), { concurrency: 20 })
// Check if some models/props were deleted
let hasChanged
const newIds = {}
result.forEach(r => {
_.extend(newIds, r.newIds)
if (r.changed)
hasChanged = true
})
for (const p in currentIds) {
if (newIds[p])
continue
hasChanged = true
}
if (!hasChanged && !newOnly)
return
dfile.sort((a, b) => {
return a.en > b.en
})
const paramsPut = {
Body: Buffer.from(JSON.stringify(dfile, 0, 2)),
Bucket: BUCKET,
Key: `${DICTIONARIES_FOLDER}${fn}`,
ACL: 'public-read'
}
s3.putObject(paramsPut, (err, data) => {
if (err) {
// An error occurred
console.log(err, err.stack);
} else {
// Successful response
console.log(data);
}
})
}
const translateModel = async ({ model, dictionary, lang, currentIds }) => {
const m = model
const { id } = m
// Memo: if (!m || !m.title || !id) {
// Memo: debugger
// Memo: }
const mid = ['model', id, m.title].join('_')
const newIds = {[mid]: true}
let hasChanged
if (!currentIds[mid]) {
hasChanged = true
const obj = await addToDictionary({
dictionary, model, title: m.title, lang
})
if (m.enum) {
obj.enum = {}
// eslint-disable-next-line no-inline-comments
m.enum.forEach(async ({ /* Also available: id, */ title}) => {
obj.enum[id] = await translateText(title, lang)
})
}
} else if (m.enum) {
const idx = dictionary.findIndex(r => r.type === 'model' && r.name === m.id)
const obj = dictionary[idx]
if (obj.enum.length !== m.enum.length) {
hasChanged = true
for (let i=0; i<m.enum.length; i += 1) {
const { id: mEId, mETitle } = m.enum[i]
if (!obj.enum[mEId])
// eslint-disable-next-line no-await-in-loop
obj.enum[mEId] = await translateText(mETitle, lang)
}
}
}
const props = m.properties
for (const p in props) {
if (p.charAt(0) === '_')
continue
let { title } = props[p]
const { description, units } = props[p]
let pid
let notDefault = false
if (title) {
pid = [id, p, title].join('_')
notDefault = true
// Memo: if (description)
// Memo: pid += `_${description}`
} else {
title = makeLabel(p)
pid = [(description && id) || DEFAULT, p, title].join('_')
if (description) {
notDefault = true
// Memo: pid += `_${description}`
}
}
newIds[pid] = true
if (!currentIds[pid]) {
currentIds[pid] = true
hasChanged = true
// eslint-disable-next-line no-await-in-loop
await addToDictionary({
dictionary, model: notDefault && model,
propertyName: p, description, units, title, lang
})
}
}
return { changed: hasChanged, newIds }
}
async function addToDictionary ({
dictionary, model, propertyName, title, description, units, lang
}) {
const obj = {
[lang]: await translateText(title, lang),
en: title,
name: propertyName || model.id,
type: (propertyName && PROPERTY_NAME) || MODEL
}
if (propertyName)
obj.model = (model && model.id) || DEFAULT
if (description)
obj.description = await translateText(description, lang)
if (units)
obj.units = await translateText(units, lang)
dictionary.push(obj)
return obj
}
async function translateText (text, lang) {
if (lang === 'en') return text
const results = await translate.translate(text, lang)
let [translations] = results
translations = translations.charAt(0).toUpperCase() + translations.slice(1)
return Array.isArray(translations) ? translations[0] : translations
}
function makeLabel (label) {
return label
// Insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// Uppercase the first character
.replace(/^./, str => str.toUpperCase())
}