All files / web/bundles/pimui/js translator.js

75.86% Statements 22/29
33.33% Branches 4/12
83.33% Functions 5/6
75.86% Lines 22/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 911183x       1183x 1183x 1183x 1183x 1183x   1183x 1183x               1183x 1703492x 1703492x                   1183x 966957x   966957x                   1183x 1162x     1162x   1162x                   966957x 966957x                       1183x                             1183x    
define(['module', 'underscore', 'translator-lib', 'json'],
function (module, _, Translator) {
    'use strict';
 
    var dict = {};
    var debug = false;
    var add = Translator.add;
    var get = Translator.get;
    var fromJSON = Translator.fromJSON;
 
    Translator.placeHolderPrefix = '{{ ';
    Translator.placeHolderSuffix = ' }}';
 
    /**
     * Adds a translation to Translator object and stores
     * translation id in protected dictionary
     *
     * @param {string} id
     */
    Translator.add = function (id) {
        dict[id] = 1;
        add.apply(Translator, arguments);
    };
 
    /**
     * Fetches translation by its id,
     * but before checks if the id was registered in dictionary
     *
     * @param {string} id
     * @returns {string}
     */
    Translator.get = function (id) {
        checkTranslation(id);
 
        return get.apply(Translator, arguments);
    };
 
    /**
     * Parses JSON data in store translations inside,
     * also turns on debug mode if in data was such directive
     *
     * @param {Object} data
     * @returns {Object} Translator
     */
    Translator.fromJSON = function (data) {
        Iif (typeof data === 'string') {
            data = JSON.parse(data);
        }
        debug = data.debug || false;
 
        return fromJSON.call(Translator, data);
    };
 
    /**
     * Checks if translation for passed id exist, if it's debug mode
     * and there's no translation - output error message in console
     *
     * @param {string} id
     */
    function checkTranslation(id) {
        Eif (!debug) {
            return;
        }
        var domains = Translator.defaultDomains;
        var checker = function (domain) {
            return dict.hasOwnProperty(domain ? domain + ':' + id : id);
        };
        domains = _.union([undefined], _.isArray(domains) ? domains : [domains]);
        if (!_.some(domains, checker)) {
            console.error('Untranslated: %s', id);
        }
    }
 
    _.mixin({
        /**
         * Shortcut for Translator.get() method call,
         * Due to it's underscore mixin, it can be used inside templates
         * @returns {string}
         */
        __: _.bind(Translator.get, Translator)
    });
 
    /**
     * Shortcut for Translator.get() method call
     *
     * @export oro/translator
     * @returns {string}
     */
    return _.__;
});