mirror of
https://gitlab.com/octtspacc/OcttKB
synced 2025-04-17 00:37:18 +02:00
69 lines
5.9 KiB
Plaintext
69 lines
5.9 KiB
Plaintext
created: 20231105210128175
|
|
creator: Octt
|
|
list-after: $:/core/ui/ViewTemplate/tags
|
|
modified: 20231108163056575
|
|
modifier: Octt
|
|
tags: $:/tags/ViewTemplate
|
|
title: $:/Styles/View/i18n
|
|
|
|
\define OcttKB-i18n()
|
|
<<script script='
|
|
!function(){var e,t,n,r,o={},a="object"==typeof module&&"object"==typeof module.exports,i="undefined"!=typeof window&&void 0!==window.document,s={lingva:["google"],mozhi:["deepl","duckduckgo","mymemory","reverso","watson","yandex"]};function l(e,t,n=[]){var r=0;return function e(t,n,o){for(var a,i,s=[],l=0,u=t.childNodes.length;l<u;l++){var d=t.childNodes[l];1!==d.nodeType||(i=void 0,(a="notranslate").getAttribute&&a.getAttribute("class").toLowerCase().replace(/\t/g," ").split(" ").includes(i))||n.excludeTags.includes(d.tagName.toLowerCase())?3===d.nodeType&&""!==d.data.trim()&&(o.length>0&&(d.data=o[r]),s.push(d.data),r++):(n.includeAttrs.forEach(function(e){var t=d.getAttribute(e);t&&(o.length>0&&d.setAttribute(e,o[r]),s.push(t),r++)}),s=s.concat(e(d,n,o)))}return s}(e,t,n)}a&&!i&&(n=require("jsdom").JSDOM,r=require("sync-request"),e=(e=>new n(e).window.document),t=(e=>{var t=r("GET",e);return{response:t,text:t.getBody("utf8")}})),i&&(e=function(e){return(new DOMParser).parseFromString(e,"text/html")},t=function(e){var t=new XMLHttpRequest;return t.open("GET",e,!1),t.send(),{response:t,text:t.responseText}}),o.TranslateHtml=function(n,r,o={}){o.languageFrom||(o.languageFrom="auto"),o.instances||(o.instances=[{type:"Lingva",url:"https://lingva.garudalinux.org",cors:!0},{type:"Lingva",url:"https://lingva.lunar.icu",cors:!0},{type:"Mozhi",url:"https://translate.projectsegfau.lt",cors:!1},{type:"Mozhi",url:"https://mozhi.aryak.me",cors:!1},{type:"Mozhi",url:"https://translate.bus-hit.me",cors:!1}]),o.engines||(o.engines=["DeepL","DuckDuckGo","Google"]),o.maxFragmentSize||(o.maxFragmentSize=900),o.endsOfSentence||(o.endsOfSentence=[".",":",";","!","?"]),o.excludeTags||(o.excludeTags=["script","style"]),o.includeAttrs||(o.includeAttrs=["title","alt"]);for(var a=e(n),i=l(a,o),u="";i.join("").includes(u);)u+=Math.random().toString().slice(-1)[0];u=`{ ${u} }`;for(var d=i.join(u),c="",g=[""],p=0,m=0;m<d.length;m++){var h=g.length-1;g[h]+=d[m];var f=g[h].length>=o.maxFragmentSize,v=o.endsOfSentence.includes(d[m])&&d[m]!==d[m+1];v&&!f?p=g[h].length:v&&f&&(g.push(""),g[h+1]=g[h].slice(p),g[h]=g[h].slice(0,p))}var y,w,x={},M=0,S=o.instances[Math.floor(Math.random()*o.instances.length)],T=S.type.toLowerCase();o.engines=o.engines.filter(function(e){return-1!==s[T].indexOf(e.toLowerCase())});var z=o.engines[Math.floor(Math.random()*o.engines.length)].toLowerCase();"lingva"===T?w=`${S.url}/api/v1/${o.languageFrom}/${r}/`:"mozhi"===T&&(w=`${S.url}/api/translate?engine=${z}&from=${o.languageFrom}&to=${r}&text=`);for(m=0;m<g.length;m++){var L;try{x=JSON.parse(t(`${w}${encodeURIComponent(g[m])}`).text),"lingva"===T?(y=x.translation,x.info.detectedSource):"mozhi"===T&&(y=x["translated-text"],x.detected)}catch(e){M++,L=e}if(y||M++,M>0){if(M>=3&&M%3==0)return{error:L};if(M>=9)return{error:L};m--}else c+=y+" ",M=0}return l(a,o,c.split(u)),{instance:S,engine:z,html:a.documentElement.outerHTML}},"object"==typeof module&&"object"==typeof module.exports&&(module.exports=o),"undefined"!=typeof window&&void 0!==window.document&&(window.TiddlyCoso_i18n=o)}();
|
|
'>>
|
|
|
|
<<script script="try{
|
|
var BodySplitter = '</p>>';
|
|
var TidTitle = this.variables.thisTiddler.value;
|
|
var TidFrame = this.parentDomNode.parentElement;
|
|
/* var [TidTitleEl, TidBodyEl] = this.parentDomNode.parentElement.querySelectorAll('.tc-tiddler-title, .tc-tiddler-body'); */
|
|
var TidLangKnown = $tw.wiki.getTiddler(TidTitle).fields['i18n-main'];
|
|
this.parentDomNode.innerHTML += '[' + (TidLangKnown ? TidLangKnown : '?') + '] ';
|
|
var input = document.createElement('input');
|
|
input.type = 'text';
|
|
input.style['max-width'] = '4em';
|
|
input.value = (window['OcttKB-i18n-current'] ? window['OcttKB-i18n-current'] : 'it');
|
|
input.onchange = function(){ window['OcttKB-i18n-current'] = input.value };
|
|
this.parentDomNode.appendChild(input);
|
|
var btn = document.createElement('button');
|
|
btn.innerHTML = 'Translate [Beta]';
|
|
/* TODO: make this not lock up the main thread */
|
|
btn.onclick = function(){
|
|
var TidBodyEl = TidFrame.querySelector('.tc-tiddler-body');
|
|
var TidBody = TidBodyEl.innerHTML.trim();
|
|
/* var TidBodyParags = TidBody.trim().replaceAll('>', '>>').split(BodySplitter); */
|
|
var TidBodyTranslated = '';
|
|
/* TODO: should probably fix the splitting to make it do longer segments, otherwise translations are more likely broken, and also find a good way to avoid the occasional translating of HTML elements; we should ideally do like the GTranslate embeddable script and parse the doctree to translate only the innerHTML strings */
|
|
/* for (var i=0; i<TidBodyParags.length; i++) {
|
|
if (TidBodyParags[i].trim()) {
|
|
var req = new XMLHttpRequest();
|
|
req.open('GET', `https://lingva.garudalinux.org/api/v1/${TidLangKnown ? TidLangKnown : 'auto'}/${input.value}/${encodeURIComponent(TidBodyParags[i].trim())}`, false);
|
|
req.send();
|
|
var jsonResponse = JSON.parse(req.responseText);
|
|
TidBodyTranslated += jsonResponse.translation + BodySplitter;
|
|
}
|
|
} */
|
|
var TransBodyEl = document.createElement('div');
|
|
TransBodyEl.innerHTML = TiddlyCoso_i18n.TranslateHtml(TidBody, input.value).html;
|
|
/* TransBodyEl.innerHTML += TidBodyTranslated.replaceAll('>>', '>'); */
|
|
TidFrame.appendChild(TransBodyEl);
|
|
TidBodyEl.style.display = 'none';
|
|
btn.style.display = 'none';
|
|
|
|
var close = document.createElement('button');
|
|
close.innerHTML = '❌ Close';
|
|
close.onclick = function(){
|
|
TidFrame.removeChild(TransBodyEl);
|
|
TidBodyEl.style.display = 'revert';
|
|
btn.style.display = 'revert';
|
|
this.parentElement.removeChild(close);
|
|
};
|
|
this.parentElement.appendChild(close);
|
|
};
|
|
this.parentDomNode.appendChild(btn);
|
|
} catch(e) { ErrAtLine(e); };
|
|
">><<script off>>
|
|
\end
|
|
|
|
<div class="OcttKB-i18n" style="font-size: initial;">
|
|
<<OcttKB-i18n>>
|
|
</div> |