]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/jquery/jquery.localize.js
MediaWiki 1.17.0
[autoinstalls/mediawiki.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  * 
20  *              <p class="somethingCool">
21  *                      My Message
22  *                      <img src="something.jpg" title="My Title Message" alt="My Alt Message" />
23  *              </p>
24  */
25
26 ( function( $, mw ) {
27         /**
28          * Localizes a DOM selection by replacing <msg /> elements with localized text and adding
29          * localized title and alt attributes to elements with title-msg and alt-msg attributes
30          * respectively.
31          * 
32          * @param Object: options Map of options
33          *      * prefix: Message prefix to use when localizing elements and attributes
34          */
35
36         $.fn.localize = function( options ) {
37                 options = $.extend( { 'prefix': '' }, options );
38                 return $(this)
39                         .find( 'msg,html\\:msg' )
40                                 .each( function() {
41                                         $(this)
42                                                 .text( mediaWiki.msg( options.prefix + $(this).attr( 'key' ) ) )
43                                                 .replaceWith( $(this).html() );
44                                 } )
45                                 .end()
46                         .find( '[title-msg]' )
47                                 .each( function() {
48                                         $(this)
49                                                 .attr( 'title', mw.msg( options.prefix + $(this).attr( 'title-msg' ) ) )
50                                                 .removeAttr( 'title-msg' );
51                                 } )
52                                 .end()
53                         .find( '[alt-msg]' )
54                                 .each( function() {
55                                         $(this)
56                                                 .attr( 'alt', mw.msg( options.prefix + $(this).attr( 'alt-msg' ) ) )
57                                                 .removeAttr( 'alt-msg' );
58                                 } )
59                                 .end();
60         };
61 } )( jQuery, mediaWiki );
62
63 // Let IE know about the msg tag before it's used...
64 document.createElement( 'msg' );