jquery.i18n.properties.js 25.7 KB
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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/******************************************************************************
 * jquery.i18n.properties
 *
 * Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
 * MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
 *
 * @version     1.2.2
 * @url         https://github.com/jquery-i18n-properties/jquery-i18n-properties
 * @inspiration Localisation assistance for jQuery (http://keith-wood.name/localisation.html)
 *              by Keith Wood (kbwood{at}iinet.com.au) June 2007
 *
 *****************************************************************************/

(function($) {
    $.i18n = {};

    /** Map holding bundle keys (if mode: 'map') */
    $.i18n.map = {};

    /**
     * Load and parse message bundle files (.properties),
     * making bundles keys available as javascript variables.
     *
     * i18n files are named <name>.js, or <name>_<language>.js or <name>_<language>_<country>.js
     * Where:
     *      The <language> argument is a valid ISO Language Code. These codes are the lower-case,
     *      two-letter codes as defined by ISO-639. You can find a full list of these codes at a
     *      number of sites, such as: http://www.loc.gov/standards/iso639-2/englangn.html
     *      The <country> argument is a valid ISO Country Code. These codes are the upper-case,
     *      two-letter codes as defined by ISO-3166. You can find a full list of these codes at a
     *      number of sites, such as: http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
     *
     * Sample usage for a bundles/Messages.properties bundle:
     * $.i18n.properties({
     *      name:      'Messages',
     *      language:  'en_US',
     *      path:      'bundles'
     * });
     * @param  name      (string/string[], optional) names of file to load (eg, 'Messages' or ['Msg1','Msg2']). Defaults to "Messages"
     * @param  language    (string, optional) language/country code (eg, 'en', 'en_US', 'pt_BR'). if not specified, language reported by the browser will be used instead.
     * @param  path      (string, optional) path of directory that contains file to load
     * @param  mode      (string, optional) whether bundles keys are available as JavaScript variables/functions or as a map (eg, 'vars' or 'map')
     * @param  cache        (boolean, optional) whether bundles should be cached by the browser, or forcibly reloaded on each page load. Defaults to false (i.e. forcibly reloaded)
     * @param  encoding  (string, optional) the encoding to request for bundles. Property file resource bundles are specified to be in ISO-8859-1 format. Defaults to UTF-8 for backward compatibility.
     * @param  callback     (function, optional) callback function to be called after script is terminated
     */
    $.i18n.properties = function(settings) {
        // set up settings
        var defaults = {
            name: "Messages",
            language: "",
            path: "",
            mode: "vars",
            cache: false,
            encoding: "UTF-8",
            async: false,
            checkAvailableLanguages: false,
            callback: null
        };
        settings = $.extend(defaults, settings);

        // Try to ensure that we have at a least a two letter language code
        settings.language = this.normaliseLanguageCode(settings.language);

        var languagesFileLoadedCallback = function(languages) {
            settings.totalFiles = 0;
            settings.filesLoaded = 0;

            // load and parse bundle files
            var files = getFiles(settings.name);

            if (settings.async) {
                for (var i = 0, j = files.length; i < j; i++) {
                    // 1 for the base.
                    settings.totalFiles += 1;
                    // 2. with language code (eg, Messages_pt.properties)
                    var shortCode = settings.language.substring(0, 2);
                    if (
                        languages.length == 0 ||
                        $.inArray(shortCode, languages) != -1
                    ) {
                        // 1 for the short code file
                        settings.totalFiles += 1;
                    }
                    // 3. with language code and country code (eg, Messages_pt_BR.properties)
                    if (settings.language.length >= 5) {
                        var longCode = settings.language.substring(0, 5);
                        if (
                            languages.length == 0 ||
                            $.inArray(longCode, languages) != -1
                        ) {
                            // 1 for the long code file
                            settings.totalFiles += 1;
                        }
                    }
                }
            }

            for (var k = 0, m = files.length; k < m; k++) {
                // 1. load base (eg, Messages.properties)
                loadAndParseFile(
                    settings.path + files[k] + ".properties",
                    settings
                );
                // 2. with language code (eg, Messages_pt.properties)
                var shortCode = settings.language.substring(0, 2);
                if (
                    languages.length == 0 ||
                    $.inArray(shortCode, languages) != -1
                ) {
                    loadAndParseFile(
                        settings.path +
                            files[k] +
                            "_" +
                            shortCode +
                            ".properties",
                        settings
                    );
                }
                // 3. with language code and country code (eg, Messages_pt_BR.properties)
                if (settings.language.length >= 5) {
                    var longCode = settings.language.substring(0, 5);
                    if (
                        languages.length == 0 ||
                        $.inArray(longCode, languages) != -1
                    ) {
                        loadAndParseFile(
                            settings.path +
                                files[k] +
                                "_" +
                                longCode +
                                ".properties",
                            settings
                        );
                    }
                }
            }

            // call callback
            if (settings.callback && !settings.async) {
                settings.callback();
            }
        };

        if (settings.checkAvailableLanguages) {
            $.ajax({
                url: settings.path + "languages.json",
                async: settings.async,
                cache: false,
                success: function(data, textStatus, jqXHR) {
                    languagesFileLoadedCallback(data.languages || []);
                }
            });
        } else {
            languagesFileLoadedCallback([]);
        }
    };

    /**
     * When configured with mode: 'map', allows access to bundle values by specifying its key.
     * Eg, jQuery.i18n.prop('com.company.bundles.menu_add')
     */
    $.i18n.prop = function(
        key /* Add parameters as function arguments as necessary  */
    ) {
        var value = $.i18n.map[key];
        if (value == null) return "[" + key + "]";

        var phvList;
        if (arguments.length == 2 && $.isArray(arguments[1]))
            // An array was passed as the only parameter, so assume it is the list of place holder values.
            phvList = arguments[1];

        // Place holder replacement
        /**
         * Tested with:
         *   test.t1=asdf ''{0}''
         *   test.t2=asdf '{0}' '{1}'{1}'zxcv
         *   test.t3=This is \"a quote" 'a''{0}''s'd{fgh{ij'
         *   test.t4="'''{'0}''" {0}{a}
         *   test.t5="'''{0}'''" {1}
         *   test.t6=a {1} b {0} c
         *   test.t7=a 'quoted \\ s\ttringy' \t\t x
         *
         * Produces:
         *   test.t1, p1 ==> asdf 'p1'
         *   test.t2, p1 ==> asdf {0} {1}{1}zxcv
         *   test.t3, p1 ==> This is "a quote" a'{0}'sd{fgh{ij
         *   test.t4, p1 ==> "'{0}'" p1{a}
         *   test.t5, p1 ==> "'{0}'" {1}
         *   test.t6, p1 ==> a {1} b p1 c
         *   test.t6, p1, p2 ==> a p2 b p1 c
         *   test.t6, p1, p2, p3 ==> a p2 b p1 c
         *   test.t7 ==> a quoted \ s	tringy 		 x
         */

        var i;
        if (typeof value == "string") {
            // Handle escape characters. Done separately from the tokenizing loop below because escape characters are
            // active in quoted strings.
            i = 0;
            while ((i = value.indexOf("\\", i)) != -1) {
                if (value.charAt(i + 1) == "t")
                    value =
                        value.substring(0, i) + "\t" + value.substring(i++ + 2);
                else if (value.charAt(i + 1) == "r")
                    // tab
                    value =
                        value.substring(0, i) + "\r" + value.substring(i++ + 2);
                else if (value.charAt(i + 1) == "n")
                    // return
                    value =
                        value.substring(0, i) + "\n" + value.substring(i++ + 2);
                else if (value.charAt(i + 1) == "f")
                    // line feed
                    value =
                        value.substring(0, i) + "\f" + value.substring(i++ + 2);
                else if (value.charAt(i + 1) == "\\")
                    // form feed
                    value =
                        value.substring(0, i) + "\\" + value.substring(i++ + 2); // \
                else value = value.substring(0, i) + value.substring(i + 1); // Quietly drop the character
            }

            // Lazily convert the string to a list of tokens.
            var arr = [],
                j,
                index;
            i = 0;
            while (i < value.length) {
                if (value.charAt(i) == "'") {
                    // Handle quotes
                    if (i == value.length - 1) value = value.substring(0, i);
                    else if (value.charAt(i + 1) == "'")
                        // Silently drop the trailing quote
                        value = value.substring(0, i) + value.substring(++i);
                    else {
                        // Escaped quote
                        // Quoted string
                        j = i + 2;
                        while ((j = value.indexOf("'", j)) != -1) {
                            if (
                                j == value.length - 1 ||
                                value.charAt(j + 1) != "'"
                            ) {
                                // Found start and end quotes. Remove them
                                value =
                                    value.substring(0, i) +
                                    value.substring(i + 1, j) +
                                    value.substring(j + 1);
                                i = j - 1;
                                break;
                            } else {
                                // Found a double quote, reduce to a single quote.
                                value =
                                    value.substring(0, j) +
                                    value.substring(++j);
                            }
                        }

                        if (j == -1) {
                            // There is no end quote. Drop the start quote
                            value =
                                value.substring(0, i) + value.substring(i + 1);
                        }
                    }
                } else if (value.charAt(i) == "{") {
                    // Beginning of an unquoted place holder.
                    j = value.indexOf("}", i + 1);
                    if (j == -1) i++;
                    else {
                        // No end. Process the rest of the line. Java would throw an exception
                        // Add 1 to the index so that it aligns with the function arguments.
                        index = parseInt(value.substring(i + 1, j));
                        if (!isNaN(index) && index >= 0) {
                            // Put the line thus far (if it isn't empty) into the array
                            var s = value.substring(0, i);
                            if (s != "") arr.push(s);
                            // Put the parameter reference into the array
                            arr.push(index);
                            // Start the processing over again starting from the rest of the line.
                            i = 0;
                            value = value.substring(j + 1);
                        } else i = j + 1; // Invalid parameter. Leave as is.
                    }
                } else i++;
            }

            // Put the remainder of the no-empty line into the array.
            if (value != "") arr.push(value);
            value = arr;

            // Make the array the value for the entry.
            $.i18n.map[key] = arr;
        }

        if (value.length == 0) return "";
        if (value.length == 1 && typeof value[0] == "string") return value[0];

        var str = "";
        for (i = 0; i < value.length; i++) {
            if (typeof value[i] == "string") str += value[i];
            else if (phvList && value[i] < phvList.length)
                // Must be a number
                str += phvList[value[i]];
            else if (!phvList && value[i] + 1 < arguments.length)
                str += arguments[value[i] + 1];
            else str += "{" + value[i] + "}";
        }

        return str;
    };

    function callbackIfComplete(settings) {
        if (settings.async) {
            settings.filesLoaded += 1;
            if (settings.filesLoaded === settings.totalFiles) {
                if (settings.callback) {
                    settings.callback();
                }
            }
        }
    }

    /** Load and parse .properties files */
    function loadAndParseFile(filename, settings) {
        $.ajax({
            url: filename,
            async: settings.async,
            cache: settings.cache,
            dataType: "text",
            success: function(data, status) {
                parseData(data, settings.mode);
                callbackIfComplete(settings);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("Failed to download or parse " + filename);
                callbackIfComplete(settings);
            }
        });
    }

    /** Parse .properties files */
    function parseData(data, mode) {
        var parsed = "";
        var parameters = data.split(/\n/);
        var regPlaceHolder = /(\{\d+})/g;
        var regRepPlaceHolder = /\{(\d+)}/g;
        var unicodeRE = /(\\u.{4})/gi;
        for (var i = 0; i < parameters.length; i++) {
            parameters[i] = parameters[i]
                .replace(/^\s\s*/, "")
                .replace(/\s\s*$/, ""); // trim
            if (parameters[i].length > 0 && parameters[i].match("^#") != "#") {
                // skip comments
                var pair = parameters[i].split("=");
                if (pair.length > 0) {
                    /** Process key & value */
                    var name = decodeURI(pair[0])
                        .replace(/^\s\s*/, "")
                        .replace(/\s\s*$/, ""); // trim
                    var value = pair.length == 1 ? "" : pair[1];
                    // process multi-line values
                    while (value.match(/\\$/) == "\\") {
                        value = value.substring(0, value.length - 1);
                        value += parameters[++i].replace(/\s\s*$/, ""); // right trim
                    }
                    // Put values with embedded '='s back together
                    for (var s = 2; s < pair.length; s++) {
                        value += "=" + pair[s];
                    }
                    value = value.replace(/^\s\s*/, "").replace(/\s\s*$/, ""); // trim

                    /** Mode: bundle keys in a map */
                    if (mode == "map" || mode == "both") {
                        // handle unicode chars possibly left out
                        var unicodeMatches = value.match(unicodeRE);
                        if (unicodeMatches) {
                            for (var u = 0; u < unicodeMatches.length; u++) {
                                value = value.replace(
                                    unicodeMatches[u],
                                    unescapeUnicode(unicodeMatches[u])
                                );
                            }
                        }
                        // add to map
                        $.i18n.map[name] = value;
                    }

                    /** Mode: bundle keys as vars/functions */
                    if (mode == "vars" || mode == "both") {
                        value = value.replace(/"/g, '\\"'); // escape quotation mark (")

                        // make sure namespaced key exists (eg, 'some.key')
                        checkKeyNamespace(name);

                        // value with variable substitutions
                        if (regPlaceHolder.test(value)) {
                            var parts = value.split(regPlaceHolder);
                            // process function args
                            var first = true;
                            var fnArgs = "";
                            var usedArgs = [];
                            for (var p = 0; p < parts.length; p++) {
                                if (
                                    regPlaceHolder.test(parts[p]) &&
                                    (usedArgs.length == 0 ||
                                        usedArgs.indexOf(parts[p]) == -1)
                                ) {
                                    if (!first) {
                                        fnArgs += ",";
                                    }
                                    fnArgs += parts[p].replace(
                                        regRepPlaceHolder,
                                        "v$1"
                                    );
                                    usedArgs.push(parts[p]);
                                    first = false;
                                }
                            }
                            parsed += name + "=function(" + fnArgs + "){";
                            // process function body
                            var fnExpr =
                                '"' +
                                value.replace(regRepPlaceHolder, '"+v$1+"') +
                                '"';
                            parsed += "return " + fnExpr + ";" + "};";

                            // simple value
                        } else {
                            parsed += name + '="' + value + '";';
                        }
                    } // END: Mode: bundle keys as vars/functions
                } // END: if(pair.length > 0)
            } // END: skip comments
        }
        eval(parsed);
    }

    /** Make sure namespace exists (for keys with dots in name) */
    // TODO key parts that start with numbers quietly fail. i.e. month.short.1=Jan
    function checkKeyNamespace(key) {
        var regDot = /\./;
        if (regDot.test(key)) {
            var fullname = "";
            var names = key.split(/\./);
            for (var i = 0; i < names.length; i++) {
                if (i > 0) {
                    fullname += ".";
                }
                fullname += names[i];
                if (eval("typeof " + fullname + ' == "undefined"')) {
                    eval(fullname + "={};");
                }
            }
        }
    }

    /** Make sure filename is an array */
    function getFiles(names) {
        return names && names.constructor == Array ? names : [names];
    }

    /** Ensure language code is in the format aa_AA. */
    $.i18n.normaliseLanguageCode = function(lang) {
        if (!lang || lang.length < 2) {
            // 先从地址栏获取语言设置
            lang = getUrlParameter("lang");
            if (!lang || lang.length < 2) {
                // 从浏览器获取语言参数
                lang = navigator.languages
                    ? navigator.languages[0]
                    : navigator.language ||
                      navigator.userLanguage /* IE */ ||
                      "en";
            }
        }

        lang = lang.toLowerCase();
        lang = lang.replace(/-/, "_"); // some browsers report language as en-US instead of en_US
        if (lang.length > 3) {
            lang = lang.substring(0, 3) + lang.substring(3).toUpperCase();
        }
        return lang;
    };

    function getUrlParameter(sParam) {
        // 如果有父窗口,则使用父窗口的地址
        var topWindow = window;
        if (window.parent) {
            topWindow = window.parent;
        }
        var sPageURL = decodeURIComponent(
                topWindow.location.search.substring(1)
            ),
            sURLVariables = sPageURL.split("&"),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split("=");

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined
                    ? true
                    : sParameterName[1];
            }
        }
    }
    /** Unescape unicode chars ('\u00e3') */
    function unescapeUnicode(str) {
        // unescape unicode codes
        var codes = [];
        var code = parseInt(str.substr(2), 16);
        if (code >= 0 && code < Math.pow(2, 16)) {
            codes.push(code);
        }
        // convert codes to text
        var unescaped = "";
        for (var i = 0; i < codes.length; ++i) {
            unescaped += String.fromCharCode(codes[i]);
        }
        return unescaped;
    }

    /* Cross-Browser Split 1.0.1
   (c) Steven Levithan <stevenlevithan.com>; MIT License
   An ECMA-compliant, uniform cross-browser split method */
    var cbSplit;
    // avoid running twice, which would break `cbSplit._nativeSplit`'s reference to the native `split`
    if (!cbSplit) {
        cbSplit = function(str, separator, limit) {
            // if `separator` is not a regex, use the native `split`
            if (
                Object.prototype.toString.call(separator) !== "[object RegExp]"
            ) {
                if (typeof cbSplit._nativeSplit == "undefined")
                    return str.split(separator, limit);
                else return cbSplit._nativeSplit.call(str, separator, limit);
            }

            var output = [],
                lastLastIndex = 0,
                flags =
                    (separator.ignoreCase ? "i" : "") +
                    (separator.multiline ? "m" : "") +
                    (separator.sticky ? "y" : ""),
                separator = new RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy
                separator2,
                match,
                lastIndex,
                lastLength;

            str = str + ""; // type conversion
            if (!cbSplit._compliantExecNpcg) {
                separator2 = new RegExp(
                    "^" + separator.source + "$(?!\\s)",
                    flags
                ); // doesn't need /g or /y, but they don't hurt
            }

            /* behavior for `limit`: if it's...
       - `undefined`: no limit.
       - `NaN` or zero: return an empty array.
       - a positive number: use `Math.floor(limit)`.
       - a negative number: no limit.
       - other: type-convert, then use the above rules. */
            if (limit === undefined || +limit < 0) {
                limit = Infinity;
            } else {
                limit = Math.floor(+limit);
                if (!limit) {
                    return [];
                }
            }

            while ((match = separator.exec(str))) {
                lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser

                if (lastIndex > lastLastIndex) {
                    output.push(str.slice(lastLastIndex, match.index));

                    // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
                    if (!cbSplit._compliantExecNpcg && match.length > 1) {
                        match[0].replace(separator2, function() {
                            for (var i = 1; i < arguments.length - 2; i++) {
                                if (arguments[i] === undefined) {
                                    match[i] = undefined;
                                }
                            }
                        });
                    }

                    if (match.length > 1 && match.index < str.length) {
                        Array.prototype.push.apply(output, match.slice(1));
                    }

                    lastLength = match[0].length;
                    lastLastIndex = lastIndex;

                    if (output.length >= limit) {
                        break;
                    }
                }

                if (separator.lastIndex === match.index) {
                    separator.lastIndex++; // avoid an infinite loop
                }
            }

            if (lastLastIndex === str.length) {
                if (lastLength || !separator.test("")) {
                    output.push("");
                }
            } else {
                output.push(str.slice(lastLastIndex));
            }

            return output.length > limit ? output.slice(0, limit) : output;
        };

        cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined; // NPCG: nonparticipating capturing group
        cbSplit._nativeSplit = String.prototype.split;
    } // end `if (!cbSplit)`
    String.prototype.split = function(separator, limit) {
        return cbSplit(this, separator, limit);
    };
})(jQuery);