PageSourceSearch

https://stavrosdecor.com/bitrix/js/main/core/core_translit.js

js stavrosdecor.com collected 2026-10-04 09:12:12 UTC 9,578 bytes, 386 lines download raw bytes

1;(function(window) {
2if (BX.translit) return;
3
4var
5	arTransTable = null,
6	translatorCache = [],
7	defaultParams = {
8		max_len: 100,
9		change_case: 'L', // 'L' - toLower, 'U' - toUpper, false - do not change
10		replace_space_and_other: true,
11		replace_space: '_',
12		replace_other: '_',
13		delete_repeat_replace: true,
14		use_google: false, // Yandex.Translator is really using here but we won't rename a setting name now
15		replace_dict: 'TRANS',
16		replace_way: 'LE', // LE - LANG->ENGLISH, EL - ENGLISH->LANG
17		skip_r_test: false
18	},
19	r = {
20		en: /[A-Z0-9]/i,
21		space: /\s/
22	};
23
24BX.translit = function (str, params)
25{
26	if (null == params) params = {};
27	for (var i in defaultParams)
28	{
29		if (typeof params[i] == 'undefined')
30			params[i] = defaultParams[i];
31	}
32	if (params.change_case)
33		params.change_case = params.change_case.toUpperCase();
34
35	if (params.use_google && params.callback && !!BX.message('YANDEX_KEY'))
36	{
37		return (new BX.CYandexTranslator(str, params)).run();
38	}
39	else
40	{
41		var len = str.length;
42		var str_new = '';
43		var last_chr_new = '';
44
45		for (var i = 0; i < len; i++)
46		{
47			var chr = str.charAt(i);
48
49			if (!params.skip_r_test && r.en.test(chr))
50			{
51				chr_new = chr;
52			}
53			else if (!params.skip_r_test && r.space.test(chr))
54			{
55				if (
56					!params.delete_repeat_replace
57					||
58					(i > 0 && last_chr_new != params.replace_space)
59				)
60					chr_new = params.replace_space;
61				else
62					chr_new = '';
63			}
64			else
65			{
66				var chr_new = __getChar(chr, params.change_case, params.replace_way, params.replace_dict, params.replace_separator);
67				if (null === chr_new)
68				{
69					if (params.replace_space_and_other)
70					{
71						if (!params.delete_repeat_replace
72							||
73							(i > 0 && i != len-1 && last_chr_new != params.replace_other)
74						)
75							chr_new = params.replace_other;
76						else
77							chr_new = '';
78					}
79					else
80					{
81						chr_new = chr;
82					}
83				}
84			}
85
86			if (null != chr_new && chr_new.length > 0)
87			{
88				switch(params.change_case)
89				{
90					case 'L': chr_new = chr_new.toLowerCase(); break;
91					case 'U': chr_new = chr_new.toUpperCase(); break;
92				}
93
94				str_new += chr_new;
95				last_chr_new = chr_new;
96			}
97
98			if (str_new.length >= params.max_len)
99				break;
100		}
101
102		if (null != params.callback && BX.type.isFunction(params.callback))
103		{
104			params.callback(str_new)
105			return str_new;
106		}
107		else
108			return str_new;
109	}
110};
111
112BX.correctText = function(str, params)
113{
114	if (typeof(params) == 'undefined')
115		params = {};
116
117	params.max_len = params.max_len? params.max_len: str.length;
118	params.replace_way = params.replace_way? params.replace_way: 'EL'; // LE - LANG->ENGLISH, EL - ENGLISH->LANG, AUTO - auto select
119	params.mixed = params.mixed? true: false;
120
121	if (!params.mixed || params.replace_way == 'AUTO')
122	{
123		var countEnglish = 0;
124		var countForeign = 0;
125		var len = str.length;
126		var regEnglish = /[A-Z]/i;
127		var regSpecial = /[0-9!@#$%\^\&\*:;"~_ \(\),.\+\=\-\\\{\}\?\<\>]/i;
128		for (var i = 0; i < len; i++)
129		{
130			var chr = str.charAt(i);
131			if (regEnglish.test(chr))
132			{
133				countEnglish++
134			}
135			else if (regSpecial.test(chr))
136			{
137			}
138			else
139			{
140				countForeign++;
141			}
142		}
143	}
144	if (params.replace_way == 'AUTO')
145	{
146		if (countForeign > countEnglish)
147		{
148			params.replace_way = 'LE';
149		}
150		else
151		{
152			params.replace_way = 'EL';
153		}
154	}
155
156	if (params.replace_way == 'LE')
157	{
158		if (!params.mixed && (countEnglish > 0 || countForeign == 0))
159		{
160			return str;
161		}
162	}
163	else if (!params.mixed && params.replace_way == 'EL')
164	{
165		if (!params.mixed && (countEnglish == 0 || countForeign > 0))
166		{
167			return str;
168		}
169	}
170
171	return BX.translit(str, {
172		'replace_dict': 'CORRECT',
173		'replace_way': params.replace_way,
174		'replace_separator': ' ',
175		'skip_r_test': true,
176		'change_case': false,
177		'max_len': params.max_len,
178		'delete_repeat_replace': false,
179		'replace_space_and_other': false
180	});
181};
182
183/* external translator interface class */
184BX.IExternalTranslator = function(str, params)
185{
186	this.str = str;
187	this.params = params;
188};
189
190BX.IExternalTranslator.prototype.run = function()
191{
192	var res = __checkCache(this.str);
193	if (res)
194		this.result({translation: res}, true);
195	else
196		this.translate();
197};
198
199/* translation function */
200BX.IExternalTranslator.prototype.translate = function() {};
201
202/* result processing function */
203BX.IExternalTranslator.prototype.result = function(result, bSkipCache)
204{
205	if (!bSkipCache)
206		translatorCache[translatorCache.length] = {original: this.str, translation: result.translation};
207
208	this.params.use_google = false;
209	BX.translit(result.translation, this.params);
210};
211
212/* Google Translate external class - DEPRECATED */
213BX.CGoogleTranslator = function(str, params)
214{
215	BX.CBingTranslator.superclass.constructor.apply(this, arguments);
216};
217BX.extend(BX.CGoogleTranslator, BX.IExternalTranslator);
218
219BX.CGoogleTranslator.prototype.translate = function()
220{
221	if (!window.google || typeof(window.google.load) != "function")
222	{
223		if (BX.browser.IsIE())
224		{
225			var cb_ie = BX.proxy(this.translate, this);
226			var cb = function() {
227				setTimeout(function() {
228					cb_ie(arguments);
229				}, 100);
230			}
231		}
232		else
233		{
234			var cb = BX.proxy(this.translate, this);
235		}
236
237		BX.loadScript('http://www.google.com/jsapi?rnd=' + Math.random(), cb);
238	}
239	else if (!window.google.language)
240	{
241		google.load(
242			'language', 1, {callback: BX.proxy(this.translate, this)}
243		);
244	}
245	else
246	{
247		google.language.translate(
248			this.str,
249			BX.message('LANGUAGE_ID'),
250			"en",
251			BX.delegate(this.result, this)
252		);
253	}
254};
255
256/* Bing Translate external class - DEPARECATED AFTER 2012-08-01 */
257BX.CBingTranslator = function(str, params)
258{
259	BX.CBingTranslator.superclass.constructor.apply(this, arguments);
260};
261BX.extend(BX.CBingTranslator, BX.IExternalTranslator);
262
263BX.CBingTranslator.prototype.translate = function()
264{
265	var cb_name = 'bing_translate_callback_' + parseInt(Math.random() * 10000),
266		url = 'http://api.bing.net/json.aspx?AppId='+BX.message('BING_KEY')+'&Query=' + BX.util.urlencode(this.str.substr(0, 5000))+'&Sources=Translation&Version=' + (this.params.version||'2.2') + '&Translation.SourceLanguage='+BX.message('LANGUAGE_ID')+'&Translation.TargetLanguage=en&JsonType=callback&JsonCallback=' + cb_name;
267
268	window[cb_name] = BX.proxy(this.result, this);
269	BX.loadScript(url, function() {window[cb_name] = null});
270};
271
272BX.CBingTranslator.prototype.result = function(result, bSkipCache)
273{
274	var res = {translation: this.str};
275	if (result)
276	{
277		if (result.translation)
278			res = result;
279		else if (
280			result.SearchResponse
281			&& result.SearchResponse.Translation
282			&& result.SearchResponse.Translation.Results
283			&& result.SearchResponse.Translation.Results[0]
284		)
285		{
286			res.translation = result.SearchResponse.Translation.Results[0].TranslatedTerm;
287		}
288	}
289
290	return BX.CBingTranslator.superclass.result.apply(this, [res, b
290SkipCache]);
291};
292
293
294/* Yandex Translate external class */
295BX.CYandexTranslator = function(str, params)
296{
297	BX.CYandexTranslator.superclass.constructor.apply(this, arguments);
298};
299BX.extend(BX.CYandexTranslator, BX.IExternalTranslator);
300
301BX.CYandexTranslator.prototype.translate = function()
302{
303	var arStr = this.str.substr(0,5000).split(/\n/), text = '', i;
304
305	for (i=0; i<arStr.length; i++)
306		text += '&text=' + BX.util.urlencode(arStr[i]);
307
308	var langPair = (BX.message('LANGUAGE_ID') == 'ua' ? 'uk' : BX.message('LANGUAGE_ID')) + '-en';
309
310	var cb_name = 'yandex_translate_callback_' + parseInt(Math.random() * 100000),
311		url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key='+BX.message('YANDEX_KEY')+'&lang='+langPair+'&callback=' + cb_name + '&clientId=bitrix' + text;
312
313	window[cb_name] = BX.proxy(this.result, this);
314	BX.loadScript(url, function() {window[cb_name] = null});
315};
316
317BX.CYandexTranslator.prototype.result = function(result, bSkipCache)
318{
319	var res = {translation: this.str};
320	if (!!result)
321	{
322		if (result.translation)
323			res = result;
324		else if (result.code == 200 && result.text.length > 0)
325			res.translation = result.text.join("\n");
326	}
327
328	return BX.CYandexTranslator.superclass.result.apply(this, [res, b
328SkipCache]);
329};
330
331/* private static functions */
332
333function __checkCache(str)
334{
335	for (var i = 0, len = translatorCache.length; i < len; i++)
336	{
337		if (translatorCache[i].original == str)
338			return translatorCache[i].translation;
339	}
340
341	return null;
342};
343
344function __generateTransTable(replace_dict, replace_way, replace_separator)
345{
346	var
347		arFrom = (BX.message(replace_dict+(replace_way == 'LE'? '_FROM': '_TO')) || '').split(replace_separator),
348		arTo = (BX.message(replace_dict+(replace_way == 'LE'? '_TO': '_FROM')) || '').split(replace_separator),
349		i, len;
350
351	if (arTransTable == null)
352		arTransTable = {};
353
354	if (typeof(arTransTable[replace_dict]) == 'undefined')
355		arTransTable[replace_dict] = {};
356
357	arTransTable[replace_dict][replace_way] = [];
358	for (i = 0, len = arFrom.length; i < len; i++)
359	{
360		arTransTable[replace_dict][replace_way][i] = [arFrom[i], arTo[i]];
361	}
362};
363
364function __getChar(chr, change_case, replace_way, replace_dict, replace_separator)
365{
366	if (typeof(replace_separator) == 'undefined')
367		replace_separator = ',';
368
369	if (typeof(replace_dict) == 'undefined')
370		replace_dict = 'TRANS';
371
372	if (arTransTable == null || typeof(arTransTable[replace_dict]) == 'undefined' || typeof(arTransTable[replace_dict][replace_way]) == 'undefined')
373		__generateTransTable(replace_dict, replace_way, replace_separator)
374
375	for (var i=0, len = arTransTable[replace_dict][replace_way].length; i < len; i++)
376	{
377		if (chr === arTransTable[replace_dict][replace_way][i][0])
378		{
379			return arTransTable[replace_dict][replace_way][i][1];
380		}
381	}
382
383	return null;
384};
385
386})(window);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.