-
Notifications
You must be signed in to change notification settings - Fork 14
/
generate-entry-i18n-chunks.js
222 lines (195 loc) · 6 KB
/
generate-entry-i18n-chunks.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
// cli usage
// start node console on heroku
// $ heroku run node --app participedia-i18n-staging
// or locally (locally you will have to paste your .env vars exports in the terminal before running so the console can access them)
// $ node
// then require the script and run the function you want
// > var GenerateEntryI18n = require("/app/scripts/generate-entry-i18n.js");
// > GenerateEntryI18n.translateListOfEntries([{type: "case", id: 2}]);
// Get google translate credentials
const keysEnvVar = process.env["GOOGLE_TRANSLATE_CREDENTIALS"];
if (!keysEnvVar) {
throw new Error(
"The GOOGLE_TRANSLATE_CREDENTIALS environment variable was not found!"
);
return;
}
const { SUPPORTED_LANGUAGES } = require("./../constants.js");
const { db, pgp } = require("../api/helpers/db");
const { Translate } = require("@google-cloud/translate").v2;
const authKeys = JSON.parse(keysEnvVar);
authKeys["key"] = process.env.GOOGLE_API_KEY;
const translate = new Translate(authKeys);
function translateAllEntries(type, limit, offset) {
getThings(type, limit, offset);
}
// translateListOfEntries([
// ]);
// getListOfNonTranslatedEntries('organization');
// translate a list of entries where entries is an array of objects [{ type: "case", id: 1 }]
function translateListOfEntries(entries) {
entries.forEach(entry => {
getThingById(entry.type, entry.id);
});
}
function getThingById(type, id) {
db.any(
`SELECT * FROM things WHERE type = '${type}' AND id = '${id}' AND hidden = false`
)
.then(function(thingData) {
thingData.forEach(data => {
getLocalizationData(data.id, data.original_language);
});
return null;
})
.catch(function(error) {
console.log(error);
});
}
function getThings(type, limit, skip) {
db.any(
`SELECT * FROM things WHERE type = '${type}' AND hidden = false ORDER BY id DESC LIMIT ${limit} OFFSET ${skip}`
)
.then(function(thingData) {
thingData.forEach(data => {
getLocalizationData(data.id, data.original_language);
});
return null;
})
.catch(function(error) {
console.log(error);
});
}
function getLocalizationData(thingid, language) {
db.any(
`SELECT * FROM localized_texts WHERE thingid = ${thingid} AND language = '${language}' ORDER BY timestamp DESC LIMIT 1`
)
.then(function(data) {
data.forEach(data => {
createNewRecord(data, thingid);
});
return null;
})
.catch(function(error) {
console.log(error);
});
}
async function createNewRecord(data, thingid) {
var currentLanguages = [];
await db
.any(`SELECT language FROM localized_texts WHERE thingid = ${thingid}`)
.then(function(existingLanguages) {
existingLanguages.forEach(lang => {
currentLanguages.push(lang.language);
});
})
.catch(function(error) {
console.log(error);
});
for (var i = 0; i < SUPPORTED_LANGUAGES.length; i++) {
// Loop in supported languages
const language = SUPPORTED_LANGUAGES[i];
if (currentLanguages.indexOf(language.twoLetterCode) < 0) {
// If language in loop not exist from currentLanguages. Add record
if (language.twoLetterCode !== data.language) {
// Don't create language from original
const item = {
body: "",
title: "",
description: "",
language: language.twoLetterCode,
thingid: thingid,
timestamp: "now",
};
if (data.body) {
item.body = await translateText(data.body, language.twoLetterCode);
}
if (data.title) {
item.title = await translateText(data.title, language.twoLetterCode);
}
if (data.description) {
item.description = await translateText(
data.description,
language.twoLetterCode
);
}
console.log(
`ThingID: ${thingid} => saving record for ${
language.twoLetterCode
} from ${data.language}`
);
await saveRecord([item]).then(() => {
console.log(
`ThingID: ${thingid} => record for ${language.twoLetterCode} from ${
data.language
} DONE!`
);
console.log(`ThingID: ${item.title}`);
console.log(
"====================================================================="
);
});
}
}
}
}
async function saveRecord(records) {
const insert = pgp.helpers.insert(
records,
["body", "title", "description", "language", "thingid", "timestamp"],
"localized_texts"
);
db.none(insert);
}
async function translateText(data, targetLanguage) {
// The text to translate
let allTranslation = '';
// The target language
const target = targetLanguage;
let textParts = data.match(/.{1,5000}/g);
for(var text of textParts){
let [translation] = await translate
.translate(text, target)
.catch(function(error) {
console.log(error);
});
allTranslation += translation;
}
return allTranslation;
}
async function getListOfNonTranslatedEntries(type) {
await db
.any(
`SELECT * FROM things WHERE type = '${type}' AND hidden = false ORDER BY id DESC`
)
.then(function(things) {
things.forEach(async thing => {
await db
.any(`select * from localized_texts where thingid=${thing.id}`)
.then(
await function(records) {
const hasTranslatedRecords =
records.filter(record => {
return record.language === "es";
}).length > 0;
if (!hasTranslatedRecords) {
// needs to be translated
console.log(`{ type: "${type}", id: ${thing.id} },`);
}
}
)
.catch(function(error) {
console.log(error);
});
});
return null;
})
.catch(function(error) {
console.log(error);
});
}
module.exports = {
translateAllEntries,
translateListOfEntries,
getListOfNonTranslatedEntries,
};