Index: branches/wmf/1.17wmf1/resources/jquery/jquery.localize.js |
— | — | @@ -1,14 +1,11 @@ |
2 | 2 | /** |
3 | | - * Simple Placeholder-based Localization |
4 | | - * |
5 | | - * Call on a selection of HTML which contains <msg key="message-key" /> elements or elements with |
6 | | - * title-msg="message-key" or alt-msg="message-key" attributes. <msg /> elements will be replaced |
| 3 | + * Simple Placeholder-based Localization |
| 4 | + * |
| 5 | + * Call on a selection of HTML which contains <html:msg key="message-key" /> elements or elements with |
| 6 | + * title-msg="message-key" or alt-msg="message-key" attributes. <html:msg /> elements will be replaced |
7 | 7 | * with localized text, elements with title-msg and alt-msg attributes will receive localized title |
8 | 8 | * and alt attributes. |
9 | | - * |
10 | | - * Note that "msg" elements must have html namespacing such as "<html:msg />" to be compatible with |
11 | | - * Internet Explorer. |
12 | | - * |
| 9 | + * * |
13 | 10 | * Example: |
14 | 11 | * <p class="somethingCool"> |
15 | 12 | * <html:msg key="my-message" /> |
— | — | @@ -16,49 +13,61 @@ |
17 | 14 | * </p> |
18 | 15 | * |
19 | 16 | * Localizes to... |
20 | | - * |
21 | | - * <p class="somethingCool"> |
22 | | - * My Message |
23 | | - * <img src="something.jpg" title="My Title Message" alt="My Alt Message" /> |
24 | | - * </p> |
| 17 | + * <p class="somethingCool"> |
| 18 | + * My Message |
| 19 | + * <img src="something.jpg" title="My Title Message" alt="My Alt Message" /> |
| 20 | + * </p> |
25 | 21 | */ |
| 22 | +( function( $ ) { |
| 23 | +/** |
| 24 | + * Localizes a DOM selection by replacing <html:msg /> elements with localized text and adding |
| 25 | + * localized title and alt attributes to elements with title-msg and alt-msg attributes |
| 26 | + * respectively. |
| 27 | + * |
| 28 | + * @param Object: options Map of options |
| 29 | + * * prefix: Message prefix to use when localizing elements and attributes |
| 30 | + */ |
26 | 31 | |
27 | | -( function( $, mw ) { |
28 | | - /** |
29 | | - * Localizes a DOM selection by replacing <msg /> elements with localized text and adding |
30 | | - * localized title and alt attributes to elements with title-msg and alt-msg attributes |
31 | | - * respectively. |
32 | | - * |
33 | | - * @param Object: options Map of options |
34 | | - * * prefix: Message prefix to use when localizing elements and attributes |
35 | | - */ |
36 | | - |
37 | | - $.fn.localize = function( options ) { |
38 | | - options = $.extend( { 'prefix': '' }, options ); |
39 | | - return $(this) |
40 | | - .find( 'msg,html\\:msg' ) |
41 | | - .each( function() { |
42 | | - $(this) |
43 | | - .text( mediaWiki.msg( options.prefix + $(this).attr( 'key' ) ) ) |
44 | | - .replaceWith( $(this).html() ); |
45 | | - } ) |
46 | | - .end() |
47 | | - .find( '[title-msg]' ) |
48 | | - .each( function() { |
49 | | - $(this) |
50 | | - .attr( 'title', mw.msg( options.prefix + $(this).attr( 'title-msg' ) ) ) |
51 | | - .removeAttr( 'title-msg' ); |
52 | | - } ) |
53 | | - .end() |
54 | | - .find( '[alt-msg]' ) |
55 | | - .each( function() { |
56 | | - $(this) |
57 | | - .attr( 'alt', mw.msg( options.prefix + $(this).attr( 'alt-msg' ) ) ) |
58 | | - .removeAttr( 'alt-msg' ); |
59 | | - } ) |
60 | | - .end(); |
| 32 | +$.fn.localize = function( options ) { |
| 33 | + options = $.extend( { 'prefix': '', 'keys': {}, 'params': {} }, options ); |
| 34 | + function msg( key ) { |
| 35 | + var args = key in options.params ? options.params[key] : []; |
| 36 | + // Format: mw.msg( key [, p1, p2, ...] ) |
| 37 | + args.unshift( options.prefix + ( key in options.keys ? options.keys[key] : key ) ); |
| 38 | + return mw.msg.apply( mw, args ); |
61 | 39 | }; |
62 | | -} )( jQuery, mediaWiki ); |
| 40 | + return $(this) |
| 41 | + // Ok, so here's the story on this selector. |
| 42 | + // In IE 6/7, searching for 'msg' turns up the 'html:msg', but searching for 'html:msg' does not. |
| 43 | + // In later IE and other browsers, searching for 'html:msg' turns up the 'html:msg', but searching for 'msg' does not. |
| 44 | + // So searching for both 'msg' and 'html:msg' seems to get the job done. |
| 45 | + // This feels pretty icky, though. |
| 46 | + .find( 'msg,html\\:msg' ) |
| 47 | + .each( function() { |
| 48 | + var $el = $(this); |
| 49 | + $el |
| 50 | + .text( msg( $el.attr( 'key' ) ) ) |
| 51 | + .replaceWith( $el.html() ); |
| 52 | + } ) |
| 53 | + .end() |
| 54 | + .find( '[title-msg]' ) |
| 55 | + .each( function() { |
| 56 | + var $el = $(this); |
| 57 | + $el |
| 58 | + .attr( 'title', msg( $el.attr( 'title-msg' ) ) ) |
| 59 | + .removeAttr( 'title-msg' ); |
| 60 | + } ) |
| 61 | + .end() |
| 62 | + .find( '[alt-msg]' ) |
| 63 | + .each( function() { |
| 64 | + var $el = $(this); |
| 65 | + $el |
| 66 | + .attr( 'alt', msg( $el.attr( 'alt-msg' ) ) ) |
| 67 | + .removeAttr( 'alt-msg' ); |
| 68 | + } ) |
| 69 | + .end(); |
| 70 | +}; |
63 | 71 | |
64 | 72 | // Let IE know about the msg tag before it's used... |
65 | 73 | document.createElement( 'msg' ); |
| 74 | +} )( jQuery ); |
Property changes on: branches/wmf/1.17wmf1/resources/jquery/jquery.localize.js |
___________________________________________________________________ |
Deleted: svn:mime-type |
66 | 75 | - text/plain |