Page MenuHomePhabricator

Implement mw.Api convenience method for loading interface messages
Closed, ResolvedPublic

Description

Author: p.selitskas

Description:
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?


Version: unspecified
Severity: enhancement

Attached:

Event Timeline

bzimport raised the priority of this task from to Lowest.Nov 22 2014, 1:03 AM
bzimport set Reference to bz38280.
bzimport added a subscriber: Unknown Object (MLST).

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 );

p.selitskas wrote:

(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).

(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.

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.

Change 251796 had a related patch set uploaded (by Florianschmidtwelzow):
Add a messages api module to easily retrieve a set of messages

https://gerrit.wikimedia.org/r/251796

Krinkle raised the priority of this task from Lowest to Low.
Krinkle edited projects, added MediaWiki-Action-API; removed MediaWiki-General.
Krinkle set Security to None.
Krinkle removed a subscriber: wikibugs-l-list.

Change 251796 merged by jenkins-bot:
Add mediawiki.api.messages module to easily retrieve a set of messages

https://gerrit.wikimedia.org/r/251796