Skip to content

Instantly share code, notes, and snippets.

@wiedymi
Last active January 3, 2020 07:16
Show Gist options
  • Save wiedymi/4589e8afb6271d3d69ba9a9a479c0db1 to your computer and use it in GitHub Desktop.
Save wiedymi/4589e8afb6271d3d69ba9a9a479c0db1 to your computer and use it in GitHub Desktop.
Japanese Transliteration For Belarusian Language
/* eslint-disable no-fallthrough */
const JapaneseLatin = {
ba: "ба",
cha: "ця",
da: "да",
ga: "га",
ha: "ха",
ja: "дзя",
ka: "ка",
ma: "ма",
na: "на",
pa: "па",
ra: "ра",
sa: "са",
sha: "ся",
ta: "та",
wa: "ўа",
ya: "я",
za: "дза",
be: "бэ",
de: "дэ",
ge: "гэ",
he: "хэ",
ke: "кэ",
me: "мэ",
ne: "нэ",
pe: "пэ",
re: "рэ",
se: "сэ",
te: "тэ",
ze: "дзэ",
bi: "бі",
chi: "ці",
gi: "гі",
hi: "хі",
ji: "дзі",
ki: "кі",
mi: "мі",
ni: "ні",
pi: "пі",
ri: "ры",
ro: "ро",
shi: "сі",
bo: "бо",
cho: "цё",
do: "до",
go: "го",
ho: "хо",
jo: "дзё",
ko: "ко",
mo: "мо",
no: "но",
po: "по",
so: "со",
sho: "сё",
to: "то",
wo: "ўо",
yo: "ё",
zo: "дзо",
bu: "бу",
fu: "фу",
gu: "гу",
ju: "дзю",
ku: "ку",
mu: "му",
nu: "ну",
pu: "пу",
ru: "ру",
su: "су",
chu: "цю",
shu: "сю",
tsu: "цу",
yu: "ю",
zu: "дзу",
bya: "бя",
gya: "гя",
hya: "хя",
kya: "кя",
mya: "мя",
nya: "ня",
pya: "пя",
rya: "р’я",
byo: "бё",
gyo: "гё",
hyo: "хё",
kyo: "кё",
myo: "мё",
nyo: "нё",
pyo: "пё",
ryo: "р’ё",
byu: "бю",
gyu: "гю",
hyu: "хю",
kyu: "кю",
myu: "мю",
nyu: "ню",
pyu: "пю",
ryu: "р’ю",
Cha: "Ця",
Ba: "Ба",
Da: "Да",
Ga: "Га",
Ha: "Ха",
Ja: "Дзя",
Ka: "Ка",
Ma: "Ма",
Na: "На",
Pa: "Па",
Ra: "Ра",
Sa: "Са",
Sha: "Ся",
Ta: "Та",
Wa: "Ўа",
Ya: "Я",
Za: "Дза",
Be: "Бэ",
De: "Дэ",
Ge: "Гэ",
He: "Хэ",
Ke: "Кэ",
Me: "Мэ",
Ne: "Нэ",
Pe: "Пэ",
Re: "Рэ",
Se: "Сэ",
Te: "Тэ",
Ze: "Дзэ",
Bi: "Бі",
Chi: "Ці",
Gi: "Гі",
Hi: "Хі",
Ji: "Дзі",
Ki: "Кі",
Mi: "Мі",
Ni: "Ні",
Pi: "Пі",
Ri: "Ры",
Ro: "Ро",
Shi: "Сі",
Bo: "Бо",
Cho: "Цё",
Di: "Ды",
di: "ды",
Do: "До",
Go: "Го",
Ho: "Хо",
Jo: "Дзё",
Ko: "Ко",
Mo: "Мо",
No: "Но",
Po: "По",
So: "Со",
Sho: "Сё",
To: "То",
Wo: "Ўо",
Yo: "Ё",
Zo: "Дзо",
Bu: "Бу",
Chu: "Цю",
Fu: "Фу",
Gu: "Гу",
Ju: "Дзю",
Ku: "Ку",
Mu: "Му",
Nu: "Ну",
Pu: "Пу",
Ru: "Ру",
Su: "Су",
Shu: "Сю",
Tsu: "Цу",
Yu: "Ю",
Zu: "Дзу",
Bya: "Бя",
Gya: "Гя",
Hya: "Хя",
Kya: "Кя",
Mya: "Мя",
Nya: "Ня",
Pya: "Пя",
Rya: "Р’я",
Byo: "Бё",
Gyo: "Гё",
Hyo: "Хё",
Kyo: "Кё",
Myo: "Мё",
Nyo: "Нё",
Pyo: "Пё",
Ryo: "Р’ё",
Byu: "Бю",
Gyu: "Гю",
Hyu: "Хю",
Kyu: "Кю",
Myu: "Мю",
Nyu: "Ню",
Pyu: "Пю",
Ryu: "Р’ю",
Ue: "Уэ",
ue: "уэ",
Ua: "Уа",
ua: "уа",
Ui: "Уі",
ui: "уі",
ii: "і",
II: "і",
Uo: "Уо",
uo: "уо",
"?": "?",
".": ".",
",": ","
};
class Translit {
constructor(text) {
this.text = text.toLowerCase();
}
toAkanne() {
this.text = this.text.replace(/(.*)\о/, x => {
return x.replace(/\о/g, "а").slice(0, -1) + "о";
});
return this;
}
toJotacija() {
this.text = this.text.replace(/\іо/g, "іё").replace(/\ыо/g, "ыё");
return this;
}
toJakanne() {
this.text = this.text.replace(/\іа/g, "ія").replace(/\ыа/g, "ыя");
return this;
}
lognSoundRemover() {
const sklady = ["оў", "аў", "ёў", "яў", "юў"];
const reg = value => {
const replace = value;
return new RegExp(replace, "g");
};
const isExist = sklady.map(value => {
return this.text.includes(value);
});
if (isExist.indexOf(true) !== -1) {
sklady.forEach(value => {
const replace = reg(value);
this.text = this.text.replace(replace, value.slice(0, -1));
return this;
});
return this;
}
return this;
}
capitalize() {
if (this.text.includes(" ")) {
const result = this.text.split(" ").map(word => {
if (word.charAt(0) === "ў") {
return "У" + word.slice(1);
}
return word.charAt(0).toUpperCase() + word.slice(1);
});
if (result.includes("\n")) {
this.text = result
.split("\n")
.map(word => word.charAt(0).toUpperCase() + word.slice(1));
return this;
}
this.text = result.join(" ");
return this;
}
if (this.text.charAt(0) === "ў") {
this.text = "У" + this.text.slice(1);
return this;
}
this.text = this.text.charAt(0).toUpperCase() + this.text.slice(1);
return this;
}
oneDigit() {
const oneDigit = {
o: "о",
O: "О",
a: "а",
i: "й",
n: "н",
e: "э",
E: "Э",
u: "ў",
U: "У",
A: "А",
I: "Й",
N: "Н",
"?": "?",
".": ".",
",": ","
};
const jp = Object.entries(oneDigit);
let tranlit = this.text;
jp.forEach(value => {
const [en, be] = value;
if (tranlit.includes(en)) {
tranlit = tranlit.replace(en, be);
}
});
this.text = tranlit;
return this;
}
addLastWa() {
const { text } = this;
if (text.includes(" ")) {
let newText = text.split(" ");
newText = newText.map(word => {
if (word.includes("ўа") && word.length - 2 === word.indexOf("ўа")) {
return word.replace(/..$/, "ва");
}
return word;
});
this.text = newText.join(" ");
}
if (text.includes("ўа")) {
this.text = text.replace(/..$/, "ва");
}
return this;
}
get() {
return this.text;
}
}
export const japaneseToBelarusian = text => {
String.prototype.replaceAll = function(search, replacement) {
const target = this;
return target.split(search).join(replacement);
};
const jp = Object.entries(JapaneseLatin);
let tranlit = text;
const sorter = (a, b) => b[0].length - a[0].length;
jp.sort(sorter).forEach(value => {
const [en, be] = value;
if (tranlit.includes(en)) {
tranlit = tranlit.replaceAll(en, be);
}
});
tranlit = new Translit(tranlit);
return tranlit
.oneDigit()
.addLastWa()
.lognSoundRemover()
.oneDigit()
.lognSoundRemover()
.toJotacija()
.toAkanne()
.toJakanne()
.capitalize()
.get();
};
import { japaneseToBelarusian } from "./translit";
describe("Test Translit", () => {
it("Uesugi", () => {
expect(japaneseToBelarusian("Uesugi Youki")).toEqual("Уэсугі Ёкі");
});
it("Chio", () => {
expect(japaneseToBelarusian("Chio")).toEqual("Ціё");
});
it("Ono", () => {
expect(japaneseToBelarusian("Ono")).toEqual("Ано");
});
it("Tokio", () => {
expect(japaneseToBelarusian("Tokio")).toEqual("Токіё");
});
it("Dio", () => {
expect(japaneseToBelarusian("Dio")).toEqual("Дыё");
});
it("Fumio", () => {
expect(japaneseToBelarusian("Fumio")).toEqual("Фуміё");
});
it("Noriaki", () => {
expect(japaneseToBelarusian("Noriaki")).toEqual("Норыякі");
});
it("Naruto", () => {
expect(japaneseToBelarusian("Naruto")).toEqual("Наруто");
});
it("Gorou", () => {
expect(japaneseToBelarusian("Gorou")).toEqual("Гаро");
});
it("Shinpachi Shimura", () => {
expect(japaneseToBelarusian("Shinpachi Shimura")).toEqual("Сінпаці Сімура");
});
it("Ayame Sarutobi", () => {
expect(japaneseToBelarusian("Ayame Sarutobi")).toEqual("Аямэ Сарутобі");
});
it("Gedoumaru", () => {
expect(japaneseToBelarusian("Gedoumaru")).toEqual("Гэдомару");
});
it("Gengai Hiraga", () => {
expect(japaneseToBelarusian("Gengai Hiraga")).toEqual("Гэнгай Хірага");
});
it("Hatsu Hasegawa", () => {
expect(japaneseToBelarusian("Hatsu Hasegawa")).toEqual("Хацу Хасэгава");
});
it("Hime Onizuka", () => {
expect(japaneseToBelarusian("Hime Onizuka")).toEqual("Хімэ Онідзука");
});
it("Kazuyoshi Usui", () => {
expect(japaneseToBelarusian("Kazuyoshi Usui")).toEqual("Кадзуёсі Усуй");
});
it("Katakuriko Matsudaira", () => {
expect(japaneseToBelarusian("Katakuriko Matsudaira")).toEqual(
"Катакурыко Мацудайра"
);
});
it("Kyuubei Yagyuu", () => {
expect(japaneseToBelarusian("Kyuubei Yagyuu")).toEqual("Кюбэй Ягю");
});
it("Tae Shimura", () => {
expect(japaneseToBelarusian("Tae Shimura")).toEqual("Таэ Сімура");
});
it("Watashiwa", () => {
expect(japaneseToBelarusian("Watashiwa")).toEqual("Уатасіва");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment