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

80.19% Statements 85/106
41.25% Branches 106/257
90.91% Functions 10/11
80.19% Lines 85/106

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 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454          1183x 1183x 1183x 1183x 1183x                         966957x 966957x   966957x 34419x   34419x 12578x 12578x       966957x                         496673x   496673x       496673x 1019735x 470284x       26389x                                                             4837x 4837x 4837x 4837x   4837x 9413x 9413x 9413x   9413x 9157x 9157x 256x       256x       4837x 7516x   7516x 7516x   7516x 2549x     2549x 2549x 918x       4967x 4967x   4967x   3663x           256x                           9934x 1796x 8138x 3171x     4967x                         256x   256x       256x 256x     256x                                                                                                                                     242x                           14x                                                                                                 1183x                                                                                                                   1703492x 1703492x                       966957x 966957x 966957x   966957x 496673x     966957x 26389x     966957x 4837x     966957x   966957x                     1022061x                   1162x       1162x 1162x     1162x 1162x     1162x   1162x 1703492x 1703492x       1162x         1183x           1183x  
/*!
 * William DURAND <william.durand1@gmail.com>
 * MIT Licensed
 */
 
var Translator = (function () {
    var _messages = {},
        _sPluralRegex = /^\w+\: +(.+)$/,
        _cPluralRegex = /^\s*((\{\s*(\-?\d+[\s*,\s*\-?\d+]*)\s*\})|([\[\]])\s*(-Inf|\-?\d+)\s*,\s*(\+?Inf|\-?\d+)\s*([\[\]]))\s?(.+?)$/,
        _iPluralRegex = /^\s*(\{\s*(\-?\d+[\s*,\s*\-?\d+]*)\s*\})|([\[\]])\s*(-Inf|\-?\d+)\s*,\s*(\+?Inf|\-?\d+)\s*([\[\]])/;
 
    /**
     * replace placeholders in given message.
     * **WARNING:** used placeholders are removed.
     *
     * @param {String} message      The translated message.
     * @param {Object} placeholders The placeholders to replace.
     * @return {String}             A human readable message.
     * @api private
     */
    function replace_placeholders(message, placeholders) {
        var _i,
            _prefix = Translator.placeHolderPrefix,
            _suffix = Translator.placeHolderSuffix;
 
        for (_i in placeholders) {
            var _r = new RegExp(_prefix + _i + _suffix, 'g');
 
            if (_r.test(message)) {
                message = message.replace(_r, placeholders[_i]);
                delete(placeholders[_i]);
            }
        }
 
        return message;
    }
 
    /**
     * Guess the domain if you don't specify it, based on
     * given default domains.
     *
     * @param {String} key  A message key.
     * @return {String}     The message if found, undefined otherwise.
     * @api private
     */
    function guess_domain(key) {
        var _k,
            _defaultDomains = Translator.defaultDomains;
 
        Iif (Translator.defaultDomains.constructor != Array) {
            _defaultDomains = [Translator.defaultDomains];
        }
 
        for (_k in _defaultDomains) {
            if (Translator.has(_defaultDomains[_k] + ':' + key)) {
                return Translator.get(_defaultDomains[_k] + ':' + key);
            }
        }
 
        return undefined;
    }
 
    /**
     * The logic comes from the Symfony2 PHP Framework.
     *
     * Given a message with different plural translations separated by a
     * pipe (|), this method returns the correct portion of the message based
     * on the given number, the current locale and the pluralization rules
     * in the message itself.
     *
     * The message supports two different types of pluralization rules:
     *
     * interval: {0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples
     * indexed:  There is one apple|There is %count% apples
     *
     * The indexed solution can also contain labels (e.g. one: There is one apple).
     * This is purely for making the translations more clear - it does not
     * affect the functionality.
     *
     * The two methods can also be mixed:
     *     {0} There is no apples|one: There is one apple|more: There is %count% apples
     *
     * @param {String} message  The message.
     * @param {Number} number   The number.
     * @return {String}         The message part to use for translation.
     * @api private
     */
    function pluralize(message, number) {
        var _p,
            _e,
            _explicitRules = [],
            _standardRules = [],
            _parts = message.split(Translator.pluralSeparator),
            _matches = [];
 
        for (_p in _parts) {
            var _part = _parts[_p];
            var _rc = new RegExp(_cPluralRegex);
            var _rs = new RegExp(_sPluralRegex);
 
            if (_rc.test(_part)) {
                _matches = _part.match(_rc);
                _explicitRules[_matches[0]] = _matches[_matches.length - 1];
            } else Iif (_rs.test(_part)) {
                _matches = _part.match(_rs);
                _standardRules.push(_matches[1]);
            } else {
                _standardRules.push(_part);
            }
        }
 
        for (_e in _explicitRules) {
            var _r = new RegExp(_iPluralRegex);
 
            Eif (_r.test(_e)) {
                _matches = _e.match(_r);
 
                if (_matches[1]) {
                    var _ns = _matches[2].split(','),
                        _n;
 
                    for (_n in _ns) {
                        if (number == _ns[_n]) {
                            return _explicitRules[_e];
                        }
                    }
                } else {
                    var _leftNumber  = convert_number(_matches[4]);
                    var _rightNumber = convert_number(_matches[5]);
 
                    if (('[' === _matches[3] ? number >= _leftNumber : number > _leftNumber) &&
                        (']' === _matches[6] ? number <= _rightNumber : number < _rightNumber)) {
                        return _explicitRules[_e];
                    }
                }
            }
        }
 
        return _standardRules[plural_position(number)] || undefined;
    }
 
    /**
     * The logic comes from the Symfony2 PHP Framework.
     *
     * Convert number as String, "Inf" and "-Inf"
     * values to number values.
     *
     * @param {String} number   A litteral number.
     * @return {Number}         The int value of the number.
     * @api private
     */
    function convert_number(number) {
        if ('-Inf' === number) {
            return Math.log(0);
        } else if ('+Inf' === number || 'Inf' === number) {
            return -Math.log(0);
        }
 
        return parseInt(number, 10);
    }
 
    /**
     * The logic comes from the Symfony2 PHP Framework.
     *
     * Returns the plural position to use for the given locale and number.
     *
     * @param {Number} number  A number.
     * @return {Number}        The plural position.
     * @api private
     */
    function plural_position(number) {
        var _locale = Translator.locale || Translator.fallback;
 
        Iif ('pt_BR' === _locale) {
            _locale = 'xbr';
        }
 
        Eif (_locale.length > 3) {
            _locale = _locale.split('_')[0];
        }
 
        switch (_locale) {
            case 'bo':
            case 'dz':
            case 'id':
            case 'ja':
            case 'jv':
            case 'ka':
            case 'km':
            case 'kn':
            case 'ko':
            case 'ms':
            case 'th':
            case 'tr':
            case 'vi':
            case 'zh':
                return 0;
            case 'af':
            case 'az':
            case 'bn':
            case 'bg':
            case 'ca':
            case 'da':
            case 'de':
            case 'el':
            case 'en':
            case 'eo':
            case 'es':
            case 'et':
            case 'eu':
            case 'fa':
            case 'fi':
            case 'fo':
            case 'fur':
            case 'fy':
            case 'gl':
            case 'gu':
            case 'ha':
            case 'he':
            case 'hu':
            case 'is':
            case 'it':
            case 'ku':
            case 'lb':
            case 'ml':
            case 'mn':
            case 'mr':
            case 'nah':
            case 'nb':
            case 'ne':
            case 'nl':
            case 'nn':
            case 'no':
            case 'om':
            case 'or':
            case 'pa':
            case 'pap':
            case 'ps':
            case 'pt':
            case 'so':
            case 'sq':
            case 'sv':
            case 'sw':
            case 'ta':
            case 'te':
            case 'tk':
            case 'ur':
            case 'zu':
                return (number == 1) ? 0 : 1;
 
            case 'am':
            case 'bh':
            case 'fil':
            case 'fr':
            case 'gun':
            case 'hi':
            case 'ln':
            case 'mg':
            case 'nso':
            case 'xbr':
            case 'ti':
            case 'wa':
                return ((number === 0) || (number == 1)) ? 0 : 1;
 
            case 'be':
            case 'bs':
            case 'hr':
            case 'ru':
            case 'sr':
            case 'uk':
                return ((number % 10 == 1) && (number % 100 != 11)) ? 0 : (((number % 10 >= 2) && (number % 10 <= 4) && ((number % 100 < 10) || (number % 100 >= 20))) ? 1 : 2);
 
            case 'cs':
            case 'sk':
                return (number == 1) ? 0 : (((number >= 2) && (number <= 4)) ? 1 : 2);
 
            case 'ga':
                return (number == 1) ? 0 : ((number == 2) ? 1 : 2);
 
            case 'lt':
                return ((number % 10 == 1) && (number % 100 != 11)) ? 0 : (((number % 10 >= 2) && ((number % 100 < 10) || (number % 100 >= 20))) ? 1 : 2);
 
            case 'sl':
                return (number % 100 == 1) ? 0 : ((number % 100 == 2) ? 1 : (((number % 100 == 3) || (number % 100 == 4)) ? 2 : 3));
 
            case 'mk':
                return (number % 10 == 1) ? 0 : 1;
 
            case 'mt':
                return (number == 1) ? 0 : (((number === 0) || ((number % 100 > 1) && (number % 100 < 11))) ? 1 : (((number % 100 > 10) && (number % 100 < 20)) ? 2 : 3));
 
            case 'lv':
                return (number === 0) ? 0 : (((number % 10 == 1) && (number % 100 != 11)) ? 1 : 2);
 
            case 'pl':
                return (number == 1) ? 0 : (((number % 10 >= 2) && (number % 10 <= 4) && ((number % 100 < 12) || (number % 100 > 14))) ? 1 : 2);
 
            case 'cy':
                return (number == 1) ? 0 : ((number == 2) ? 1 : (((number == 8) || (number == 11)) ? 2 : 3));
 
            case 'ro':
                return (number == 1) ? 0 : (((number === 0) || ((number % 100 > 0) && (number % 100 < 20))) ? 1 : 2);
 
            case 'ar':
                return (number === 0) ? 0 : ((number == 1) ? 1 : ((number == 2) ? 2 : (((number >= 3) && (number <= 10)) ? 3 : (((number >= 11) && (number <= 99)) ? 4 : 5))));
 
            default:
                return 0;
        }
    }
 
    return {
        /**
         * The current locale.
         *
         * @type {String}
         * @api public
         */
        locale: '',
 
        /**
         * Fallback locale.
         *
         * @type {String}
         * @api public
         */
        fallback: 'en',
 
        /**
         * Placeholder prefix.
         *
         * @type {String}
         * @api public
         */
        placeHolderPrefix: '%',
 
        /**
         * Placeholder suffix.
         *
         * @type {String}
         * @api public
         */
        placeHolderSuffix: '%',
 
        /**
         * Default domains.
         *
         * @type {String|Array}
         * @api public
         */
        defaultDomains: [],
 
        /**
         * Plurar separator.
         *
         * @type {String}
         * @api public
         */
        pluralSeparator: '|',
 
        /**
         * Add a translation entry.
         *
         * @param {String} key      A translation key.
         * @param {String} message  A message for this key.
         * @return {Object}         Translator.
         * @api public
         */
        add: function(key, message) {
            _messages[key] = message;
            return this;
        },
 
      /**
       * Get the translated message for the given key.
       *
       * @param {String} key            A translation key.
       * @param {Object} placeholders   Placeholders.
       * @param {Number} number         A number of objects being described.
       * @return {String} The corresponding message if the key exists otherwise the key will be returned.
       */
        get: function(key, placeholders, number) {
            var _message = _messages[key],
                _number = parseInt(number, 10),
                _placeholders = placeholders || {};
 
            if (_message === undefined) {
                _message = guess_domain(key);
            }
 
            if (_message === undefined) {
                _message = key;
            }
 
            if (_message && !isNaN(_number)) {
                _message = pluralize(_message, _number);
            }
 
            _message = replace_placeholders(_message, _placeholders);
 
            return _message;
        },
 
        /**
         * Determines wether a message is registered or not.
         *
         * @param {String} key  A translation id.
         * @return {Boolean}    Wether the message is registered or not.
         * @api public
         */
        has: function(key) {
            return (_messages[key] ? true : false);
        },
 
        /**
         * Accepts a JSON string to feed translations
         *
         * @param {String} data     A JSON string or object literal
         * @return {Object}         Translator
         */
        fromJSON: function (data) {
            Iif(typeof data === "string") {
                data = JSON.parse(data);
            }
 
            Eif(data.locale) {
                this.locale = data.locale;
            }
 
            Eif(data.defaultDomains) {
                this.defaultDomains = data.defaultDomains;
            }
 
            Eif(data.messages) {
                var key;
                for(key in data.messages) {
                    var message = data.messages[key];
                    this.add(key, message);
                }
            }
 
            return this;
        }
    };
})();
 
Iif (typeof window.define === "function" && window.define.amd) {
    window.define("Translator", [], function () {
        return Translator;
    });
}
 
module.exports = Translator