Using the mw alias for mediaWiki in core. Saves bandwidth and makes sense (there...
[lhc/web/wiklou.git] / resources / jquery / jquery.localize.js
1 /**
2 * Simple Placeholder-based Localization
3 *
4 * Call on a selection of HTML which contains <msg key="message-key" /> elements or elements with
5 * title-msg="message-key" or alt-msg="message-key" attributes. <msg /> elements will be replaced
6 * with localized text, elements with title-msg and alt-msg attributes will receive localized title
7 * and alt attributes.
8 *
9 * Note that "msg" elements must have html namespacing such as "<html:msg />" to be compatible with
10 * Internet Explorer.
11 *
12 * Example:
13 * <p class="somethingCool">
14 * <html:msg key="my-message" />
15 * <img src="something.jpg" title-msg="my-title-message" alt-msg="my-alt-message" />
16 * </p>
17 *
18 * Localizes to...
19 * <p class="somethingCool">
20 * My Message
21 * <img src="something.jpg" title="My Title Message" alt="My Alt Message" />
22 * </p>
23 */
24 ( function( $ ) {
25 /**
26 * Localizes a DOM selection by replacing <msg /> elements with localized text and adding
27 * localized title and alt attributes to elements with title-msg and alt-msg attributes
28 * respectively.
29 *
30 * @param Object: options Map of options
31 * * prefix: Message prefix to use when localizing elements and attributes
32 */
33
34 $.fn.localize = function( options ) {
35 options = $.extend( { 'prefix': '' }, options );
36 return $(this)
37 .find( 'msg,html\\:msg' )
38 .each( function() {
39 var $el = $(this);
40 $(this)
41 .text( mw.msg( options.prefix + $(this).attr( 'key' ) ) )
42 .replaceWith( $(this).html() );
43 } )
44 .end()
45 .find( '[title-msg]' )
46 .each( function() {
47 $(this)
48 .attr( 'title', mw.msg( options.prefix + $(this).attr( 'title-msg' ) ) )
49 .removeAttr( 'title-msg' );
50 } )
51 .end()
52 .find( '[alt-msg]' )
53 .each( function() {
54 $(this)
55 .attr( 'alt', mw.msg( options.prefix + $(this).attr( 'alt-msg' ) ) )
56 .removeAttr( 'alt-msg' );
57 } )
58 .end();
59 };
60
61 // Let IE know about the msg tag before it's used...
62 document.createElement( 'msg' );
63 } )( jQuery );