Removing unneeded wraps and using $ and mw instead of jQuery or mediaWiki since r7924...
[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 * Example:
10 * <p class="somethingCool">
11 * <msg key="my-message" />
12 * <img src="something.jpg" title-msg="my-title-message" alt-msg="my-alt-message" />
13 * </p>
14 *
15 * Localizes to...
16 * <p class="somethingCool">
17 * My Message
18 * <img src="something.jpg" title="My Title Message" alt="My Alt Message" />
19 * </p>
20 */
21 /**
22 * Localizes a DOM selection by replacing <msg /> elements with localized text and adding
23 * localized title and alt attributes to elements with title-msg and alt-msg attributes
24 * respectively.
25 *
26 * @param Object: options Map of options
27 * * prefix: Message prefix to use when localizing elements and attributes
28 */
29 $.fn.localize = function( options ) {
30 options = $.extend( { 'prefix': '' }, options );
31 return $(this)
32 .find( 'msg' )
33 .each( function() {
34 $(this)
35 .text( mediaWiki.msg( options.prefix + $(this).attr( 'key' ) ) )
36 .replaceWith( $(this).html() );
37 } )
38 .end()
39 .find( '[title-msg]' )
40 .each( function() {
41 $(this)
42 .attr( 'title', mw.msg( options.prefix + $(this).attr( 'title-msg' ) ) )
43 .removeAttr( 'title-msg' );
44 } )
45 .end()
46 .find( '[alt-msg]' )
47 .each( function() {
48 $(this)
49 .attr( 'alt', mw.msg( options.prefix + $(this).attr( 'alt-msg' ) ) )
50 .removeAttr( 'alt-msg' );
51 } )
52 .end();
53 };