MediaWiki:Gadget-I18n-js.js: Difference between revisions
From Tardis Wiki, the free Doctor Who reference
use Dev Wiki API via JSONP to load messages, rather than ResourceLoader's 'styles' mode
(remove support for inline comments in JSON pages (Thread:19272#21)) |
(use Dev Wiki API via JSONP to load messages, rather than ResourceLoader's 'styles' mode) |
||
Line 4: | Line 4: | ||
* | * | ||
* @author Cqm <https://dev.wikia.com/User:Cqm> | * @author Cqm <https://dev.wikia.com/User:Cqm> | ||
* @version 0.4. | * @version 0.4.6 | ||
* | * | ||
* @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page) | * @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page) | ||
Line 637: | Line 637: | ||
json = json.trim() | json = json.trim() | ||
.replace(/\/\*[\s\S]*?\*\//g | .replace(/\/\*[\s\S]*?\*\//g, ''); | ||
mw.log('[I18n-js] Comment-stripped i18n.json:', json); | mw.log('[I18n-js] Comment-stripped i18n.json:', json); | ||
Line 690: | Line 688: | ||
function loadMessages(name) { | function loadMessages(name) { | ||
var deferred = $.Deferred(), | var deferred = $.Deferred(), | ||
customSource = name.match(/^u:([a-z0-9-]+):/), | |||
apiEndpoint, | |||
page, | page, | ||
params; | params; | ||
Line 698: | Line 698: | ||
// allow custom i18n pages to be specified on other wikis | // allow custom i18n pages to be specified on other wikis | ||
// mainly for VSTF wiki to keep their own JSON file | // mainly for VSTF wiki to keep their own JSON file | ||
// this does not currently support language-prefixed wikis | |||
apiEndpoint = 'https://dev.wikia.com/api.php'; | |||
page = 'MediaWiki:Custom-' + name + '/i18n.json'; | |||
if ( | if (customSource) { | ||
page = name; | apiEndpoint = apiEndpoint.replace('dev', customSource[1]); | ||
page = name.slice(customSource[0].length); | |||
} | } | ||
params = { | params = { | ||
action: 'query', | |||
format: 'json', | |||
indexpageids: 1, | |||
prop: 'revisions', | |||
rvprop: 'content', | |||
titles: page | |||
}; | }; | ||
Line 716: | Line 720: | ||
// generally, we will implicitly depend on those anyway due to where/when this is loaded | // generally, we will implicitly depend on those anyway due to where/when this is loaded | ||
mw.loader.using(['mediawiki.language', 'mediawiki.util'/*, 'site', 'user'*/], function () { | mw.loader.using(['mediawiki.language', 'mediawiki.util'/*, 'site', 'user'*/], function () { | ||
$. | $.ajax(apiEndpoint, { | ||
data: params, | |||
dataType: 'jsonp' | |||
}).always(function (data) { | |||
var res = '', | |||
revisionData = data.query && data.query.pages[data.query.pageids[0]].revisions; | |||
if (revisionData) { | |||
res = revisionData[0]['*']; | |||
} | |||
deferred.resolve(parseMessagesToObject(name, res)); | deferred.resolve(parseMessagesToObject(name, res)); | ||
}); | }); |