1 | var maps = require('./maps.js'); |
||
2 | |||
3 | var trans = function() { |
||
4 | var that = this; |
||
5 | |||
6 | this.maps = maps; |
||
7 | |||
8 | this.tWord1 = function translingualate(word, from_lang, to_lang) { |
||
9 | // console.debug(word, that.maps.map[from_lang][to_lang]); |
||
10 | |||
11 | for (var k in that.maps.map[from_lang][to_lang]) { |
||
12 | // console.debug('k-->', k, word, that.maps.map[from_lang][to_lang][k]); |
||
13 | if (that.maps.map[from_lang][to_lang].hasOwnProperty(k)) { |
||
14 | word = word.replace(new RegExp(k, 'g'), that.maps.map[from_lang][to_lang][k]); |
||
15 | } |
||
16 | } |
||
17 | |||
18 | return word; |
||
19 | }; |
||
20 | |||
21 | this.tWordN = function translingualate(word, from_lang, to_lang, jumpTo) { |
||
22 | if (!word) { |
||
23 | return ''; |
||
24 | } |
||
25 | |||
26 | jumpTo = jumpTo || 0; |
||
27 | var tmp, parts, keys = Object.keys(that.maps.map[from_lang][to_lang]); |
||
28 | |||
29 | for (; jumpTo < keys.length; ++jumpTo) { |
||
30 | if (tmp = word.match(new RegExp(keys[jumpTo], 'ig'))) { |
||
31 | try { |
||
32 | tmp[0] = tmp[0].replace(/([\()\[\]])/g, "\\$1"); |
||
33 | parts = word.split(new RegExp('(' + tmp[0] + ')')).filter(function (v) { |
||
34 | return v; |
||
35 | }); |
||
36 | |||
37 | // console.debug('in-->', tmp, word, parts); |
||
38 | |||
39 | for (var i = 0; i < parts.length; ++i) { |
||
40 | if (parts[i] === tmp[0]) { |
||
41 | // console.debug('in-->', keys[jumpTo]); |
||
42 | parts[i] = parts[i].replace(new RegExp(keys[jumpTo], 'ig'), that.maps.map[from_lang][to_lang][keys[jumpTo]]); |
||
43 | } else { |
||
44 | parts[i] = that.tWordN(parts[i], from_lang, to_lang, jumpTo + 1); |
||
45 | } |
||
46 | } |
||
47 | word = parts.join(''); |
||
48 | } catch(e) { |
||
49 | console.error('Failed transliting:', keys[jumpTo], e, jumpTo, tmp, word, from_lang, to_lang); |
||
50 | } |
||
51 | |||
52 | break; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | return word; |
||
57 | }; |
||
58 | |||
59 | this.find_bridge = function (str, from_lang, to_lang) { |
||
60 | var found_bridge = false; |
||
61 | |||
62 | // console.debug(str, from_lang, to_lang, that.maps.languages); |
||
63 | |||
64 | for (var i = 0; i < that.maps.languages.length && found_bridge === false; ++i) { |
||
65 | found_bridge = !!that.maps.map[that.maps.languages[i]][to_lang] && that.maps.languages[i]; |
||
66 | } |
||
67 | |||
68 | if (found_bridge) { |
||
69 | return that.lingualate_helper(that.lingualate_helper(str, from_lang, found_bridge), found_bridge, to_lang); |
||
70 | } |
||
71 | return ''; |
||
72 | }; |
||
73 | |||
74 | this.tsplit = function (str) { |
||
75 | return str.split(/[\s\-]/).filter(function (v) { return typeof v !== 'undefined' && v.length; }) |
||
76 | }; |
||
77 | |||
78 | this.lingualate_helper = function (str, from_lang, to_lang) { |
||
79 | // return str; |
||
80 | // console.debug('rrr!!!', str, from_lang, to_lang); |
||
81 | if (!str || !that.maps.map[from_lang]) { |
||
82 | return str || ''; |
||
83 | } |
||
84 | |||
85 | if (!that.maps.map[from_lang][to_lang]) { |
||
86 | return that.find_bridge(str, from_lang, to_lang); |
||
87 | } |
||
88 | |||
89 | var words = that.tsplit(str), |
||
90 | func = ['ru', 'ua', 'sr', 'el', 'trans'].indexOf(from_lang) !== -1 ? that.tWord1 : that.tWordN; |
||
91 | |||
92 | for (var i=0; i < words.length; ++i) { |
||
93 | words[i] = func(words[i], from_lang, to_lang); |
||
94 | } |
||
95 | |||
96 | return words.join(' '); |
||
97 | }; |
||
98 | |||
99 | this.up = function (str) { |
||
100 | return that.tsplit(str).map(function (v) { |
||
101 | return v.substr(0, 1).toUpperCase() + v.substr(1); |
||
102 | }).join(' '); |
||
103 | }; |
||
104 | |||
105 | this.lingualate = function (str, from_lang, to_lang) { |
||
106 | return that.up(that.lingualate_helper(str.toLowerCase(), from_lang, to_lang)); |
||
107 | }; |
||
108 | }; |
||
109 | |||
110 | module.exports = new trans(); |
||
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
![]() |