-
Notifications
You must be signed in to change notification settings - Fork 0
/
libs.js
45 lines (34 loc) · 1.43 KB
/
libs.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
const utils = require('./translate_modules/utils');
const translateV2 = require('./translate_modules/translateV2');
const manageLangDetection = async (lowerCaseKeyword) => {
let translatedKeyword;
// detect lang
let detectedLang;
if (utils.isSpanish(lowerCaseKeyword)) {
detectedLang = 'es';
console.log('detected as es');
}else if(lowerCaseKeyword.includes(' ')){
detectedLang = 'es';
console.log('has space, send to translation as ES');
}else{
detectedLang = 'jp';
console.log('detected as jp');
}
// manage keyword format depending on the source lang
if (detectedLang === 'es') {
translatedKeyword = await translateV2.esToEn(lowerCaseKeyword).catch(e => {
throw new Error(`Error en el manejo de la búsqueda para: ${lowerCaseKeyword} - ${e}`);
});
// make lowercase
translatedKeyword = translatedKeyword.toLocaleLowerCase();
}
if (detectedLang === 'jp') {
translatedKeyword = lowerCaseKeyword;
}
// if has japanese (ex. romaji false positive es -> transl made it kana), then remove spaces
if (utils.checkForJapaneseInString(translatedKeyword, utils.isKanji) || utils.checkForJapaneseInString(translatedKeyword, utils.isKana)) {
translatedKeyword = translatedKeyword.replace(/ /g, '');
}
return translatedKeyword;
}
exports.manageLangDetection = manageLangDetection;