Ventures of an ex indie game developer

Do it yourself

A few days ago I started thinking about a small tool that could help the Ukrainians. Namely a map where you can report enemy sightings, in order to help the Ukrainian army and possibly also civilians.


Anyway, I needed a few strings translated. There are a ton of tools for doing such things, but... it's so easy to do, and hardly any code is required to make one yourself. So I built a script for extracting the strings to be translated — 13 lines of code. And then some JavaScript+jQuery to do the actual replacement — 8 lines of code. All that remained was to make the manual translations myself. This is the JS code:

function xlat() {
    dict = {
        "Enemy spotted": {
            "sv": "Fiende siktad",
            "ru": "Враг замечен",
            "uk": "Ворог помічений"
        },
        ...
    };

    $(".xlat").each(function() {
        let text = $(this).text();
        let key = text || $(this).val();
        let o = dict[key];
        let lang = (navigator.language || navigator.userLanguage).replace(/-.*/, '');
        let s = o && o[lang] || key;
        text ? $(this).text(s) : $(this).val(s);
    });
}

Using dynamic programming languages that are flexible and powerful are great for adding data transformation features. And although I lack standard tooling for editing translations, the pros of a minimal implementation far outweigh the cons of adding third party bloat.

This is a generic tip for small applications: if you can implement something yourself using less than 30 lines of code instead of adding a dependency — do it!

About the author

Mitt foto
Gothenburg, Sweden