Hello everybody,
I'm trying to figure out the best method for internationalization/localization of MediaWiki Gadgets. There are several approaches implemented in the different available gadgets, but none of them seem to use MediaWikis client side message system. I want to use something like
mw.message('my-gadget-some-key').plain();
But this only works with message keys that are available on the client side, based on the 'messages' field of a server side module definition ($wgResourceModules). The clientside
mw.loader.implement( moduleName, scripts, styles, messages );
has a parameter for a 'messages' config object. But it does not load the translation texts for the keys automatically. Actually it expects a JavaScript object that contains keys _and_ values [1]. I've tried whether the values get overridden when a translation on the MediaWiki-namespace is available, but they don't seem to. Am I missing something important? Can you recommend a method for I18N in a gadget?
Just for the record: I've been experimenting with keeping the messages in subpages ("MediaWiki:Gadget.-/de.js") in JSON format [2] and loading them on runtime via AJAX [3]. I think this has some beauty because it's close to what MediaWiki does on the server side. Unfortunately in this case I'll have to implement a language fallback [4] by myself. Any thoughts or hints?
[1] https://www.mediawiki.org/wiki/ResourceLoader/Features#Response [2] https://www.mediawiki.org/w/index.php?title=User%3AOsnard%2FGadget-Test%2Fde... [3] https://www.mediawiki.org/wiki/User:Osnard/Gadget-Test.js [4] https://www.mediawiki.org/wiki/Localisation#mediaviewer/File:MediaWiki_fallb...
--
Robert Vogel Softwareentwicklung
Hallo Welt! - Medienwerkstatt GmbH Residenzstraße 2 93047 Regensburg
Tel. +49 (0) 941 - 66 0 80-198 Fax +49 (0) 941 - 66 0 80-189
www.hallowelt.biz vogel@hallowelt.biz
Sitz: Regensburg Amtsgericht: Regensburg Handelsregister: HRB 10467 E.USt.Nr.: DE 253050833 Geschäftsführer: Anja Ebersbach, Markus Glaser, Dr. Richard Heigl, Radovan Kubani
Gadgets 2.0 will solve this: https://www.mediawiki.org/wiki/Gadgets_2.0#Messages
Helder
You can use mw.messages.set( { key: 'value', … } ) to set the message keys that are used by mw.message() and mw.msg(). No need for mw.loader.implement() (in fact, you should probably never call that function at all).
This has no i18n support by itself, so you have to handle that part yourself (either loading them from somewhere else or just keeping the entire list in source code). A very simple practical example (with no language fallback chain support, it just falls back to English):
var translations = { en: { 'my-gadget-some-key': "My message" }, pl: { 'my-gadget-some-key': "Moja wiadomość" } };
var userLanguage = mw.config.get( 'wgUserLanguage' ); mw.messages.set( translations[userLanguage] || translations.en );
Hi Bartosz,
thank you very much. This is exactly the information I needed.
-- Robert
-----Ursprüngliche Nachricht----- Von: wikitech-l-bounces@lists.wikimedia.org [mailto:wikitech-l-bounces@lists.wikimedia.org] Im Auftrag von Bartosz Dziewonski Gesendet: Mittwoch, 23. Juli 2014 17:19 An: Wikimedia developers Betreff: Re: [Wikitech-l] Gadget I18N: Recommendation?
You can use mw.messages.set( { key: 'value', … } ) to set the message keys that are used by mw.message() and mw.msg(). No need for mw.loader.implement() (in fact, you should probably never call that function at all).
This has no i18n support by itself, so you have to handle that part yourself (either loading them from somewhere else or just keeping the entire list in source code). A very simple practical example (with no language fallback chain support, it just falls back to English):
var translations = { en: { 'my-gadget-some-key': "My message" }, pl: { 'my-gadget-some-key': "Moja wiadomość" } };
var userLanguage = mw.config.get( 'wgUserLanguage' ); mw.messages.set( translations[userLanguage] || translations.en );
_______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
wikitech-l@lists.wikimedia.org