Ngaganti basa dina website atawa blog ku basa Sunda.
Kode nu samemehna dipake pikeun
ngaganti basa Facebook ku basa Sunda mun diseselkeun jadi wijet (
widget) atawa dina
template blog anjeun samemeh tulisan
/body bakal bisa ngaganti kecap nu aya dina kacaloka anjeun.
kodena
Kode dina wangun javascript nu eusina kurang leuwih kieu:
1. variable kecap nu rek ditarjamahkeun jeung tarjamahanana
<script type="text/javascript">
var words={
"Beranda":"Tepas","tangan":"leungeun","wait a minutes":"antos heula", // tuluykeun ku anjeun...
"":""
};
2. kode pungsi
replace text
String.prototype.prepareRegex = function () {
return this.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, "\\$1")
};
function isOkTag(a) {
return ("pre,blockquote,code,input,button,textarea, form, input, label ".indexOf("," + a) == -1)
}
var regexs = new Array(),
replacements = new Array();
for (var word in words) {
if (word != "") {
regexs.push(new RegExp("\\b" + word.prepareRegex()
.replace(/\*/g, '[^ ]*') + "\\b", 'gi'));
replacements.push(words[word])
}
}
var texts = document.evaluate(".//text()[normalize-space(.)!='']", document.body, null, 6, null),
text = "";
for (var i = 0, l = texts.snapshotLength;
(this_text = texts.snapshotItem(i)); i++) {
if (isOkTag(this_text.parentNode.tagName.toLowerCase()) && (text = this_text.textContent)) {
for (var x = 0, l = regexs.length; x < l; x++) {
text = text.replace(regexs[x], replacements[x]);
this_text.textContent = text
}
}
} </script>
Kode di luhur bakal ngaganti kecap 'Beranda' ku Tepas', 'tangan' ku 'leungeun', jeung 'wait a minutes' ku 'antos heula'. Anjeun bisa nangtukeun atawa nambahan kecap nu rek digantina sakahayang. Salian ti kecap oge bisa mangrupa frase atawa kalimah. Pola nuliskeuna: kecap nu rek diganti jeung kecap gagantina kudu aya dina jero kekenteng (tanda kutip) dipisahkeun ku titik dua (:), unggal kecap (jeung gagantina) anu beda kudu dipisahkeun ku koma (,).
3. HTML lengkep
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Blog ku Basa Sunda</title>
</head>
<body>
<!-- ieu conto kecap-kecap nu rek ditarjamahkeun -->
<p>Aku melangkah ke beranda. "Wait a minutes!" kata Obama. Tangannya melambai.<p>
<script type="text/javascript">
var words={
"Aku":"Kuring","melangkah":"ngalengkah","ke":"ka","beranda":"tepas","tangan":"leungeun","Wait a minutes":"Antos heula sakedap", "kata Obama":"ceuk Obama","Tangannya":"Leungeunna","melambai":"gugupay", // jeung saterusna...
"":""
};
// kode pungsi replace text nu dipadetkeun
String.prototype.prepareRegex=function(){return this.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g,"\\$1")};function isOkTag(a){return("pre,blockquote,code,input,button,textarea, form, input, label ".indexOf(","+a)==-1)}var regexs=new Array(),replacements=new Array();for(var word in words){if(word!=""){regexs.push(new RegExp("\\b"+word.prepareRegex().replace(/\*/g,'[^ ]*')+"\\b",'gi'));replacements.push(words[word])}}var texts=document.evaluate(".//text()[normalize-space(.)!='']",document.body,null,6,null),text="";for(var i=0,l=texts.snapshotLength;(this_text=texts.snapshotItem(i));i++){if(isOkTag(this_text.parentNode.tagName.toLowerCase())&&(text=this_text.textContent)){for(var x=0,l=regexs.length;x<l;x++){text=text.replace(regexs[x],replacements[x]);this_text.textContent=text}}}
</script>
</body>
</html>
4. Hasilna
Kuring ngalengkah ka tepas. "Antos heula sakedap!" ceuk Obama. Leungeunna gugupay.
Anjeun bisa nyobaan ngaropea kodena sacara langsung dina fiddle.
alpukahna jsfiddle