From 4cb694ec6c639c4a101b7d1da2b630c77e3ca519 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Sat, 4 Dec 2010 02:16:37 +0000 Subject: [PATCH] Added new localize plugin which allows simple placeholder based localization of a dom selection with special elements or elements with title-msg or alt-msg attributes. --- resources/jquery/jquery.localize.js | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 resources/jquery/jquery.localize.js diff --git a/resources/jquery/jquery.localize.js b/resources/jquery/jquery.localize.js new file mode 100644 index 0000000000..a7aef5dfc4 --- /dev/null +++ b/resources/jquery/jquery.localize.js @@ -0,0 +1,58 @@ +/** + * Simple Placeholder-based Localization + * + * Call on a selection of HTML which contains elements or elements with + * title-msg="message-key" or alt-msg="message-key" attributes. elements will be replaced + * with localized text, elements with title-msg and alt-msg attributes will receive localized title + * and alt attributes. + * + * Example: + * + *

+ * + * + *

+ * + * Localizes to... + * + *

+ * My Message + * My Alt Message + *

+ */ + +( function( $, mw ) { + /** + * Localizes a DOM selection by replacing elements with localized text and adding + * localized title and alt attributes to elements with title-msg and alt-msg attributes + * respectively. + * + * @param Object: options Map of options + * * prefix: Message prefix to use when localizing elements and attributes + */ + $.fn.localize = function( options ) { + options = $.extend( { 'prefix': '' }, options ); + return $(this) + .find( 'msg' ) + .each( function() { + $(this) + .text( mediaWiki.msg.call( mw, options.prefix + $(this).attr( 'key' ) ) ) + .replaceWith( $(this).html() ); + } ) + .end() + .find( '[title-msg]' ) + .each( function() { + $(this) + .attr( 'title', mw.msg.call( mw, options.prefix + $(this).attr( 'title-msg' ) ) ) + .removeAttr( 'title-msg' ); + } ) + .end() + .find( '[alt-msg]' ) + .each( function() { + $(this) + .attr( 'alt', mw.msg.call( mw, options.prefix + $(this).attr( 'alt-msg' ) ) ) + .removeAttr( 'alt-msg' ); + } ) + .end(); + }; +} )( jQuery, mediaWiki ); -- 2.20.1