oMetaTranslateInitData = {}; oMetaTranslateInitData.api_key = 'FoEBOKARUwAUt46LLYBL3Xs%2Bnz%2BNph6fr5R33VPi21uoq5ei9cdOpB7CJ5fBf8xrd51IPjAsNCJytgk3bBCkOBo5RJu1Bo%2FX5bpbq%2BifRXA1a66TtUzrf06f3KffQQy7LkpfX5ilbFoeYcq0'; oMetaTranslateInitData.output_id = false; oMetaTranslateInitData.redirect_form = false; oMetaTranslateInitData.http_host = 'translate.meta.ua'; var oMetaTranslate = new function() { this.sApiKey; this.sHostRedirectUrl = 'http://translate.meta.ua/'; this.bAllowRedirect = true; this.sHostUrl = 'http://translate.meta.ua/ajax/'; this.sSnChunks = 'sn=api_recive_parts'; this.sSnTranslate = 'sn=api_get_translate'; this.sCharset = 'UTF-8'; this.iCurrentUniqid = false; this.iPartLength = 300; this.iPartCount = false; this.iPartSendSuccess = {count: false, parts: {}}; this.iChunkSuccess = {count: false, chunks: {}}; this.iChunkCount = false; this.sLangFrom = false; this.sLangTo = false; this.aValidLangs = {en: true, de: true,lv: true, pl: true, ru: true, ua: true, fr: true}; this.oChunksTranslate = {}; this.callbackOnEndTranslate = false; this.idTextSource = false; this.idTextTranslate = false; this.idLangFrom = false; this.idLangTo = false; this.tagNameValue = {"INPUT": true, "TEXTAREA": true }; this.bRedirectForm = false; this.idMetaForm = 'meta-translate-form'; this.idMetaFormTa = 'meta-translate-ta'; this.idMetaFormLangFrom = 'meta-translate-lng-from'; this.idMetaFormLangTo = 'meta-translate-lng-to'; this.idMetaFormLangSubmit = 'meta-translate-btn'; this.idMetaFormLink = 'meta-translate-link'; this.init = function(o) { if (typeof o == 'object') { if (typeof o.api_key != 'undefined') { this.sApiKey = o.api_key; } if (typeof o.host_url != 'undefined') { this.sHostUrl = o.host_url; } if (typeof o.redirect_url != 'undefined') { this.sHostRedirectUrl = o.redirect_url; } if (typeof o.output_id != 'undefined') { this.idTextTranslate = o.output_id; } if (typeof o.redirect_form != 'undefined') { this.bRedirectForm = o.redirect_form; } } this.sCharset = document.charset || document.characterSet; //setTimeout(this.bindMetaForm, 2000); } this.setEndTranslateCallback = function(f) { if (typeof f != 'function') { return false; } this.callbackOnEndTranslate = f; } this.translate = function() { if (this.idTextSource == false || this.idLangFrom == false || this.idLangTo == false) { return false; } var sLangFrom = document.getElementById(this.idLangFrom).value; var sLangTo = document.getElementById(this.idLangTo).value; var domSource = document.getElementById(this.idTextSource); var tn = domSource.tagName; var sSource = ''; if (typeof this.tagNameValue[tn] != 'undefined' && this.tagNameValue[tn] == true) { sSource = domSource.value; } else { sSource = domSource.innerHTML; } this.traslateText({text: sSource, lang_from: sLangFrom, lang_to: sLangTo}); } this.genUniqid = function() { return parseInt(Math.random() * 100000); } this.chunkText = function(sTextSource) { var aChunkedText = new Array(); var sText = sTextSource; if (sText.length < this.iPartLength) { aChunkedText.push(sText); return aChunkedText; } while (sText.length > 0) { var s = sText.slice(0, this.iPartLength-1); aChunkedText.push(s); sText = sText.slice(this.iPartLength); } return aChunkedText; } this.traslateText = function(o) { if (typeof o == 'undefined' || typeof o.text == 'undefined') { return false; } if (typeof o.lang_from == 'undefined' || typeof this.aValidLangs[o.lang_from] == 'undefined' || this.aValidLangs[o.lang_from] != true) { return false; } if (typeof o.lang_to == 'undefined' || typeof this.aValidLangs[o.lang_to] == 'undefined' || this.aValidLangs[o.lang_to] != true) { return false; } if (o.lang_from == o.lang_to) { return false; } this.iPartCount = false; this.iPartSendSuccess.count = false; this.iPartSendSuccess.parts = {}; this.iChunkSuccess.count = false; this.iChunkSuccess.chunks = {}; this.iChunkCount = false; this.oChunksTranslate = {}; this.sLangFrom = o.lang_from; this.sLangTo = o.lang_to; this.iCurrentUniqid = this.genUniqid(); var aChunks = this.chunkText(o.text); if (typeof aChunks == undefined || aChunks.length < 1) { return false; } this.iPartCount = aChunks.length; this.sendChunks(aChunks); } this.sendChunks = function(aChunkedText) { var iPartCount = aChunkedText.length; this.iPartSendSuccess.count = 0; for (var i in aChunkedText) { var aQueryData = new Array(); aQueryData.push(this.sSnChunks); aQueryData.push('uniqid=' + this.iCurrentUniqid); aQueryData.push('lang_from=' + this.sLangFrom); aQueryData.push('lang_to=' + this.sLangTo); aQueryData.push('part=' + i); aQueryData.push('pc=' + iPartCount); aQueryData.push('ps=' + encodeURIComponent(aChunkedText[i])); aQueryData.push('api_key=' + this.sApiKey); aQueryData.push('encoding=' + this.sCharset); var sQueryUrl = this.sHostUrl+'?'+aQueryData.join('&'); this.genScriptHtml(sQueryUrl); } } this.genScriptHtml = function(sQueryUrl) { var oHtmlScript = document.createElement('script'); var attr = document.createAttribute("type"); attr.value = "text/javascript"; oHtmlScript.setAttributeNode(attr); var attr = document.createAttribute("src"); attr.value = sQueryUrl; oHtmlScript.setAttributeNode(attr); //var attr = document.createAttribute("id"); //attr.value = 'mp_data_script'; //oHtmlScript.setAttributeNode(attr); //oHtmlScript.type = 'text/javascript'; //oHtmlScript.src = sQueryUrl; //alert(oHtmlScript); var body = document.getElementsByTagName('head')[0]; body.appendChild(oHtmlScript); } this.onSendChunk = function(o) { if (typeof o == 'undefined' || typeof o.r == 'undefined' || typeof o.status == 'undefined') return false; if (o.r == false) { return false; } this.iPartSendSuccess.count++; this.iPartSendSuccess.parts[o.part] = true; switch (o.status) { case "redirect": this.redirectToTranslate(); return false; break; case "start_translate": if (typeof o.chunk_count == 'undefined') { return false; } this.iChunkCount = o.chunk_count; this.iChunkSuccess.count = 0; this.getPartsTanslate(); break; } } this.getPartsTanslate = function() { if (this.iChunkCount == false) { return false; } for (var i =0; i < this.iChunkCount; i++) { var aQueryData = new Array(); aQueryData.push(this.sSnTranslate); aQueryData.push('uniqid=' + this.iCurrentUniqid); aQueryData.push('chunk=' + i); var sQueryUrl = this.sHostUrl+'?'+aQueryData.join('&'); this.genScriptHtml(sQueryUrl); } } this.onGetPartsTanslate = function(o){ if (typeof o == 'undefined' || typeof o.r == 'undefined') return false; if (o.r == false) { return false; } this.oChunksTranslate[o.chunk] = {}; this.oChunksTranslate[o.chunk].translate = o.translate; this.oChunksTranslate[o.chunk].index = o.index; this.oChunksTranslate[o.chunk].type = o.type; //alert(this.oChunksTranslate[o.chunk].toSource()); this.iChunkSuccess.count++; if (this.iChunkSuccess.count == this.iChunkCount) { this.combineChunks(); } } this.combineChunks = function(){ if (typeof this.oChunksTranslate != 'object' || this.oChunksTranslate.length < 1) { this.redirectToTranslate(); } if (false == this.checkLink) { this.redirectToTranslate(); } var sTranslate = ""; var iLastIndex = 0; for (var i in this.oChunksTranslate) { if (iLastIndex != this.oChunksTranslate[i].index) { sTranslate += "\r\n"; } sTranslate += (this.oChunksTranslate[i].type == "p" ? "" : " ") + this.oChunksTranslate[i].translate; iLastIndex = this.oChunksTranslate[i].index; } if (typeof this.callbackOnEndTranslate == 'function') { this.callbackOnEndTranslate(sTranslate); return true; } if (this.idTextTranslate != false) { var domTranslate = document.getElementById(this.idTextTranslate); if (typeof domTranslate == 'undefined') { this.redirectToTranslate(); } var tn = domTranslate.tagName; if (typeof this.tagNameValue[tn] != 'undefined' && this.tagNameValue[tn] == true) { domTranslate.value = sTranslate; } else { domTranslate.innerHTML = sTranslate; } return true; } this.redirectToTranslate(); } this.checkLink = function() { var domLink = document.getElementById(this.idMetaFormLink); if (typeof domLink == 'undefined') { return false; } return true; } this.redirectToTranslate = function() { if (!this.bAllowRedirect) { return false; } if (!oMetaTranslate.bRedirectForm) { window.location = this.sHostRedirectUrl + '?tuid=' + this.iCurrentUniqid; // window.open(this.sHostRedirectUrl + '?tuid=' + this.iCurrentUniqid, 'translate_result'); } return false; } } if (typeof oMetaTranslateInitData == 'undefined') { oMetaTranslateInitData = {}; } oMetaTranslateInitData.host_url = 'http://' + oMetaTranslateInitData.http_host +'/ajax/'; oMetaTranslateInitData.redirect_url = 'http://' + oMetaTranslateInitData.http_host +'/'; oMetaTranslate.init(oMetaTranslateInitData); function fMetaTranslateBind() { var domForm = document.getElementById(oMetaTranslate.idMetaForm); domForm.onsubmit = function () { return false; }; oMetaTranslate.idLangFrom = oMetaTranslate.idMetaFormLangFrom; oMetaTranslate.idLangTo = oMetaTranslate.idMetaFormLangTo; oMetaTranslate.idTextSource = oMetaTranslate.idMetaFormTa; var domFormSubmit = document.getElementById(oMetaTranslate.idMetaFormLangSubmit); domFormSubmit.onclick = function () { if (oMetaTranslate.bRedirectForm) { oMetaTranslate.translate(); document.getElementById(oMetaTranslate.idMetaForm).setAttribute('action', oMetaTranslate.sHostRedirectUrl + '?tuid=' + oMetaTranslate.iCurrentUniqid); document.getElementById(oMetaTranslate.idMetaForm).submit(); return false; } else { return oMetaTranslate.translate(); } }; } window.onload = function(){ fMetaTranslateBind(); };