* (bug 26541) Generator-ise ApiQueryRecentChanges
[mediawiki.git] / resources / jquery / jquery.localize.js
blob1f65833142168601a8898279edc64ec063b90c6e
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  * Example:
10  * 
11  *              <p class="somethingCool">
12  *                      <msg key="my-message" />
13  *                      <img src="something.jpg" title-msg="my-title-message" alt-msg="my-alt-message" />
14  *              </p>
15  * 
16  * Localizes to...
17  * 
18  *              <p class="somethingCool">
19  *                      My Message
20  *                      <img src="something.jpg" title="My Title Message" alt="My Alt Message" />
21  *              </p>
22  */
24 ( function( $, mw ) {
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         $.fn.localize = function( options ) {
34                 options = $.extend( { 'prefix': '' }, options );
35                 return $(this)
36                         .find( 'msg' )
37                                 .each( function() {
38                                         $(this)
39                                                 .text( mediaWiki.msg( options.prefix + $(this).attr( 'key' ) ) )
40                                                 .replaceWith( $(this).html() );
41                                 } )
42                                 .end()
43                         .find( '[title-msg]' )
44                                 .each( function() {
45                                         $(this)
46                                                 .attr( 'title', mw.msg( options.prefix + $(this).attr( 'title-msg' ) ) )
47                                                 .removeAttr( 'title-msg' );
48                                 } )
49                                 .end()
50                         .find( '[alt-msg]' )
51                                 .each( function() {
52                                         $(this)
53                                                 .attr( 'alt', mw.msg( options.prefix + $(this).attr( 'alt-msg' ) ) )
54                                                 .removeAttr( 'alt-msg' );
55                                 } )
56                                 .end();
57         };
58 } )( jQuery, mediaWiki );