你不能直接這樣做,因爲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