Path: blob/main/projects/cupcake2048/js/l10n.js
1835 views
/*1* l10n.js2* 2013-04-183*4* By Eli Grey, http://eligrey.com5* Licensed under the X11/MIT License6* See LICENSE.md7*/89/*global XMLHttpRequest, setTimeout, document, navigator, ActiveXObject*/1011/*! @source http://purl.eligrey.com/github/l10n.js/blob/master/l10n.js*/1213(function () {14"use strict";1516var17undef_type = "undefined"18, string_type = "string"19, nav = self.navigator20, String_ctr = String21, has_own_prop = Object.prototype.hasOwnProperty22, load_queues = {}23, localizations = {}24, FALSE = !125, TRUE = !026// the official format is application/vnd.oftn.l10n+json, though l10n.js will also27// accept application/x-l10n+json and application/l10n+json28, l10n_js_media_type = /^\s*application\/(?:vnd\.oftn\.|x-)?l10n\+json\s*(?:$|;)/i29, XHR3031// property minification aids32, $locale = "locale"33, $default_locale = "defaultLocale"34, $to_locale_string = "toLocaleString"35, $to_lowercase = "toLowerCase"3637, array_index_of = Array.prototype.indexOf || function (item) {38var39len = this.length40, i = 041;4243for (; i < len; i++) {44if (i in this && this[i] === item) {45return i;46}47}4849return -1;50}51, request_JSON = function (uri) {52var req = new XHR();5354// sadly, this has to be blocking to allow for a graceful degrading API55req.open("GET", uri, FALSE);56req.send(null);5758if (req.status !== 200) {59// warn about error without stopping execution60setTimeout(function () {61// Error messages are not localized as not to cause an infinite loop62var l10n_err = new Error("Unable to load localization data: " + uri);63l10n_err.name = "Localization Error";64throw l10n_err;65}, 0);6667return {};68} else {69return JSON.parse(req.responseText);70}71}72, load = String_ctr[$to_locale_string] = function (data) {73// don't handle function.toLocaleString(indentationAmount:Number)74if (arguments.length > 0 && typeof data !== "number") {75if (typeof data === string_type) {76load(request_JSON(data));77} else if (data === FALSE) {78// reset all localizations79localizations = {};80} else {81// Extend current localizations instead of completely overwriting them82var locale, localization, message;83for (locale in data) {84if (has_own_prop.call(data, locale)) {85localization = data[locale];86locale = locale[$to_lowercase]();8788if (!(locale in localizations) || localization === FALSE) {89// reset locale if not existing or reset flag is specified90localizations[locale] = {};91}9293if (localization === FALSE) {94continue;95}9697// URL specified98if (typeof localization === string_type) {99if (String_ctr[$locale][$to_lowercase]().indexOf(locale) === 0) {100localization = request_JSON(localization);101} else {102// queue loading locale if not needed103if (!(locale in load_queues)) {104load_queues[locale] = [];105}106load_queues[locale].push(localization);107continue;108}109}110111for (message in localization) {112if (has_own_prop.call(localization, message)) {113localizations[locale][message] = localization[message];114}115}116}117}118}119}120// Return what function.toLocaleString() normally returns121return Function.prototype[$to_locale_string].apply(String_ctr, arguments);122}123, process_load_queue = function (locale) {124var125queue = load_queues[locale]126, i = 0127, len = queue.length128, localization129;130131for (; i < len; i++) {132localization = {};133localization[locale] = request_JSON(queue[i]);134load(localization);135}136137delete load_queues[locale];138}139, use_default140, localize = String_ctr.prototype[$to_locale_string] = function () {141if (typeof this === undef_type) {142return this;143}144145var146using_default = use_default147, current_locale = String_ctr[using_default ? $default_locale : $locale]148, parts = current_locale[$to_lowercase]().split("-")149, i = parts.length150, this_val = this.valueOf()151, locale152;153154use_default = FALSE;155156// Iterate through locales starting at most-specific until a localization is found157do {158locale = parts.slice(0, i).join("-");159// load locale if not loaded160if (locale in load_queues) {161process_load_queue(locale);162}163if (locale in localizations && this_val in localizations[locale]) {164return localizations[locale][this_val];165}166}167while (i --> 1);168169if (!using_default && String_ctr[$default_locale]) {170use_default = TRUE;171return localize.call(this_val);172}173174return this_val;175}176;177178if (typeof XMLHttpRequest === undef_type && typeof ActiveXObject !== undef_type) {179var AXO = ActiveXObject;180181XHR = function () {182try {183return new AXO("Msxml2.XMLHTTP.6.0");184} catch (xhrEx1) {}185try {186return new AXO("Msxml2.XMLHTTP.3.0");187} catch (xhrEx2) {}188try {189return new AXO("Msxml2.XMLHTTP");190} catch (xhrEx3) {}191192throw new Error("XMLHttpRequest not supported by this browser.");193};194} else {195XHR = XMLHttpRequest;196}197198String_ctr[$default_locale] = String_ctr[$default_locale] || "";199String_ctr[$locale] = nav && (nav.language || nav.userLanguage) || "";200201if (typeof document !== undef_type) {202var203elts = document.getElementsByTagName("link")204, i = elts.length205, localization206;207208while (i--) {209var210elt = elts[i]211, rel = (elt.getAttribute("rel") || "")[$to_lowercase]().split(/\s+/)212;213214if (l10n_js_media_type.test(elt.type)) {215if (array_index_of.call(rel, "localizations") !== -1) {216// multiple localizations217load(elt.getAttribute("href"));218} else if (array_index_of.call(rel, "localization") !== -1) {219// single localization220localization = {};221localization[(elt.getAttribute("hreflang") || "")[$to_lowercase]()] =222elt.getAttribute("href");223load(localization);224}225}226}227}228229}());230231function Localize(key) {232var string = '%' + key;233return string.toLocaleString();234};235236function LocalizeElement(className) {237var element = document.getElementsByClassName(className);238if (element[0]) element[0].innerHTML = Localize(className);239}240241