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