| | |
| | | var baseUrl = "/wcs"; |
| | | |
| | | var WCS_I18N = (function (window, document) { |
| | | "use strict"; |
| | | |
| | | var STORAGE_KEY = "wcs_lang"; |
| | | var COOKIE_KEY = "wcs_lang"; |
| | | var I18N_PATH = "/i18n/messages"; |
| | | var TRANSLATABLE_RE = /[\u3400-\u9fff]/; |
| | | var state = { |
| | | ready: false, |
| | | loading: false, |
| | | locale: null, |
| | | defaultLocale: "zh-CN", |
| | | supportedLocales: ["zh-CN", "en-US"], |
| | | localeOptions: [], |
| | | messages: {}, |
| | | legacy: {}, |
| | | legacyEntries: [], |
| | | legacyRegexEntries: [], |
| | | builtinRegexEntries: [], |
| | | translateCache: {}, |
| | | translateCacheSize: 0, |
| | | callbacks: [], |
| | | observer: null, |
| | | bridgeAttempts: 0, |
| | | applying: false, |
| | | pendingNodes: [], |
| | | flushTimer: 0 |
| | | }; |
| | | |
| | | patchXmlHttpRequest(); |
| | | patchFetch(); |
| | | scheduleBridgeWrap(); |
| | | |
| | | if (document && document.documentElement) { |
| | | document.documentElement.lang = getLocale(); |
| | | } |
| | | |
| | | if (document.readyState === "loading") { |
| | | document.addEventListener("DOMContentLoaded", function () { |
| | | load(); |
| | | }); |
| | | } else { |
| | | load(); |
| | | } |
| | | |
| | | function normalizeLocale(locale) { |
| | | var value = (locale || "").replace(/_/g, "-").trim(); |
| | | if (!value) { |
| | | return state.defaultLocale; |
| | | } |
| | | if (/^en/i.test(value)) { |
| | | return "en-US"; |
| | | } |
| | | if (/^zh/i.test(value)) { |
| | | return "zh-CN"; |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | function getBasePath() { |
| | | return typeof baseUrl === "string" && baseUrl ? baseUrl : ""; |
| | | } |
| | | |
| | | function isExternalUrl(url) { |
| | | return /^(?:[a-z]+:)?\/\//i.test(url || ""); |
| | | } |
| | | |
| | | function appendNonceUrl(url) { |
| | | var value = (url || "").trim(); |
| | | if (!value || isExternalUrl(value) || value.charAt(0) === "<") { |
| | | return url; |
| | | } |
| | | if (/[?&]_wcsI18nNonce=/.test(value)) { |
| | | return value; |
| | | } |
| | | return value + (value.indexOf("?") === -1 ? "?" : "&") + "_wcsI18nNonce=" + new Date().getTime(); |
| | | } |
| | | |
| | | function getCookie(name) { |
| | | var pattern = new RegExp("(?:^|; )" + name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "=([^;]*)"); |
| | | var matches = document.cookie.match(pattern); |
| | | return matches ? decodeURIComponent(matches[1]) : ""; |
| | | } |
| | | |
| | | function setCookie(name, value) { |
| | | var path = getBasePath() || "/"; |
| | | document.cookie = name + "=" + encodeURIComponent(value) + "; path=" + path + "; max-age=31536000"; |
| | | } |
| | | |
| | | function getStoredLocale() { |
| | | var locale = ""; |
| | | try { |
| | | locale = window.localStorage ? window.localStorage.getItem(STORAGE_KEY) : ""; |
| | | } catch (e) { |
| | | locale = ""; |
| | | } |
| | | if (!locale) { |
| | | locale = getCookie(COOKIE_KEY); |
| | | } |
| | | if (!locale && window.navigator) { |
| | | locale = window.navigator.language || (window.navigator.languages && window.navigator.languages[0]) || ""; |
| | | } |
| | | return normalizeLocale(locale || state.defaultLocale); |
| | | } |
| | | |
| | | function setStoredLocale(locale) { |
| | | var normalized = normalizeLocale(locale); |
| | | try { |
| | | if (window.localStorage) { |
| | | window.localStorage.setItem(STORAGE_KEY, normalized); |
| | | } |
| | | } catch (e) { |
| | | } |
| | | setCookie(COOKIE_KEY, normalized); |
| | | } |
| | | |
| | | function getLocale() { |
| | | if (!state.locale) { |
| | | state.locale = getStoredLocale(); |
| | | } |
| | | return state.locale; |
| | | } |
| | | |
| | | function setLocale(locale, reload) { |
| | | var normalized = normalizeLocale(locale); |
| | | state.locale = normalized; |
| | | setStoredLocale(normalized); |
| | | if (document && document.documentElement) { |
| | | document.documentElement.lang = normalized; |
| | | } |
| | | if (reload === false) { |
| | | state.ready = false; |
| | | load(); |
| | | return; |
| | | } |
| | | window.location.reload(); |
| | | } |
| | | |
| | | function onReady(callback) { |
| | | if (typeof callback !== "function") { |
| | | return; |
| | | } |
| | | if (state.ready) { |
| | | callback(api); |
| | | return; |
| | | } |
| | | state.callbacks.push(callback); |
| | | } |
| | | |
| | | function flushCallbacks() { |
| | | var queue = state.callbacks.slice(); |
| | | var i; |
| | | state.callbacks = []; |
| | | for (i = 0; i < queue.length; i++) { |
| | | try { |
| | | queue[i](api); |
| | | } catch (e) { |
| | | } |
| | | } |
| | | } |
| | | |
| | | function parseJson(text) { |
| | | try { |
| | | return JSON.parse(text); |
| | | } catch (e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | function request(url, success, failure) { |
| | | var xhr = new XMLHttpRequest(); |
| | | xhr.open("GET", url, true); |
| | | xhr.setRequestHeader("X-Lang", getLocale()); |
| | | xhr.onreadystatechange = function () { |
| | | if (xhr.readyState !== 4) { |
| | | return; |
| | | } |
| | | if (xhr.status >= 200 && xhr.status < 300) { |
| | | success(parseJson(xhr.responseText) || {}); |
| | | } else if (typeof failure === "function") { |
| | | failure(xhr); |
| | | } |
| | | }; |
| | | xhr.send(null); |
| | | } |
| | | |
| | | function load(callback) { |
| | | if (typeof callback === "function") { |
| | | onReady(callback); |
| | | } |
| | | if (state.loading || state.ready) { |
| | | return; |
| | | } |
| | | state.loading = true; |
| | | request(getBasePath() + I18N_PATH + "?lang=" + encodeURIComponent(getLocale()) + "&_t=" + new Date().getTime(), function (res) { |
| | | var data = res && res.code === 200 ? (res.data || {}) : {}; |
| | | state.loading = false; |
| | | state.ready = true; |
| | | state.locale = normalizeLocale(data.locale || getLocale()); |
| | | state.defaultLocale = normalizeLocale(data.defaultLocale || state.defaultLocale); |
| | | state.supportedLocales = readLocaleTags(data.supportedLocales); |
| | | state.localeOptions = readLocaleOptions(data.supportedLocales); |
| | | state.messages = data.messages || {}; |
| | | state.legacy = data.legacy || {}; |
| | | state.legacyRegexEntries = readLegacyRegexEntries(state.legacy); |
| | | state.legacyEntries = sortedLegacyEntries(state.legacy); |
| | | state.builtinRegexEntries = buildBuiltinRegexEntries(); |
| | | resetTranslateCache(); |
| | | if (document && document.documentElement) { |
| | | document.documentElement.lang = state.locale; |
| | | } |
| | | wrapLibraries(); |
| | | observeDom(); |
| | | apply(document.body || document.documentElement); |
| | | if (document.title) { |
| | | document.title = translate(document.title); |
| | | } |
| | | flushCallbacks(); |
| | | }, function () { |
| | | state.loading = false; |
| | | state.ready = true; |
| | | state.locale = getLocale(); |
| | | state.builtinRegexEntries = buildBuiltinRegexEntries(); |
| | | resetTranslateCache(); |
| | | wrapLibraries(); |
| | | flushCallbacks(); |
| | | }); |
| | | } |
| | | |
| | | function readLocaleTags(options) { |
| | | var tags = []; |
| | | var i; |
| | | if (!options || !options.length) { |
| | | return state.supportedLocales.slice(); |
| | | } |
| | | for (i = 0; i < options.length; i++) { |
| | | if (typeof options[i] === "string") { |
| | | tags.push(normalizeLocale(options[i])); |
| | | } else if (options[i] && options[i].tag) { |
| | | tags.push(normalizeLocale(options[i].tag)); |
| | | } |
| | | } |
| | | return tags.length ? tags : state.supportedLocales.slice(); |
| | | } |
| | | |
| | | function readLocaleOptions(options) { |
| | | var list = []; |
| | | var i; |
| | | if (!options || !options.length) { |
| | | options = state.supportedLocales.slice(); |
| | | } |
| | | for (i = 0; i < options.length; i++) { |
| | | if (typeof options[i] === "string") { |
| | | list.push({ tag: normalizeLocale(options[i]), label: options[i] }); |
| | | } else if (options[i] && options[i].tag) { |
| | | list.push({ |
| | | tag: normalizeLocale(options[i].tag), |
| | | label: options[i].label || options[i].tag |
| | | }); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | function sortedLegacyEntries(map) { |
| | | var entries = []; |
| | | var key; |
| | | for (key in map) { |
| | | if (map.hasOwnProperty(key) && key.indexOf("regex:") !== 0) { |
| | | entries.push([key, map[key]]); |
| | | } |
| | | } |
| | | entries.sort(function (left, right) { |
| | | return right[0].length - left[0].length; |
| | | }); |
| | | return entries; |
| | | } |
| | | |
| | | function readLegacyRegexEntries(map) { |
| | | var entries = []; |
| | | var key; |
| | | var source; |
| | | for (key in map) { |
| | | if (!map.hasOwnProperty(key) || key.indexOf("regex:") !== 0) { |
| | | continue; |
| | | } |
| | | source = key.substring(6); |
| | | if (!source) { |
| | | continue; |
| | | } |
| | | if (isBrokenRegexEntry(source, map[key])) { |
| | | continue; |
| | | } |
| | | try { |
| | | entries.push({ |
| | | source: source, |
| | | regex: new RegExp(source), |
| | | value: map[key] |
| | | }); |
| | | } catch (e) { |
| | | } |
| | | } |
| | | entries.sort(function (left, right) { |
| | | return right.source.length - left.source.length; |
| | | }); |
| | | return entries; |
| | | } |
| | | |
| | | function isBrokenRegexEntry(source, value) { |
| | | if (!source || !value) { |
| | | return false; |
| | | } |
| | | if (value.indexOf("$=") >= 0) { |
| | | return true; |
| | | } |
| | | return /\\s\*|\\d\+|\\\||\\:|\(\?:/.test(value); |
| | | } |
| | | |
| | | function format(template, params) { |
| | | var result = template || ""; |
| | | var index; |
| | | if (!params) { |
| | | return result; |
| | | } |
| | | if (Object.prototype.toString.call(params) === "[object Array]") { |
| | | for (index = 0; index < params.length; index++) { |
| | | result = result.replace(new RegExp("\\{" + index + "\\}", "g"), params[index]); |
| | | } |
| | | return result; |
| | | } |
| | | for (index in params) { |
| | | if (params.hasOwnProperty(index)) { |
| | | result = result.replace(new RegExp("\\{" + index + "\\}", "g"), params[index]); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | function resetTranslateCache() { |
| | | state.translateCache = {}; |
| | | state.translateCacheSize = 0; |
| | | } |
| | | |
| | | function rememberTranslateCache(text, translated) { |
| | | if (state.translateCacheSize > 4000) { |
| | | resetTranslateCache(); |
| | | } |
| | | if (!Object.prototype.hasOwnProperty.call(state.translateCache, text)) { |
| | | state.translateCacheSize += 1; |
| | | } |
| | | state.translateCache[text] = translated; |
| | | return translated; |
| | | } |
| | | |
| | | function canTranslateText(text) { |
| | | return !!(text && TRANSLATABLE_RE.test(text)); |
| | | } |
| | | |
| | | function buildBuiltinRegexEntries() { |
| | | return [ |
| | | { |
| | | regex: /^圈(\d+)\s*\|\s*站点:\s*(\d+)\s*\|\s*(?:任务|Task):\s*(\d+)\s*\|\s*承载:\s*([\d.]+%)$/, |
| | | key: "legacy.regex.loopStatus", |
| | | fallback: "Zone {0} | Stations: {1} | Tasks: {2} | Load: {3}" |
| | | }, |
| | | { |
| | | regex: /^点击(?:删除|Delete)关联:\s*站点\s*(\d+)\s*->\s*设备\s*(\d+)$/, |
| | | key: "legacy.regex.stationDeviceLink", |
| | | fallback: "Click to remove mapping: Station {0} -> Device {1}" |
| | | } |
| | | ]; |
| | | } |
| | | |
| | | function formatBuiltinRegex(entry, args) { |
| | | var value = t(entry.key, args); |
| | | if (value === entry.key && entry.fallback) { |
| | | return format(entry.fallback, args); |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | function t(key, params) { |
| | | var value = state.messages[key]; |
| | | if (!value) { |
| | | return key; |
| | | } |
| | | return format(value, params); |
| | | } |
| | | |
| | | function preserveSpaces(original, translated) { |
| | | var match = original.match(/^(\s*)([\s\S]*?)(\s*)$/); |
| | | if (!match) { |
| | | return translated; |
| | | } |
| | | return match[1] + translated + match[3]; |
| | | } |
| | | |
| | | function exactLegacy(text) { |
| | | var trimmed = (text || "").trim(); |
| | | var translated = state.legacy[trimmed]; |
| | | if (!translated) { |
| | | return text; |
| | | } |
| | | return preserveSpaces(text, translated); |
| | | } |
| | | |
| | | function fragmentLegacy(text) { |
| | | var result = text; |
| | | var i; |
| | | var entry; |
| | | for (i = 0; i < state.legacyEntries.length; i++) { |
| | | entry = state.legacyEntries[i]; |
| | | if (!entry[0] || entry[0].length < 2 || entry[0] === entry[1]) { |
| | | continue; |
| | | } |
| | | result = result.split(entry[0]).join(entry[1]); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | function regexLegacy(text) { |
| | | var trimmed = (text || "").trim(); |
| | | var i; |
| | | var entry; |
| | | var translated; |
| | | for (i = 0; i < state.legacyRegexEntries.length; i++) { |
| | | entry = state.legacyRegexEntries[i]; |
| | | if (!entry || !entry.regex) { |
| | | continue; |
| | | } |
| | | if (trimmed) { |
| | | translated = trimmed.replace(entry.regex, entry.value); |
| | | if (translated !== trimmed) { |
| | | return preserveSpaces(text, translated); |
| | | } |
| | | } |
| | | translated = text.replace(entry.regex, entry.value); |
| | | if (translated !== text) { |
| | | return translated; |
| | | } |
| | | } |
| | | for (i = 0; i < state.builtinRegexEntries.length; i++) { |
| | | entry = state.builtinRegexEntries[i]; |
| | | if (!entry || !entry.regex) { |
| | | continue; |
| | | } |
| | | if (trimmed) { |
| | | translated = trimmed.replace(entry.regex, function () { |
| | | return formatBuiltinRegex(entry, Array.prototype.slice.call(arguments, 1, -2)); |
| | | }); |
| | | if (translated !== trimmed) { |
| | | return preserveSpaces(text, translated); |
| | | } |
| | | } |
| | | translated = text.replace(entry.regex, function () { |
| | | return formatBuiltinRegex(entry, Array.prototype.slice.call(arguments, 1, -2)); |
| | | }); |
| | | if (translated !== text) { |
| | | return translated; |
| | | } |
| | | } |
| | | return text; |
| | | } |
| | | |
| | | function translate(text) { |
| | | if (!text || !state.ready || state.locale === state.defaultLocale) { |
| | | return text; |
| | | } |
| | | if (Object.prototype.hasOwnProperty.call(state.translateCache, text)) { |
| | | return state.translateCache[text]; |
| | | } |
| | | if (!canTranslateText(text)) { |
| | | return rememberTranslateCache(text, text); |
| | | } |
| | | var exact = exactLegacy(text); |
| | | if (exact !== text) { |
| | | return rememberTranslateCache(text, exact); |
| | | } |
| | | var regex = regexLegacy(text); |
| | | if (regex !== text) { |
| | | return rememberTranslateCache(text, regex); |
| | | } |
| | | return rememberTranslateCache(text, fragmentLegacy(text)); |
| | | } |
| | | |
| | | function translateAttribute(element, attrName) { |
| | | var value = element.getAttribute(attrName); |
| | | var translated = translate(value); |
| | | if (translated && translated !== value) { |
| | | element.setAttribute(attrName, translated); |
| | | } |
| | | } |
| | | |
| | | function applyI18nKeys(element) { |
| | | var textKey = element.getAttribute("data-i18n-key"); |
| | | var placeholderKey = element.getAttribute("data-i18n-placeholder-key"); |
| | | var titleKey = element.getAttribute("data-i18n-title-key"); |
| | | var params = element.getAttribute("data-i18n-params"); |
| | | var parsedParams = params ? parseJson(params) : null; |
| | | if (textKey) { |
| | | if (/^(INPUT|TEXTAREA)$/i.test(element.tagName)) { |
| | | if (element.type === "button" || element.type === "submit" || element.type === "reset") { |
| | | var buttonText = t(textKey, parsedParams); |
| | | if (element.value !== buttonText) { |
| | | element.value = buttonText; |
| | | } |
| | | } |
| | | } else { |
| | | var textValue = t(textKey, parsedParams); |
| | | if (element.textContent !== textValue) { |
| | | element.textContent = textValue; |
| | | } |
| | | } |
| | | } |
| | | if (placeholderKey) { |
| | | var placeholderValue = t(placeholderKey, parsedParams); |
| | | if (element.getAttribute("placeholder") !== placeholderValue) { |
| | | element.setAttribute("placeholder", placeholderValue); |
| | | } |
| | | } |
| | | if (titleKey) { |
| | | var titleValue = t(titleKey, parsedParams); |
| | | if (element.getAttribute("title") !== titleValue) { |
| | | element.setAttribute("title", titleValue); |
| | | } |
| | | } |
| | | } |
| | | |
| | | function shouldSkipElement(element) { |
| | | if (!element || element.nodeType !== 1) { |
| | | return true; |
| | | } |
| | | return /^(SCRIPT|STYLE|TEXTAREA|CODE|PRE)$/i.test(element.tagName); |
| | | } |
| | | |
| | | function traverse(node) { |
| | | var child; |
| | | if (!node) { |
| | | return; |
| | | } |
| | | if (node.nodeType === 3) { |
| | | var translatedText = translate(node.nodeValue); |
| | | if (translatedText !== node.nodeValue) { |
| | | node.nodeValue = translatedText; |
| | | } |
| | | return; |
| | | } |
| | | if (node.nodeType !== 1 || shouldSkipElement(node)) { |
| | | return; |
| | | } |
| | | applyI18nKeys(node); |
| | | translateAttribute(node, "placeholder"); |
| | | translateAttribute(node, "title"); |
| | | translateAttribute(node, "aria-label"); |
| | | translateAttribute(node, "alt"); |
| | | if (/^(INPUT)$/i.test(node.tagName)) { |
| | | if (/^(button|submit|reset)$/i.test(node.type || "")) { |
| | | if (node.value) { |
| | | var translatedValue = translate(node.value); |
| | | if (translatedValue !== node.value) { |
| | | node.value = translatedValue; |
| | | } |
| | | } |
| | | } else if ((node.readOnly || node.disabled || node.getAttribute("readonly") !== null) && node.value) { |
| | | var translatedReadonlyValue = translate(node.value); |
| | | if (translatedReadonlyValue !== node.value) { |
| | | node.value = translatedReadonlyValue; |
| | | } |
| | | } |
| | | } |
| | | child = node.firstChild; |
| | | while (child) { |
| | | traverse(child); |
| | | child = child.nextSibling; |
| | | } |
| | | } |
| | | |
| | | function apply(root) { |
| | | if (!root || !state.ready) { |
| | | return; |
| | | } |
| | | state.applying = true; |
| | | try { |
| | | traverse(root); |
| | | } finally { |
| | | state.applying = false; |
| | | } |
| | | } |
| | | |
| | | function scheduleFlush() { |
| | | if (state.flushTimer) { |
| | | return; |
| | | } |
| | | var scheduler = window.requestAnimationFrame || function (callback) { |
| | | return window.setTimeout(callback, 16); |
| | | }; |
| | | state.flushTimer = scheduler(flushPendingNodes); |
| | | } |
| | | |
| | | function enqueueNode(node) { |
| | | if (!node || node.__wcsI18nQueued) { |
| | | return; |
| | | } |
| | | node.__wcsI18nQueued = true; |
| | | state.pendingNodes.push(node); |
| | | scheduleFlush(); |
| | | } |
| | | |
| | | function flushPendingNodes() { |
| | | var queue; |
| | | var i; |
| | | state.flushTimer = 0; |
| | | if (!state.pendingNodes.length) { |
| | | return; |
| | | } |
| | | if (state.applying) { |
| | | scheduleFlush(); |
| | | return; |
| | | } |
| | | queue = state.pendingNodes.slice(); |
| | | state.pendingNodes = []; |
| | | state.applying = true; |
| | | try { |
| | | for (i = 0; i < queue.length; i++) { |
| | | if (!queue[i]) { |
| | | continue; |
| | | } |
| | | queue[i].__wcsI18nQueued = false; |
| | | if (queue[i].nodeType === 3) { |
| | | var translatedText = translate(queue[i].nodeValue); |
| | | if (translatedText !== queue[i].nodeValue) { |
| | | queue[i].nodeValue = translatedText; |
| | | } |
| | | } else if (queue[i].nodeType === 1) { |
| | | traverse(queue[i]); |
| | | } |
| | | } |
| | | } finally { |
| | | state.applying = false; |
| | | } |
| | | if (state.pendingNodes.length) { |
| | | scheduleFlush(); |
| | | } |
| | | } |
| | | |
| | | function observeDom() { |
| | | if (state.observer || !window.MutationObserver || !document || !document.documentElement) { |
| | | return; |
| | | } |
| | | state.observer = new MutationObserver(function (mutations) { |
| | | var i; |
| | | if (state.applying) { |
| | | return; |
| | | } |
| | | for (i = 0; i < mutations.length; i++) { |
| | | if (mutations[i].type === "childList") { |
| | | for (var j = 0; j < mutations[i].addedNodes.length; j++) { |
| | | enqueueNode(mutations[i].addedNodes[j]); |
| | | } |
| | | } else if (mutations[i].type === "characterData" && mutations[i].target) { |
| | | enqueueNode(mutations[i].target); |
| | | } else if (mutations[i].type === "attributes" && mutations[i].target) { |
| | | enqueueNode(mutations[i].target); |
| | | } |
| | | } |
| | | }); |
| | | state.observer.observe(document.documentElement, { |
| | | childList: true, |
| | | subtree: true, |
| | | characterData: true, |
| | | attributes: true, |
| | | attributeFilter: ["placeholder", "title", "aria-label", "alt"] |
| | | }); |
| | | } |
| | | |
| | | function wrapLayer() { |
| | | if (!window.layer || window.layer.__wcsI18nWrapped) { |
| | | return; |
| | | } |
| | | var layer = window.layer; |
| | | var rawMsg = layer.msg; |
| | | var rawAlert = layer.alert; |
| | | var rawConfirm = layer.confirm; |
| | | var rawTips = layer.tips; |
| | | var rawOpen = layer.open; |
| | | |
| | | if (typeof rawMsg === "function") { |
| | | layer.msg = function (content, options, end) { |
| | | return rawMsg.call(layer, translate(content), options, end); |
| | | }; |
| | | } |
| | | if (typeof rawAlert === "function") { |
| | | layer.alert = function (content, options, yes) { |
| | | if (options && typeof options === "object" && typeof options.title === "string") { |
| | | options.title = translate(options.title); |
| | | } |
| | | return rawAlert.call(layer, translate(content), options, yes); |
| | | }; |
| | | } |
| | | if (typeof rawConfirm === "function") { |
| | | layer.confirm = function (content, options, yes, cancel) { |
| | | if (typeof options === "string") { |
| | | options = translate(options); |
| | | } else if (options && typeof options === "object") { |
| | | if (typeof options.title === "string") { |
| | | options.title = translate(options.title); |
| | | } else if (!options.title) { |
| | | options.title = translate("信息"); |
| | | } |
| | | if (Object.prototype.toString.call(options.btn) === "[object Array]") { |
| | | for (var i = 0; i < options.btn.length; i++) { |
| | | options.btn[i] = translate(options.btn[i]); |
| | | } |
| | | } |
| | | } |
| | | return rawConfirm.call(layer, translate(content), options, yes, cancel); |
| | | }; |
| | | } |
| | | if (typeof rawTips === "function") { |
| | | layer.tips = function (content, follow, options) { |
| | | return rawTips.call(layer, translate(content), follow, options); |
| | | }; |
| | | } |
| | | if (typeof rawOpen === "function") { |
| | | layer.open = function (options) { |
| | | if (options && typeof options === "object") { |
| | | if (typeof options.title === "string") { |
| | | options.title = translate(options.title); |
| | | } else if (Object.prototype.toString.call(options.title) === "[object Array]" && typeof options.title[0] === "string") { |
| | | options.title[0] = translate(options.title[0]); |
| | | } |
| | | if (typeof options.content === "string") { |
| | | if (options.type === 2) { |
| | | options.content = appendNonceUrl(options.content); |
| | | } else { |
| | | options.content = translate(options.content); |
| | | } |
| | | } |
| | | if (Object.prototype.toString.call(options.btn) === "[object Array]") { |
| | | for (var i = 0; i < options.btn.length; i++) { |
| | | options.btn[i] = translate(options.btn[i]); |
| | | } |
| | | } |
| | | } |
| | | return rawOpen.call(layer, options); |
| | | }; |
| | | } |
| | | window.layer.__wcsI18nWrapped = true; |
| | | } |
| | | |
| | | function wrapElement() { |
| | | if (!window.ELEMENT || window.ELEMENT.__wcsI18nWrapped) { |
| | | return; |
| | | } |
| | | var element = window.ELEMENT; |
| | | var methods = ["success", "warning", "info", "error"]; |
| | | var i; |
| | | if (typeof element.i18n === "function") { |
| | | element.i18n(function (key, value) { |
| | | return t(key, value); |
| | | }); |
| | | } |
| | | if (typeof element.Message === "function") { |
| | | var rawMessage = element.Message; |
| | | element.Message = function (options) { |
| | | if (typeof options === "string") { |
| | | return rawMessage.call(element, translate(options)); |
| | | } |
| | | if (options && typeof options === "object" && typeof options.message === "string") { |
| | | options.message = translate(options.message); |
| | | } |
| | | return rawMessage.call(element, options); |
| | | }; |
| | | for (i = 0; i < methods.length; i++) { |
| | | if (typeof rawMessage[methods[i]] === "function") { |
| | | (function (methodName) { |
| | | element.Message[methodName] = function (options) { |
| | | if (typeof options === "string") { |
| | | return rawMessage[methodName].call(rawMessage, translate(options)); |
| | | } |
| | | if (options && typeof options === "object" && typeof options.message === "string") { |
| | | options.message = translate(options.message); |
| | | } |
| | | return rawMessage[methodName].call(rawMessage, options); |
| | | }; |
| | | })(methods[i]); |
| | | } |
| | | } |
| | | } |
| | | if (element.MessageBox) { |
| | | wrapMessageBoxMethod(element.MessageBox, "alert"); |
| | | wrapMessageBoxMethod(element.MessageBox, "confirm"); |
| | | wrapMessageBoxMethod(element.MessageBox, "prompt"); |
| | | } |
| | | window.ELEMENT.__wcsI18nWrapped = true; |
| | | } |
| | | |
| | | function wrapMessageBoxMethod(box, methodName) { |
| | | if (typeof box[methodName] !== "function") { |
| | | return; |
| | | } |
| | | var raw = box[methodName]; |
| | | box[methodName] = function (message, title, options) { |
| | | if (typeof message === "string") { |
| | | message = translate(message); |
| | | } |
| | | if (typeof title === "string") { |
| | | title = translate(title); |
| | | } else if (title && typeof title === "object" && typeof title.message === "string") { |
| | | title.message = translate(title.message); |
| | | } |
| | | if (options && typeof options === "object") { |
| | | if (typeof options.confirmButtonText === "string") { |
| | | options.confirmButtonText = translate(options.confirmButtonText); |
| | | } |
| | | if (typeof options.cancelButtonText === "string") { |
| | | options.cancelButtonText = translate(options.cancelButtonText); |
| | | } |
| | | if (typeof options.title === "string") { |
| | | options.title = translate(options.title); |
| | | } |
| | | } |
| | | return raw.call(box, message, title, options); |
| | | }; |
| | | } |
| | | |
| | | function wrapLibraries() { |
| | | wrapLayer(); |
| | | wrapElement(); |
| | | } |
| | | |
| | | function scheduleBridgeWrap() { |
| | | var timer = window.setInterval(function () { |
| | | state.bridgeAttempts += 1; |
| | | wrapLibraries(); |
| | | if ((window.layer || !window.ELEMENT) && state.bridgeAttempts > 20) { |
| | | window.clearInterval(timer); |
| | | } |
| | | }, 300); |
| | | } |
| | | |
| | | function patchXmlHttpRequest() { |
| | | if (!window.XMLHttpRequest || window.XMLHttpRequest.__wcsI18nPatched) { |
| | | return; |
| | | } |
| | | var rawOpen = window.XMLHttpRequest.prototype.open; |
| | | var rawSend = window.XMLHttpRequest.prototype.send; |
| | | var rawSetHeader = window.XMLHttpRequest.prototype.setRequestHeader; |
| | | |
| | | window.XMLHttpRequest.prototype.open = function (method, url) { |
| | | this.__wcsI18nUrl = url; |
| | | this.__wcsI18nHeaderSet = false; |
| | | return rawOpen.apply(this, arguments); |
| | | }; |
| | | window.XMLHttpRequest.prototype.setRequestHeader = function (name, value) { |
| | | if (String(name).toLowerCase() === "x-lang") { |
| | | this.__wcsI18nHeaderSet = true; |
| | | } |
| | | return rawSetHeader.apply(this, arguments); |
| | | }; |
| | | window.XMLHttpRequest.prototype.send = function () { |
| | | try { |
| | | if (!this.__wcsI18nHeaderSet && shouldAttachLangHeader(this.__wcsI18nUrl)) { |
| | | rawSetHeader.call(this, "X-Lang", getLocale()); |
| | | this.__wcsI18nHeaderSet = true; |
| | | } |
| | | } catch (e) { |
| | | } |
| | | return rawSend.apply(this, arguments); |
| | | }; |
| | | window.XMLHttpRequest.__wcsI18nPatched = true; |
| | | } |
| | | |
| | | function patchFetch() { |
| | | if (!window.fetch || window.fetch.__wcsI18nPatched) { |
| | | return; |
| | | } |
| | | var rawFetch = window.fetch; |
| | | window.fetch = function (input, init) { |
| | | var requestUrl = typeof input === "string" ? input : (input && input.url); |
| | | init = init || {}; |
| | | if (shouldAttachLangHeader(requestUrl)) { |
| | | init.headers = init.headers || {}; |
| | | if (typeof Headers !== "undefined" && init.headers instanceof Headers) { |
| | | if (!init.headers.has("X-Lang")) { |
| | | init.headers.set("X-Lang", getLocale()); |
| | | } |
| | | } else if (!init.headers["X-Lang"] && !init.headers["x-lang"]) { |
| | | init.headers["X-Lang"] = getLocale(); |
| | | } |
| | | } |
| | | return rawFetch.call(window, input, init); |
| | | }; |
| | | window.fetch.__wcsI18nPatched = true; |
| | | } |
| | | |
| | | function shouldAttachLangHeader(url) { |
| | | if (!url) { |
| | | return true; |
| | | } |
| | | if (typeof url !== "string") { |
| | | return true; |
| | | } |
| | | return url.indexOf("http://") !== 0 && url.indexOf("https://") !== 0 || url.indexOf(window.location.origin) === 0; |
| | | } |
| | | |
| | | var api = { |
| | | load: load, |
| | | onReady: onReady, |
| | | t: t, |
| | | tl: translate, |
| | | apply: apply, |
| | | getLocale: getLocale, |
| | | setLocale: setLocale, |
| | | getLocaleOptions: function () { |
| | | return state.localeOptions.slice(); |
| | | }, |
| | | getSupportedLocales: function () { |
| | | return state.supportedLocales.slice(); |
| | | }, |
| | | getDefaultLocale: function () { |
| | | return state.defaultLocale; |
| | | }, |
| | | addNonceUrl: appendNonceUrl, |
| | | isReady: function () { |
| | | return state.ready; |
| | | } |
| | | }; |
| | | |
| | | return api; |
| | | })(window, document); |
| | | |
| | | // 赋值 |
| | | function setVal(el, val) { |
| | | if (el.text() !== val){ |