Nikita Kazakov
Nikita Kazakov
3 min read

Tags

Although I can write in Russian, I do not know the Russian keyboard fluently. It takes forever to type in Russian. What if it didn’t have to be that way?

What if there was a way for me to type in English and equivalent Russian letters would magically appear?

That’s what this blog post is about.

It’s about getting typing: Privet drug. Menya zavut Nikita.

And getting back: Привет друг. Меня завут Никита.

Method 1

Use the Fast Latin-to-Russian Characters Firefox Extension.

Once you activate it, start typing in English. THe problem is that you have to wait 2 seconds for it convert what you typed into Russian. I don’t like the delay.

The other problem is that the mouse cursor goes back to the beginning of the first character. It’s hard to continuously type like this.

Method 2

Use translit.cc. It’s real-time English to Russian character conversion. It’s great if you’re writing a letter. The cursor stays where you expect it to.

The problem is that I want to take advantage of the auto-suggestions that come up on YouTube and Google as I type. I can’t do it with this method because I have to type on one screen and then copy and paste into another.

Method 3

You can have real-time transliteration on any page without delay. I glued this method together and this is the one I actively use.

We’re going to run javascript code in the browser to make a text field automatically transliterate.

I found Lenny Domnitser English to Russian transliteration Javascript code and I’m using it below.

My addition to his code is listening to the last clicked element on the page and enabling the transliterator on it.

Here’s the entire piece of code.

// Full thanks to Lenny Domnitser for this Transliterator code.
// https://domnit.org/blog/2006/11/translit.html
function bind(func, this_) {
    return function() {
        func.apply(this_, arguments);
    };
}

function Transliterator(input) {
    this.input = input;
    this.keyListener = bind(this.keyListener, this);
    function clearLastKey() {
        delete this.lastChar;
    }
    clearLastKey = bind(clearLastKey, this);
    input.addEventListener('click', clearLastKey, false);
    input.addEventListener('blur', clearLastKey, false);
}

Transliterator.lookup = {
    'A': '\u0410',                       'a': '\u0430',
    'B': '\u0411',                       'b': '\u0431',
    'V': '\u0412',                       'v': '\u0432',
    'G': '\u0413',                       'g': '\u0433',
    'D': '\u0414',                       'd': '\u0434',
    'E': '\u0415',                       'e': '\u0435',
    'Yo': '\u0401',                      'yo': '\u0451',
    'Zz': '\u0416',                      'zh': '\u0436',
    'Z': '\u0417',                       'z': '\u0437',
    'I': '\u0418',                       'i': '\u0438',
    'J': '\u0419',                       'j': '\u0439',
    'K': '\u041A',                       'k': '\u043A',
    'L': '\u041B',                       'l': '\u043B',
    'M': '\u041C',                       'm': '\u043C',
    'N': '\u041D',                       'n': '\u043D',
    'O': '\u041E',                       'o': '\u043E',
    'P': '\u041F',                       'p': '\u043F',
    'R': '\u0420',                       'r': '\u0440',
    'S': '\u0421',                       's': '\u0441',
    'T': '\u0422',                       't': '\u0442',
    'U': '\u0423',                       'u': '\u0443',
    'F': '\u0424',                       'f': '\u0444',
    'X': '\u0425', 'H': '\u0425',        'x': '\u0445', 'h': '\u0445',
    'C': '\u0426', 'Ts': '\u0426',       'c': '\u0446', 'ts': '\u0446',
    'Ch': '\u0427',                      'ch': '\u0447',
    'Sh': '\u0428',                      'sh': '\u0448',
    'w': '\u0429',                       'W': '\u0449',
    '""': '\u042A',                      "''": '\u044A',
    'Y': '\u042B',                       'y': '\u044B',
    '"': '\u042C',                       "'": '\u044C',
    'Je': '\u042D',                      'je': '\u044D',
    'Ju': '\u042E', 'Yu': '\u042E',      'ju': '\u044E', 'yu': '\u044E',
    'Ja': '\u042F', 'Ya': '\u042F',      'ja': '\u044F', 'ya': '\u044F'
};

Transliterator.prototype.keyListener = function(event) {
    if(event.ctrlKey || event.altKey || event.metaKey) {
        return;
    }
    var sl = this.input.scrollLeft;
    var st = this.input.scrollTop;
    var curChar = String.fromCharCode(event.which);
    var rusText = Transliterator.lookup[this.lastChar + curChar.toLowerCase()];
    if(rusText) {
        event.preventDefault();
        var ss = this.input.selectionStart;
        this.input.value = this.input.value.substring(0, this.input.selectionStart - 1) + rusText + this.input.value.substring(this.input.selectionEnd, this.input.textLength);
        this.input.selectionStart = ss;
        this.input.selectionEnd = ss;
    } else {
        rusText = Transliterator.lookup[curChar];
        if(rusText) {
            event.preventDefault();
            var ss = this.input.selectionStart;
            this.input.value = this.input.value.substring(0, this.input.selectionStart) + rusText + this.input.value.substring(this.input.selectionEnd, this.input.textLength);
            this.input.selectionStart = ss + 1;
            this.input.selectionEnd = ss + 1;
        }
    }
    if(rusText) {
        this.input.scrollLeft = sl;
        this.input.scrollTop = st;
    }
    this.lastChar = curChar;
};

Transliterator.prototype.enable = function() {
    this.input.addEventListener('keypress', this.keyListener, false);
};

Transliterator.prototype.disable = function() {
    this.input.removeEventListener('keypress', this.keyListener, false);
};

// Transliterator is enabled for the last active element. The one we last clicked on.
new Transliterator(document.activeElement).enable();

The last key to this puzzle is to use the ShortcutKey2URL Firefox Extension to inject this code into a page.

Install the extension and edit the settings as shown in the screenshot. Paste in that big block of code from above into the script section of the extension.

Settings in ShortcutKey2URL Extension
Settings in ShortcutKey2URL Extension

Now go to YouTube and click on the search textbox. Press cmd + . and then /. Start typing. It will dynamically convert what you type into Russian!

Real-time transliteration
Real-time transliteration