2 * @class jQuery.plugin.localize
7 * Gets a localized message, using parameters from options if present.
10 * @param {Object} options
12 * @returns {string} Localized message
14 function msg( options, key ) {
15 var args = options.params[key] || [];
16 // Format: mw.msg( key [, p1, p2, ...] )
17 args.unshift( options.prefix + ( options.keys[key] || key ) );
18 return mw.msg.apply( mw, args );
22 * Localizes a DOM selection by replacing <html:msg /> elements with localized text and adding
23 * localized title and alt attributes to elements with title-msg and alt-msg attributes
26 * Call on a selection of HTML which contains `<html:msg key="message-key" />` elements or elements
27 * with title-msg="message-key", alt-msg="message-key" or placeholder-msg="message-key" attributes.
28 * `<html:msg />` elements will be replaced with localized text, *-msg attributes will be replaced
29 * with attributes that do not have the "-msg" suffix and contain a localized message.
32 * // Messages: { 'title': 'Awesome', 'desc': 'Cat doing backflip' 'search' contains 'Search' }
35 * <html:msg key="title" />\
36 * <img src="something.jpg" title-msg="title" alt-msg="desc" />\
37 * <input type="text" placeholder-msg="search" />\
39 * $( 'body' ).append( $( html ).localize() );
41 * Appends something like this to the body...
44 * <img src="something.jpg" title="Awesome" alt="Cat doing backflip" />
45 * <input type="text" placeholder="Search" />
48 * Arguments can be passed into uses of a message using the params property of the options object
49 * given to .localize(). Multiple messages can be given parameters, because the params property is
50 * an object keyed by the message key to apply the parameters to, each containing an array of
51 * parameters to use. The limitation is that you can not use different parameters to individual uses
52 * of a message in the same selection being localized - they will all recieve the same parameters.
55 * // Messages: { 'easy-as': 'Easy as $1 $2 $3.' }
56 * var html = '<p><html:msg key="easy-as" /></p>';
57 * $( 'body' ).append( $( html ).localize( { 'params': { 'easy-as': ['a', 'b', 'c'] } } ) );
59 * Appends something like this to the body...
60 * <p>Easy as a, b, c</p>
62 * Raw HTML content can be used, instead of it being escaped as text. To do this, just use the raw
63 * attribute on a msg element.
66 * // Messages: { 'hello': '<b><i>Hello</i> $1!</b>' }
69 * <!-- escaped: --><html:msg key="hello" />\
70 * <!-- raw: --><html:msg key="hello" raw />\
72 * $( 'body' ).append( $( html ).localize( { 'params': { 'hello': ['world'] } } ) );
74 * Appends something like this to the body...
76 * <!-- escaped: --><b><i>Hello</i> world!</b>
77 * <!-- raw: --><b><i>Hello</i> world!</b>
80 * Message keys can also be remapped, allowing the same generic template to be used with a variety
81 * of messages. This is important for improving re-usability of templates.
84 * // Messages: { 'good-afternoon': 'Good afternoon' }
85 * var html = '<p><html:msg key="greeting" /></p>';
86 * $( 'body' ).append( $( html ).localize( { 'keys': { 'greeting': 'good-afternoon' } } ) );
88 * Appends something like this to the body...
89 * <p>Good afternoon</p>
91 * Message keys can also be prefixed globally, which is handy when writing extensions, where by
92 * convention all messages are prefixed with the extension's name.
95 * // Messages: { 'teleportation-warning': 'You may not get there all in one piece.' }
96 * var html = '<p><html:msg key="warning" /></p>';
97 * $( 'body' ).append( $( html ).localize( { 'prefix': 'teleportation-' } ) );
99 * Appends something like this to the body...
100 * <p>You may not get there all in one piece.</p>
103 * @param {Object} options Map of options to be used while localizing
104 * @param {string} options.prefix String to prepend to all message keys
105 * @param {Object} options.keys Message key aliases, used for remapping keys to a template
106 * @param {Object} options.params Lists of parameters to use with certain message keys
109 $.fn.localize = function ( options ) {
111 attributes = ['title', 'alt', 'placeholder'];
114 options = $.extend( {
121 // Ok, so here's the story on this selector. In IE 6/7, searching for 'msg' turns up the
122 // 'html:msg', but searching for 'html:msg' doesn't. In later IE and other browsers, searching
123 // for 'html:msg' turns up the 'html:msg', but searching for 'msg' doesn't. So searching for
124 // both 'msg' and 'html:msg' seems to get the job done. This feels pretty icky, though.
125 $target.find( 'msg,html\\:msg' ).each( function () {
128 if ( $el.attr( 'raw' ) ) {
129 $el.html( msg( options, $el.attr( 'key' ) ) );
131 $el.text( msg( options, $el.attr( 'key' ) ) );
134 $el.replaceWith( $el.html() );
138 // Note: there's no way to prevent escaping of values being injected into attributes, this is
139 // on purpose, not a design flaw.
140 $.each( attributes, function ( i, attr ) {
141 var msgAttr = attr + '-msg';
142 $target.find( '[' + msgAttr + ']' ).each( function () {
144 $el.attr( attr, msg( options, $el.attr( msgAttr ) ) ).removeAttr( msgAttr );
148 // HTML, Text for elements which cannot have children e.g. OPTION
149 $target.find( '[data-msg-text]' ).each( function() {
151 $el.text( msg( options, $el.attr( 'data-msg-text' ) ) );
154 $target.find( '[data-msg-html]' ).each( function() {
156 $el.html( msg( options, $el.attr( 'data-msg-html' ) ) );
162 // Let IE know about the msg tag before it's used...
163 document.createElement( 'msg' );
167 * @mixins jQuery.plugin.localize
170 }( jQuery, mediaWiki ) );