Convert text to and from Serbian letters
How do I add the Serbian Cyrillic alphabet to my HTML so that my browser will recognize it?
I need, for example, to make "Povrce" in "Damage".
I just need the code, so when I type "Damage" or "Povrce", the browser can show it.
source to share
What do you mean transliterating latin for Serbian Cyrillic (or vice versa). This is not a problem, since transliteration is a reversible transformation one character at a time (while the transcription is phonetic). Just set up the "associative" object with alphabet and then map()
accordingly. Has some proof of concept:
var latinString = 'Povrce';
var latinToSerbian = { "P":"", "o":"", "v":"", "r":"", "c":"Ρ", "e":"" /* ... */ };
var serbianString = latinString.split('').map(function(character){
return latinToSerbian[character];
}).join('');
console.log( latinString + ' = ' + serbianString ); // Povrce = Ρ
For HTML, of course, there are always entities to resort to. By looking at the Cyrillic Unicode block , you can easily translate characters into their decimal or hexadecimal code points:
element.innerHTML = 'Поврће';
element.onclick = function(){ alert('\u041F\u043E\u0432\u0440\u045B\u0435'); };
If you want transliteration on the fly when typing on a website, use charCodeAt () , <input>
element for the entered text, and something like strings:
var latinToCyrillic = { "80": 1055 /* entire alphabet */ };
var cyrillicToLatin = { "1115" : 263 /* entire alphabet */ };
var toCyrillic = function(character){
return String.fromCharCode(latinToCyrillic[character.charCodeAt(0)]);
};
var toLatin = function(character){
return String.fromCharCode(cyrillicToLatin[character.charCodeAt(0)]);
};
console.log(
toCyrillic('P'), // === ""
toLatin('Ρ') // === "Δ"
);
source to share
I made this solution, a little simple, but maybe it can help you:
var pp='VOΔE POVRΔE DINJA';
var ss=["NJ","V","O","Δ","E","P","R","D","I","A"];
var cyr=["Π","","","Π","","","","","",""];
for(var i=0;i<ss.length;i++) {
var tt=cyr[i];
pp=pp.replace(new RegExp(ss[i], "g"),tt);
}
There is a jsfiddle example, also
The meanings of the characters in ss
and cyr
are important. So, first place characters like lj
and nj
.
Update: By using a text box and after losing focus, the phrase will be converted. Of course, you have to put all characters in arrays.
function chChar(ele) {
var pp=ele.value;
var ss=["NJ","V","O","Δ","E","P","R","D","I","A"];
var cyr=["Π","","","Π","","","","","",""];
for(var i=0;i<ss.length;i++) {
var tt=cyr[i];
pp=pp.replace(new RegExp(ss[i], "gi"),tt);
}
document.getElementById('cyr').innerHTML=pp;
}
<input type="text" onblur="chChar(this);" /><br>
<div id="cyr"></div>
source to share
A complete match for the transliteration of a language from the wikipedia list , including uppercase and lowercase, simply because no one else has listed it. Depending on the direction of transliteration, flip the display (currently Cyrillic-> Latin).
const langmap = {
"": "A",
"": "B",
"": "V",
"": "G",
"": "D",
"Π": "Δ",
"": "E",
"": "Ε½",
"": "Z",
"": "I",
"Π": "J",
"": "K",
"": "L",
"Π": "Lj",
"": "M",
"": "N",
"Π": "Nj",
"": "O",
"": "P",
"": "R",
"": "S",
"": "T",
"Π": "Δ",
"": "U",
"": "F",
"": "H",
"": "C",
"": "Δ",
"Π": "DΕΎ",
"": "Ε ",
"": "a",
"": "b",
"": "v",
"": "g",
"": "d",
"Ρ": "Δ",
"": "e",
"": "ΕΎ",
"": "z",
"": "i",
"Ρ": "j",
"": "k",
"": "l",
"Ρ": "lj",
"": "m",
"": "n",
"Ρ": "nj",
"": "o",
"": "p",
"": "r",
"": "s",
"": "t",
"Ρ": "Δ",
"": "u",
"": "f",
"": "h",
"": "c",
"": "Δ",
"Ρ": "dΕΎ",
"": "Ε‘",
}
function remapLang (str) {
return str.replace(/[^\u0000-\u007E]/g, function(a){
return langmap[a] || a;
});
}
Then testing the eyeball:
var tests = [
"First name: , Last name: .",
// --> First name: GEORGI, Last name: KOSTADINOV.
"First name: , Last name: ."
// --> First name: Dimitr, Last name: Stoev
];
tests.map(remapLang).forEach(console.log);
I would like to point out that the tests above are real life examples, so the wiki seems to be missing an equivalent to the "deprecated" (?) "Hard sign" b that I think people still use? YMMV ...
source to share