你不能直接这样做,因为JS运行在服务器上的客户端和Lua上。你可以做的是使用the MediaWiki API from JS来调用模块。具体使用the expandtemplates
API module。
例如,如果你想从Module:Hex与参数FF
(在wiki文字{{#invoke:hex|h2d|FF}}
)和alert
结果调用的函数h2d
,那么JS是这样的:
var api = new mw.Api();
api.get({
action: 'expandtemplates',
text: '{{#invoke:hex|h2d|FF}}'
}).done (function (data) {
alert(data.expandtemplates['*']);
});
而且对于OP的具体情况,在英文维基词典上运行:
var langName = 'Esperanto';
(new mw.Api()).get({
action: 'expandtemplates',
format: 'json',
prop: 'wikitext',
text: '{{#invoke:languages/templates|getByCanonicalName|' + langName + '|getCode}}'
}).done(function(data) {
alert('Language name: ' + langName + '\nLanguage code: ' + data.expandtemplates.wikitext);
});
(prop: 'wikitext'
避免了来自API的警告,并且可以让您访问结果为data.expandtemplates.wikitext
,而不是稍微难以理解的data.expandtemplates['*']
。否则没有什么区别。)
顺便提一下,jquery.uls发布了一个从CLDR中获取的langdb。 – Nemo
@Nemo:看起来很有趣 - 现在深入探索它... – hippietrail
[我可以在MediaWiki wiki上使用jquery.uls来从英语语言的名称获取语言代码吗?](http://stackoverflow.com/questions/29485489) – hippietrail