Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Add Russian & Ukrainian transliteration support (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanglam2807 authored May 8, 2020
1 parent 4fde36e commit feb7171
Show file tree
Hide file tree
Showing 4 changed files with 436 additions and 49 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"cli-truncate": "2.1.0",
"cyrillic-to-translit-js": "3.1.0",
"electron-is-dev": "1.2.0",
"electron-settings": "3.2.0",
"electron-updater": "4.3.1",
Expand Down
31 changes: 8 additions & 23 deletions src/helpers/translate-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { createConverterMap } from 'tongwen-core';
import s2tChar from 'tongwen-core/dictionaries/s2t-char.json';
import s2tPhrase from 'tongwen-core/dictionaries/s2t-phrase.json';

// https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/
import { transliterate as tr } from 'transliteration';
import { transliterate as hebTr } from 'hebrew-transliteration';
import transliterate from './transliterate';

// https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/
// https://tech.yandex.com/dictionary/doc/dg/reference/getLangs-docpage/
const dictPairs = [
'be-be', 'be-ru', 'bg-ru', 'cs-cs', 'cs-en', 'cs-ru', 'da-en',
Expand Down Expand Up @@ -95,28 +94,14 @@ const translateTextWithYandex = (inputLang, outputLang, inputText) => {
output.inputText = inputText;
output.outputText = outputLang === 'zh-TW' ? mConv.phrase('s2t', response.text[0]) : response.text[0];

if (outputLang.startsWith('zh')) {
const outputRoman = tr(response.text[0]);
if (outputRoman !== response.text[0]) {
output.outputRoman = outputRoman;
}
} else if (outputLang === 'he') {
const outputRoman = hebTr(response.text[0], { qametsQatan: true });
if (outputRoman !== response.text[0]) {
output.outputRoman = outputRoman;
}
const outputRoman = transliterate(response.text[0], outputLang);
if (outputRoman !== response.text[0]) {
output.outputRoman = outputRoman;
}

if (inputLang === 'zh') {
const inputRoman = tr(inputText);
if (inputRoman !== inputText) {
output.inputRoman = inputRoman;
}
} else if (inputLang === 'he') {
const inputRoman = hebTr(inputText, { qametsQatan: true });
if (inputRoman !== inputText) {
output.inputRoman = inputRoman;
}
const inputRoman = transliterate(inputText, inputLang);
if (inputRoman !== inputText) {
output.inputRoman = inputRoman;
}

if (output.outputRoman === output.outputText) output.outputRoman = undefined;
Expand Down
26 changes: 26 additions & 0 deletions src/helpers/transliterate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { transliterate as tr } from 'transliteration';
import { transliterate as hebTr } from 'hebrew-transliteration';
import cyrillicToTranslit from 'cyrillic-to-translit-js';

const transliterate = (text, lang) => {
// Chinese
if (lang.startsWith('zh')) {
return tr(text[0]);
}
// Hebrew
if (lang === 'he') {
return hebTr(text, { qametsQatan: true });
}
// Russian
if (lang === 'ru') {
return cyrillicToTranslit({ preset: 'ru' }).transform(text);
}
// Ukrainian
if (lang === 'uk') {
return cyrillicToTranslit({ preset: 'uk' }).transform(text);
}

return text;
};

export default transliterate;
Loading

0 comments on commit feb7171

Please sign in to comment.