summaryrefslogtreecommitdiff
path: root/js/codeq/translation.js
blob: e648ae3e761e34e027de99a2c3eca3be7ebf0cbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* CodeQ: an online programming tutor.
   Copyright (C) 2015 UL FRI

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

(function () {
    "use strict";
    var dicts = {},
        translateElement = function (jqElt, tkey, lang) {
            var dictionaryKey = jqElt.data('dict') || 'gui',
                translationKey = jqElt.data(tkey),
                dict = dicts[dictionaryKey],
                translations, html, key, att;
            if (dict === codeq.tr.emptyDictionary) return; // silent ignore
            if (!dict) {
                codeq.log.error('Cannot find translation dictionary ' + dictionaryKey);
                return;
            }
            if (!(typeof translationKey === 'number' || typeof translationKey === 'string')) {
                codeq.log.error('Cannot find the element\'s translation key, dictionary: ' + dictionaryKey);
                return;
            }
            translations = dict[translationKey];
            if (!translations) {
                codeq.log.error('Translation key ' + translationKey + ' is missing from dictionary ' + dictionaryKey);
                return;
            }
            html = translations[lang];
            if (!html) {
                html = translations['en'];
                if (html) {
                    codeq.log.info('There is no translation in language ' + lang + ' for key ' + translationKey + ' in dictionary ' + dictionaryKey + ', defaulting to language en');
                }
                else {
                    for (key in translations) {
                        if (!translations.hasOwnProperty(key)) continue;
                        html = translations[key];
                        if (!html) continue;
                        codeq.log.info('There is no translation in languages ' + lang + ' and en for key ' + translationKey + ' in dictionary ' + dictionaryKey + ', defaulting to language ' + key);
                        break;
                    }
                    if (!html) {
                        codeq.log.warn('There is no translation in any language for key ' + translationKey + ' in dictionary ' + dictionaryKey + ', leaving empty');
                        return;
                    }
                }
            }
            att = tkey.substring(5);
            if (att) jqElt.attr(att,html);
            else jqElt.html(html);
        },
        translateDocument = function (lang) {
            $('[data-tkey]').each(function () {
                translateElement($(this), 'tkey', lang);
            });
            $('[data-tkey-placeholder]').each(function () {
                translateElement($(this), 'tkey-placeholder', lang);
            });
            $('[data-tkey-title]').each(function () {
                translateElement($(this), 'tkey-title', lang);
            });
            $('[data-tkey-value]').each(function () {
                translateElement($(this), 'tkey-value', lang);
            });
            $('[data-tkey-data-content]').each(function () {
                translateElement($(this), 'tkey-data-content', lang);
            });
        };

    // Translate the whole document when the user switches the display language
    codeq.on('langchange', function (args) {
        translateDocument(args.lang);
    });

    // ================================================================================
    // The module API
    // ================================================================================

    codeq.tr = {
        'registerDictionary': function (name, dict) {
            dicts[name] = dict;
        },

        'unregisterDictionary': function (name) {
            delete dicts[name];
        },

        'emptyDictionary': {}, // use this with registerDictionary when you don't want any translations

        'translate': function (key, lang) {
            var dict = dicts['gui'];
            lang = lang || codeq.settings['gui_lang'];
            if (dict && dict[key] && dict[key][lang]) {
                return dict[key][lang];
            }
        },

        'translateDom': function (jqTopElt) {
            var lang = codeq.settings['gui_lang'];
            jqTopElt.filter('[data-tkey]').each(function () {
                translateElement(jqTopElt,'tkey', lang)
            });
            jqTopElt.find('[data-tkey]').each(function () {
                translateElement($(this),'tkey', lang);
            });
        }
    };
})();