首页
随机
登录
设置
关于中医百科
免责声明
中医百科
搜索
查看“︁MediaWiki:Gadget-i18n.js”︁的源代码
←
MediaWiki:Gadget-i18n.js
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
此页面为本wiki上的软件提供界面文本,并受到保护以防止滥用。如欲修改所有wiki的翻译,请访问
translatewiki.net
上的MediaWiki本地化项目。
您无权编辑此JavaScript页面,因为编辑此页面可能会影响所有访问者。
您可以查看和复制此页面的源代码。
/** * SPDX-License-Identifier: GPL-3.0-or-later * _addText: '{{Gadget Header|license=GPL-3.0-or-later}}' * * @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/i18n} * @author 安忆 <i@anyi.in> * @license GPL-3.0-or-later {@link https://www.qiuwenbaike.cn/wiki/H:GPL-3.0} */ /** * Copyright (C) 安忆 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ /** * +------------------------------------------------------------+ * | === WARNING: GLOBAL GADGET FILE === | * +------------------------------------------------------------+ * | All changes should be made in the repository, | * | otherwise they will be lost. | * +------------------------------------------------------------+ * | Changes to this page may affect many users. | * | Please discuss changes by opening an issue before editing. | * +------------------------------------------------------------+ */ /* <nowiki> */ (() => { "use strict"; // dist/i18n/i18n.js function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var n = 0, F = function() { }; return { s: F, n: function() { return n >= r.length ? { done: true } : { done: false, value: r[n++] }; }, e: function(r2) { throw r2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = true, u = false; return { s: function() { t = t.call(r); }, n: function() { var r2 = t.next(); return a = r2.done, r2; }, e: function(r2) { u = true, o = r2; }, f: function() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { var _iterator = _createForOfIteratorHelper(__getOwnPropNames(from)), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done; ) { let key = _step.value; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); //! src/i18n/i18n.ts var i18n_exports = {}; __export(i18n_exports, { content: () => content, localize: () => localize, vary: () => vary }); module.exports = __toCommonJS(i18n_exports); //! src/i18n/options.json var defaultLanguageCode = "en"; var fallbackTable = { zh: ["zh", "zh-hans", "zh-hant", "zh-cn", "zh-tw", "zh-hk", "zh-sg", "zh-mo", "zh-my"], "zh-hans": ["zh-hans", "zh-cn", "zh-sg", "zh-my", "zh", "zh-hant", "zh-tw", "zh-hk", "zh-mo"], "zh-hant": ["zh-hant", "zh-tw", "zh-hk", "zh-mo", "zh", "zh-hans", "zh-cn", "zh-sg", "zh-my"], "zh-cn": ["zh-cn", "zh-hans", "zh-sg", "zh-my", "zh", "zh-hant", "zh-tw", "zh-hk", "zh-mo"], "zh-sg": ["zh-sg", "zh-hans", "zh-cn", "zh-my", "zh", "zh-hant", "zh-tw", "zh-hk", "zh-mo"], "zh-my": ["zh-my", "zh-hans", "zh-cn", "zh-sg", "zh", "zh-hant", "zh-tw", "zh-hk", "zh-mo"], "zh-tw": ["zh-tw", "zh-hant", "zh-hk", "zh-mo", "zh", "zh-hans", "zh-cn", "zh-sg", "zh-my"], "zh-hk": ["zh-hk", "zh-hant", "zh-mo", "zh-tw", "zh", "zh-hans", "zh-cn", "zh-sg", "zh-my"], "zh-mo": ["zh-mo", "zh-hant", "zh-hk", "zh-tw", "zh", "zh-hans", "zh-cn", "zh-sg", "zh-my"] }; //! src/i18n/modules/initI18nMethods.ts var import_ext_gadget2 = require("ext.gadget.Util"); //! src/i18n/modules/util/generateDefaultFallbackList.ts var import_ext_gadget = require("ext.gadget.Util"); //! src/i18n/modules/util/generateLanguageCodeSplitArray.ts var generateLanguageCodeSplitArray = (originLanguageCode) => { const languageCodeSplitArray = originLanguageCode.split("-").map((value) => { return value.toLowerCase(); }); return languageCodeSplitArray; }; //! src/i18n/modules/util/generateDefaultFallbackList.ts var generateDefaultFallbackList = () => { const documentLanguageSplitArray = generateLanguageCodeSplitArray(document.documentElement.lang); const navigatorLanguageSplitArray = generateLanguageCodeSplitArray(navigator.language); let languageCode = defaultLanguageCode; for (var _i = 0, _arr = [documentLanguageSplitArray, navigatorLanguageSplitArray]; _i < _arr.length; _i++) { const languageCodeSplitArray = _arr[_i]; switch (languageCodeSplitArray.length) { case 2: languageCode = "".concat(languageCodeSplitArray[0], "-").concat(languageCodeSplitArray[1]); break; case 3: languageCode = "".concat(languageCodeSplitArray[0], "-").concat(languageCodeSplitArray[2]); break; default: languageCode = languageCodeSplitArray[0]; break; } if ((0, import_ext_gadget.isValidKey)(fallbackTable, languageCode)) { break; } } return (0, import_ext_gadget.uniqueArray)([languageCode, defaultLanguageCode]); }; //! src/i18n/modules/initI18nMethods.ts var initI18nMethods = () => { const defaultFallbackList = generateDefaultFallbackList(); const elect = (candidates, locale) => { let fallbackList = defaultFallbackList; for (var _i2 = 0, _arr2 = [locale, ...fallbackList]; _i2 < _arr2.length; _i2++) { const key = _arr2[_i2]; if ((0, import_ext_gadget2.isValidKey)(fallbackTable, key)) { fallbackList = fallbackTable[key]; break; } } var _iterator2 = _createForOfIteratorHelper((0, import_ext_gadget2.uniqueArray)([locale, ...fallbackList, ...defaultFallbackList])), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done; ) { const key = _step2.value; if ((0, import_ext_gadget2.isValidKey)(candidates, key)) { return candidates[key]; } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return ""; }; const { wgContentLanguage, wgUserLanguage, wgUserVariant } = mw.config.get(); const i18nMethods2 = { content: (candidates) => { return elect(candidates, wgContentLanguage); }, localize: (candidates) => { return elect(candidates, wgUserLanguage); }, vary: (candidates) => { return elect(candidates, wgUserVariant !== null && wgUserVariant !== void 0 ? wgUserVariant : wgContentLanguage); } }; return i18nMethods2; }; //! src/i18n/modules/initShims.ts var initShims = (i18nMethods2) => { const wgUXS = (hans, hant, cn, tw, hk, sg, zh, mo, my, en, method) => { var _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9, _ref0, _ref1, _ref10, _ref11, _ref12; zh = String((_ref = (_ref2 = (_ref3 = (_ref4 = (_ref5 = (_ref6 = (_ref7 = (_ref8 = zh !== null && zh !== void 0 ? zh : hans) !== null && _ref8 !== void 0 ? _ref8 : hant) !== null && _ref7 !== void 0 ? _ref7 : cn) !== null && _ref6 !== void 0 ? _ref6 : tw) !== null && _ref5 !== void 0 ? _ref5 : hk) !== null && _ref4 !== void 0 ? _ref4 : sg) !== null && _ref3 !== void 0 ? _ref3 : mo) !== null && _ref2 !== void 0 ? _ref2 : my) !== null && _ref !== void 0 ? _ref : en); hans = String((_ref9 = (_ref0 = (_ref1 = hans !== null && hans !== void 0 ? hans : cn) !== null && _ref1 !== void 0 ? _ref1 : sg) !== null && _ref0 !== void 0 ? _ref0 : my) !== null && _ref9 !== void 0 ? _ref9 : zh); hant = String((_ref10 = (_ref11 = (_ref12 = hant !== null && hant !== void 0 ? hant : tw) !== null && _ref12 !== void 0 ? _ref12 : hk) !== null && _ref11 !== void 0 ? _ref11 : mo) !== null && _ref10 !== void 0 ? _ref10 : zh); cn = String(cn !== null && cn !== void 0 ? cn : hans); sg = String(sg !== null && sg !== void 0 ? sg : hans); tw = String(tw !== null && tw !== void 0 ? tw : hant); hk = String(hk !== null && hk !== void 0 ? hk : hant); mo = String(mo !== null && mo !== void 0 ? mo : hant); my = String(my !== null && my !== void 0 ? my : hant); en = String(en !== null && en !== void 0 ? en : zh); return i18nMethods2[method]({ en, zh, "zh-hans": hans, "zh-hant": hant, "zh-cn": cn, "zh-tw": tw, "zh-hk": hk, "zh-sg": sg, "zh-mo": mo, "zh-my": my }); }; window.wgUCS = (hans, hant, cn, tw, hk, sg, zh, mo, my, en) => { return wgUXS(hans, hant, cn, tw, hk, sg, zh, mo, my, en, "content"); }; window.wgULS = (hans, hant, cn, tw, hk, sg, zh, mo, my, en) => { return wgUXS(hans, hant, cn, tw, hk, sg, zh, mo, my, en, "localize"); }; window.wgUVS = (hans, hant, cn, tw, hk, sg, zh, mo, my, en) => { return wgUXS(hans, hant, cn, tw, hk, sg, zh, mo, my, en, "vary"); }; }; //! src/i18n/i18n.ts var i18nMethods = initI18nMethods(); initShims(i18nMethods); var { content, localize, vary } = i18nMethods; /*! // 仓库内用法 // 定义多条消息 // 支持多语言,可选值定义在modules/types.d.ts#L-1,可直接添加其他值(RFC 5646) import {content, localize, vary} from 'ext.gadget.i18n'; const getI18nMessages = () => { // i18n的三个方法会匹配当前语言和它所接收对象中的键,对于localize来说,假设已定义zh、en和ja,则 // 当页面语言(wgUserLanguage ?? wgContentLanguage)为中文/英语/日语时,返回页面语言所对应的值 // 当页面语言为法语,浏览器语言为中文/英语/日语时,返回浏览器语言所对应的值 // 当页面语言为法语,且不存在浏览器语言所对应的键,返回L-25定义的键所对应的值。若此键同样没被定义,则返回空字符串 return { Cancel: content({ en: 'Cancel', ja: 'キャンセル', zh: '取消', }), QiuWen: localize({ en: 'QiuWen', ja: 'ちゅううん', 'zh-cn': '求闻', 'zh-hk': '求聞', }), LongText: vary({ en: 'This is a very long text', ja: 'これは非常に長いテキストです', 'zh-hans': '这是一段非常长的文本', 'zh-hant': '這是一段非常長的文字', }), }; }; const i18nMessages = getI18nMessages(); const getMessage: GetMessages<typeof i18nMessages> = (key) => { return i18nMessages[key] || key; }; // 调用 getMessage('Cancel'); // en: Cancel, ja: キャンセル, zh: 取消, zh-hans: 取消, zh-hant: 取消, zh-cn: 取消, zh-hk: 取消, zh-tw: 取消 getMessage('QiuWen'); // en: QiuWen, ja: ちゅううん, zh: 求闻, zh-hans: 求闻, zh-hant: 求聞, zh-cn: 求闻, zh-hk: 求聞, zh-tw: 求聞 getMessage('LongText'); // en: This is a very long text // ja: これは非常に長いテキストです // zh: 这是一段非常长的文本 // zh-hans: 这是一段非常长的文本 // zh-hant: 這是一段非常長的文字 // zh-cn: 这是一段非常长的文本 // zh-hk: 這是一段非常長的文字 // zh-tw: 這是一段非常長的文字 // 直接转换单条消息(不推荐,仅为兼容性保留) // 仅支持传递中文和英语。在非中文环境且未传递英语文本时,最终返回zh的值 wgUCS('求闻', '求聞'); // en: 求闻, ja: 求闻, zh: 求闻, zh-hans: 求闻, zh-hant: 求聞, zh-cn: 求闻, zh-hk: 求聞, zh-tw: 求聞 wgULS('求闻', '求聞'); // en: 求闻, ja: 求闻, zh: 求闻, zh-hans: 求闻, zh-hant: 求聞, zh-cn: 求闻, zh-hk: 求聞, zh-tw: 求聞 wgUVS('求闻', '求聞'); // en: 求闻, ja: 求闻, zh: 求闻, zh-hans: 求闻, zh-hant: 求聞, zh-cn: 求闻, zh-hk: 求聞, zh-tw: 求聞 // 非MediaWiki命名空间的脚本可以通过mw.loader.using加载ext.gadget.i18n模块 mw.loader.using('ext.gadget.i18n').then((require) => { const {content, localize, vary} = require('ext.gadget.i18n'); }); */ })(); /* </nowiki> */
该页面使用的模板:
Template:Gadget Header
(
查看源代码
)
返回
MediaWiki:Gadget-i18n.js
。