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.5
  * @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, '');
            // for some reason a semi-colon can be appended to the JSON
            .replace(/;$/, '');


         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
             page = 'u:dev:MediaWiki:Custom-' + name + '/i18n.json';
             // this does not currently support language-prefixed wikis
            apiEndpoint = 'https://dev.wikia.com/api.php';
            page = 'MediaWiki:Custom-' + name + '/i18n.json';


             if (name.indexOf('u:') === 0) {
             if (customSource) {
                 page = name;
                apiEndpoint = apiEndpoint.replace('dev', customSource[1]);
                 page = name.slice(customSource[0].length);
             }
             }


             params = {
             params = {
                 mode: 'articles',
                 action: 'query',
                 articles: page,
                 format: 'json',
                 only: 'styles',
                indexpageids: 1,
                 // keep the json unminified as vstf wiki use comments in their json file
                 prop: 'revisions',
                 debug: '1'
                 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 () {
                 $.get(mw.util.wikiScript('load'), params).done(function (res) {
                 $.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));
                 });
                 });
Cookies help us deliver our services. By using our services, you agree to our use of cookies.