Last modified: 2014-09-24 00:49:55 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T40280, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 38280 - Implement mw.Api convenience method for loading interface messages
Implement mw.Api convenience method for loading interface messages
Status: NEW
Product: MediaWiki
Classification: Unclassified
JavaScript (Other open bugs)
unspecified
All All
: Lowest enhancement (vote)
: ---
Assigned To: Nobody - You can work on this!
: javascript
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-07-10 17:17 UTC by Pavel Selitskas [wizardist]
Modified: 2014-09-24 00:49 UTC (History)
3 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments
Message queries (451 bytes, patch)
2012-07-10 17:17 UTC, Pavel Selitskas [wizardist]
Details

Description Pavel Selitskas [wizardist] 2012-07-10 17:17:08 UTC
Created attachment 10837 [details]
Message queries

AFAIK, ResourceLoader doesn't support loading interface messages by ajax request, required messages are specified in the ResourceLoader module description and provided every time the module is loaded.

This feature would have a lot of uses, at least one can get rid of numerous arrays of messages in javascript, and load them with ResourceLoader directly to the mw.messages, thus giving more opportunities for i18n stuff.

I've written a small function that takes the given messages from the API. It's stored in the attachment.

So could you implement this feature in MediaWiki by default?
Comment 1 Krinkle 2012-07-20 04:01:04 UTC
Changing bug to be a low-priority enhancement, because it is currently already possible. Just not with a single function call.

* mw.messages is designed with dynamic loading in mind, this is used all the time whenever a module is loaded from the server through mw.loader.
* mw.Api supports making your own queries, should we add a custom method for every single api method  if the plain callee is straight forward with only 5 lines of code?
* Aside from mw.Api, $.getJSON is also available with a few more lines of code.

Example of how to do this directly:

var api = new mw.Api();
api.get({
	action: 'query',
	meta: 'allmessages',
	ammessages: msgKeys.join('|'),
	amlang: mw.config.get('wgUserLanguage')
}).done( function (data) {
	if ( data && data.query && data.query.allmessages ) {
		$.each(data.query.allmessages, function (i, obj) {
			if (obj['*']) {
				mw.messages.set(obj.name, obj['*']);
			}
		});
	}
});

If you'd like to implement an mw.Api utility method to make this easier, you're welcome to provide a patch. A few pointers:
* Provide a patch instead of a partial snippet of a file. Assuming you have a local checkout of the mediawiki/core.git repository. Use `git checkout -b js-api-messages`. Then make the local changes, then use `git diff` to see the patch. To store it in a file use `git diff > mw-bug-38280.patch` and attach it to this ticket.
* Leave the mw.messages.set part out of it, so that the method is generally useful for getting messages. It would call the callback with an object containing key/value pairs. So that in consuming code it would look like this:

-- dependencies => 'mediawiki.api.allmessages'
var api = new mw.Api();
api.allmessages( ['monday', 'tuesday', 'friday'] ).done( mw.messages.set );
Comment 2 Sumana Harihareswara 2012-07-20 17:50:05 UTC
A few links you might find useful, Paul:

https://www.mediawiki.org/wiki/How_to_become_a_MediaWiki_hacker

https://www.mediawiki.org/wiki/Developer_access
Comment 3 Pavel Selitskas [wizardist] 2012-07-20 20:42:03 UTC
(In reply to comment #2)
> A few links you might find useful, Paul:
> 
> https://www.mediawiki.org/wiki/How_to_become_a_MediaWiki_hacker
> 
> https://www.mediawiki.org/wiki/Developer_access

Thank you, and thanks to Krinkle. I'm aware of how to become a MediaWiki hacker :) but it's not the point.

I just wanted to notice that ResourceLoader (particularly, mw.loader.load/using) lacks this feature. For example, some gadgets, also scripts loaded via mw.loader.load, could use messages from MediaWiki: namespace (via MediaWiki:Gadgets-definition and mw.loader.load call arguments), like

  mw.loader.load('//domain.tld/w/index.php?...', ['message1', 'message2'])

(I don't represent actual loader.load arguments here).
Comment 4 Krinkle 2012-07-21 02:28:59 UTC
(In reply to comment #3)
> I just wanted to notice that ResourceLoader (particularly,
> mw.loader.load/using) lacks this feature. For example, some gadgets, also
> scripts loaded via mw.loader.load, could use messages from MediaWiki: namespace
> (via MediaWiki:Gadgets-definition and mw.loader.load call arguments)

As I said before, the infrastructure is all there. These 3 lines of code will make it slightly easier but implementing that would still add the requirement that the user loads this new module (the mediawiki.api.messages module) first. So the benefit is minor, but I don't object it. If you want to write or need help writing a patch, I'm happy to do that and will gladly merge it into core.

Regarding the ability to use mediawiki namespace messages in modules. This is not true. ResourceLoader is designed with localization at heart from the every beginning. Modules are constructed of mainly three resources:
* scripts
* styles
* messages

All of these are primary "citizens" in the module universe of ResourceLoader.

However, when working with ResourceLoader modules built from *Gadgets* (as opposed to server-side defined modules), then you are correct that there is no way to tell ResourceLoader about the messages that are needed. But, it is important to note that this is a missing feature in Gadgets, not ResourceLoader. The server-side infrastructure in ResourceLoader for messages is pretty elaborate and bullet-proof. It's  much than passing an array of message keys to the loader. In ResourceLoader the messages are primary citizens and associated with the module itself, so there is no need to specify this at loading time. Instead, it is taken care in advance and all you need to do is call the module by its name (e.g. mw.loader.using( 'ext.foo.bar' )) which will automatically fetch the scripts, styles and messages associated with that module (usually cached already).

The Gadgets extensions just doesn't make use of this yet, and that is a different issue all together. The way this could be done for gadgets is to add syntax like [messages=monday,tuesday,hello-whatever] to the definition page.

Right now the Gadgets extension is being rewritten from scratch to make use all of the great features that ResourceLoader provides. The progress of this rewrite is very far and expected to be finished within a few weeks or months.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links