跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
分类索引
最近更改
随便看看
灵兰秘典
捐助本站
帮助
帮助
联系我们
关于本站
MediaWiki帮助
中医百科
搜索
搜索
外观
登录
个人工具
登录
查看“︁MediaWiki:Gadget-HanAssist.js”︁的源代码
系统消息
讨论
English
阅读
查看源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
查看源代码
查看历史
清除缓存
常规
链入页面
相关更改
特殊页面
页面信息
Cargo数据
短URL
外观
移至侧栏
隐藏
←
MediaWiki:Gadget-HanAssist.js
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
此页面为本wiki上的软件提供界面文本,并受到保护以防止滥用。 如欲修改所有wiki的翻译,请访问
translatewiki.net
上的MediaWiki本地化项目。
您无权编辑此JavaScript页面,因为编辑此页面可能会影响所有访问者。
您可以查看和复制此页面的源代码。
/**! * _________________________________________________________________________________ * | | * | === WARNING: GLOBAL GADGET FILE === | * | Changes to this page affect many users. | * | Please discuss changes on the talk page, [[WP:VPT]] or GitHub before editing. | * |_________________________________________________________________________________| * * Built from source code at HanAssist GitHub repository https://github.com/wikimedia-gadgets/HanAssist. * All changes should be made in the repository, otherwise they will be lost. * * To update this script from GitHub, you must have a local repository set up. Then * follow the instructions at https://github.com/wikimedia-gadgets/HanAssist/blob/main/README.MD#%E6%9E%84%E5%BB%BA%E6%96%B9%E6%B3%95. * * For license information, see https://github.com/wikimedia-gadgets/HanAssist/blob/main/LICENSE. */ // <nowiki> (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define('HanAssist', ['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.mw = global.mw || {}, global.mw.libs = global.mw.libs || {}, global.mw.libs.HanAssist = {}))); })(this, (function (exports) { 'use strict'; /** * Safely convert an object to string. * @param val value to convert * @return string */ function safelyToString(val) { try { if (typeof val === 'undefined' || val === null) { return ''; } // eslint-disable-next-line @typescript-eslint/no-base-to-string return String(val); } catch (_a) { return Object.prototype.toString.call(val); } } function isPlainObject(val) { return $.isPlainObject(val); } const FALLBACK_LIST = { zh: ['zh', 'hans', 'hant', 'cn', 'tw', 'hk', 'sg', 'mo', 'my', 'other'], 'zh-hans': ['hans', 'cn', 'sg', 'my', 'zh', 'hant', 'tw', 'hk', 'mo', 'other'], 'zh-hant': ['hant', 'tw', 'hk', 'mo', 'zh', 'hans', 'cn', 'sg', 'my', 'other'], 'zh-cn': ['cn', 'hans', 'sg', 'my', 'zh', 'hant', 'tw', 'hk', 'mo', 'other'], 'zh-sg': ['sg', 'hans', 'cn', 'my', 'zh', 'hant', 'tw', 'hk', 'mo', 'other'], 'zh-my': ['my', 'sg', 'hans', 'cn', 'zh', 'hant', 'tw', 'hk', 'mo', 'other'], 'zh-tw': ['tw', 'hant', 'hk', 'mo', 'zh', 'hans', 'cn', 'sg', 'my', 'other'], 'zh-hk': ['hk', 'hant', 'mo', 'tw', 'zh', 'hans', 'cn', 'sg', 'my', 'other'], 'zh-mo': ['mo', 'hk', 'hant', 'tw', 'zh', 'hans', 'cn', 'sg', 'my', 'other'], }; const DEFAULT_FALLBACK = ['other', 'zh', 'hans', 'hant', 'cn', 'tw', 'hk', 'sg', 'mo', 'my']; function elect(candidates, locale) { var _a; const fallback = (_a = FALLBACK_LIST[locale]) !== null && _a !== undefined ? _a : DEFAULT_FALLBACK; // Try every locale sequently for (const key of fallback) { const value = candidates[key]; // Return if the value is neither null nor undefined if (value != null) { return value; } } return null; } function preprocessLocale(locale) { if (locale == null) { mw.log.warn('[HanAssist] locale parameter is null-ish. Defaulting to wgUserLanguage.'); return mw.config.get('wgUserLanguage'); } if (typeof locale !== 'string') { mw.log.warn('[HanAssist] locale parameter must be a string. Please check your code.'); return safelyToString(locale); } return locale; } /** * A wrapper around `elect()` to ensure no non-string results are returned. */ function safeElect(candidates, locale) { var _a; // Guards to ensure types at runtime if (!isPlainObject(candidates)) { throw new TypeError('[HanAssist] Invalid parameter. Must be an object.'); } locale = preprocessLocale(locale); const result = (_a = elect(candidates, locale)) !== null && _a !== undefined ? _a : ''; if (typeof result !== 'string') { mw.log.warn('[HanAssist] Non-string conversion result detected. Please check your code.'); } // Wrap in another guard to ensure result is really string at runtime return safelyToString(result); } /** * Select between candidates based on user language. * @param candidates an object of candidates * @param locale locale, defaults to `wgUserLanguage` * @returns selected value */ function conv(candidates, locale = mw.config.get('wgUserLanguage')) { return safeElect(candidates, locale); } /** * Select between candidates based on user variant. * @param candidates an object of candidates * @returns selected value */ function convByVar(candidates) { var _a, _b; return safeElect(candidates, (_b = (_a = mw.config.get('wgUserVariant')) !== null && _a !== undefined ? _a : new URL(location.href).searchParams.get('variant')) !== null && _b !== undefined ? _b : mw.user.options.get('variant')); } /** * Perform selection for each item in a candidates dictionary. * @param candidatesDict the dictionary of candidates * @param locale locale, defaults to `wgUserLanguage` * @returns converted candidates dictionary */ function batchConv(candidatesDict, locale = mw.config.get('wgUserLanguage')) { var _a; if (!isPlainObject(candidatesDict)) { throw new TypeError('[HanAssist] Invalid parameter. Must be an object.'); } locale = preprocessLocale(locale); const result = {}; for (const key in candidatesDict) { const candidates = candidatesDict[key]; if (isPlainObject(candidates)) { const electResult = (_a = elect(candidates, locale)) !== null && _a !== undefined ? _a : ''; if (typeof electResult !== 'string') { mw.log.warn(`[HanAssist] In '${key}': Non-string conversion result detected. Please check your code.`); } result[key] = safelyToString(electResult); } else { if (typeof candidates !== 'string') { mw.log.warn(`[HanAssist] In '${key}': Value is neither a candidate list nor a string. Please check your code.`); } result[key] = safelyToString(candidates); } } return result; } { void Promise.resolve().then(function () { return shims; }); } function uxsShim(locale, hans, hant, cn, tw, hk, sg, zh, mo, my) { try { return elect({ hans, hant, cn, tw, hk, sg, zh, mo, my, }, locale); } catch (_a) { return undefined; } } function generateUxsShim(configName) { return (hans, hant, cn, tw, hk, sg, zh, mo, my) => uxsShim(mw.config.get(configName), hans, hant, cn, tw, hk, sg, zh, mo, my); } // eslint-disable-next-line @typescript-eslint/no-explicit-any function deprecate(root, name, func, replacement) { mw.log.deprecate(root, name, func, `Use ${replacement} instead. See [[Wikipedia:HanAssist]] for more information.`); } // Compatibility: redirect wgULS, wgUVS and wgUXS calls to HanAssist implementation deprecate(self, 'wgULS', generateUxsShim('wgUserLanguage'), 'HanAssist.conv'); deprecate(self, 'wgUVS', generateUxsShim('wgUserVariant'), 'HanAssist.convByVar'); deprecate(self, 'wgUXS', uxsShim, 'HanAssist.conv'); // Compatibility: redirect HanAssist <= v3 calls to v4 const globalMountPoint = (mw.libs.HanAssist = mw.libs.HanAssist || {}); deprecate(globalMountPoint, 'localize', conv, 'conv'); deprecate(globalMountPoint, 'vary', convByVar, 'convByVar'); deprecate(globalMountPoint, 'parse', batchConv, 'batchConv'); var shims = /*#__PURE__*/Object.freeze({ __proto__: null }); exports.batchConv = batchConv; exports.conv = conv; exports.convByVar = convByVar; })); // </nowiki>
返回
MediaWiki:Gadget-HanAssist.js
。