Index: trunk/phase3/tests/qunit/index.html |
— | — | @@ -43,6 +43,7 @@ |
44 | 44 | <script src="../../resources/jquery/jquery.byteLength.js"></script> |
45 | 45 | <script src="../../resources/jquery/jquery.byteLimit.js"></script> |
46 | 46 | <script src="../../resources/jquery/jquery.colorUtil.js"></script> |
| 47 | + <script src="../../resources/jquery/jquery.localize.js"></script> |
47 | 48 | <script src="../../resources/jquery/jquery.tabIndex.js"></script> |
48 | 49 | <script src="../../resources/jquery/jquery.tablesorter.js"></script> |
49 | 50 | <script src="../../resources/mediawiki/mediawiki.Title.js"></script> |
— | — | @@ -67,6 +68,7 @@ |
68 | 69 | <script src="suites/resources/jquery/jquery.byteLength.js"></script> |
69 | 70 | <script src="suites/resources/jquery/jquery.byteLimit.js"></script> |
70 | 71 | <script src="suites/resources/jquery/jquery.colorUtil.js"></script> |
| 72 | + <script src="suites/resources/jquery/jquery.localize.js"></script> |
71 | 73 | <script src="suites/resources/jquery/jquery.tabIndex.js"></script> |
72 | 74 | <script src="suites/resources/jquery/jquery.tablesorter.test.js" charset="UTF-8"></script> |
73 | 75 | <script src="suites/resources/mediawiki/mediawiki.Title.js"></script> |
Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.localize.js |
— | — | @@ -0,0 +1,134 @@ |
| 2 | +module( 'jquery.localize.js' ); |
| 3 | + |
| 4 | +test( '-- Initial check', function() { |
| 5 | + expect(1); |
| 6 | + ok( $.fn.localize, 'jQuery.fn.localize defined' ); |
| 7 | +} ); |
| 8 | + |
| 9 | +test( 'Handle basic replacements', function() { |
| 10 | + expect(4); |
| 11 | + |
| 12 | + var html, $lc, expected; |
| 13 | + mw.messages.set( 'basic', 'Basic stuff' ); |
| 14 | + |
| 15 | + // Tag: html:msg |
| 16 | + html = '<div><span class="foobar"><html:msg key="basic"></span></div>'; |
| 17 | + $lc = $( html ).localize(); |
| 18 | + expected = '<span class="foobar">Basic stuff</span>'; |
| 19 | + |
| 20 | + strictEqual( $lc.html(), expected, 'Tag: html:msg' ); |
| 21 | + |
| 22 | + // Tag: msg (deprecated) |
| 23 | + html = '<div><span class="foobar"><msg key="basic"></span></div>'; |
| 24 | + $lc = $( html ).localize(); |
| 25 | + expected = '<span class="foobar">Basic stuff</span>'; |
| 26 | + |
| 27 | + strictEqual( $lc.html(), expected, 'Tag: msg' ); |
| 28 | + |
| 29 | + // Attribute: title-msg |
| 30 | + html = '<div><span class="foobar" title-msg="basic"></span></div>'; |
| 31 | + $lc = $( html ).localize(); |
| 32 | + expected = '<span class="foobar" title="Basic stuff"></span>'; |
| 33 | + |
| 34 | + strictEqual( $lc.html(), expected, 'Attribute: title-msg' ); |
| 35 | + |
| 36 | + // Attribute: alt-msg |
| 37 | + html = '<div><span class="foobar" alt-msg="basic"></span></div>'; |
| 38 | + $lc = $( html ).localize(); |
| 39 | + expected = '<span class="foobar" alt="Basic stuff"></span>'; |
| 40 | + |
| 41 | + strictEqual( $lc.html(), expected, 'Attribute: alt-msg' ); |
| 42 | +} ); |
| 43 | + |
| 44 | +test( 'Proper escaping', function() { |
| 45 | + expect(2); |
| 46 | + |
| 47 | + var html, $lc, expected; |
| 48 | + mw.messages.set( 'properfoo', '<proper esc="test">' ); |
| 49 | + |
| 50 | + // This is handled by jQuery inside $.fn.localize, just a simple sanity checked |
| 51 | + // making sure it is actually using text() and attr() (or something with the same effect) |
| 52 | + |
| 53 | + // Text escaping |
| 54 | + html = '<div><span class="foobar"><html:msg key="properfoo"></span></div>'; |
| 55 | + $lc = $( html ).localize(); |
| 56 | + expected = '<span class="foobar"><proper esc="test"></span>'; |
| 57 | + |
| 58 | + strictEqual( $lc.html(), expected, 'Content is inserted as text, not as html.' ); |
| 59 | + |
| 60 | + // Attribute escaping |
| 61 | + html = '<div><span class="foobar" title-msg="properbar"></span></div>'; |
| 62 | + $lc = $( html ).localize(); |
| 63 | + expected = '<span class="foobar" title="<properbar>"></span>'; |
| 64 | + |
| 65 | + strictEqual( $lc.html(), expected, 'Attributes are not inserted raw.' ); |
| 66 | + |
| 67 | + |
| 68 | +} ); |
| 69 | + |
| 70 | +test( 'Options', function() { |
| 71 | + expect(4); |
| 72 | + |
| 73 | + mw.messages.set( { |
| 74 | + 'foo-lorem': 'Lorem', |
| 75 | + 'foo-ipsum': 'Ipsum', |
| 76 | + 'foo-bar-title': 'Read more about bars', |
| 77 | + 'foo-bar-label': 'The Bars', |
| 78 | + 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)', |
| 79 | + 'foo-bazz-label': 'The Bazz ($1)', |
| 80 | + 'foo-welcome': 'Welcome to $1! (last visit: $2)' |
| 81 | + } ); |
| 82 | + var html, $lc, expected, x, sitename = 'Wikipedia'; |
| 83 | + |
| 84 | + // Message key prefix |
| 85 | + html = '<div><span title-msg="lorem"><html:msg key="ipsum" /></span></div>'; |
| 86 | + $lc = $( html ).localize( { |
| 87 | + prefix: 'foo-' |
| 88 | + } ); |
| 89 | + expected = '<span title="Lorem">Ipsum</span>'; |
| 90 | + |
| 91 | + strictEqual( $lc.html(), expected, 'Message key prefix' ); |
| 92 | + |
| 93 | + // Variable keys mapping |
| 94 | + x = 'bar'; |
| 95 | + html = '<div><span title-msg="title"><html:msg key="label" /></span></div>'; |
| 96 | + $lc = $( html ).localize( { |
| 97 | + keys: { |
| 98 | + 'title': 'foo-' + x + '-title', |
| 99 | + 'label': 'foo-' + x + '-label' |
| 100 | + } |
| 101 | + } ); |
| 102 | + expected = '<span title="Read more about bars">The Bars</span>'; |
| 103 | + |
| 104 | + strictEqual( $lc.html(), expected, 'Message prefix' ); |
| 105 | + |
| 106 | + // Passing parameteters to mw.msg |
| 107 | + html = '<div><span><html:msg key="foo-welcome" /></span></div>'; |
| 108 | + $lc = $( html ).localize( { |
| 109 | + params: { |
| 110 | + 'foo-welcome': [sitename, 'yesterday'] |
| 111 | + } |
| 112 | + } ); |
| 113 | + expected = '<span>Welcome to Wikipedia! (last visit: yesterday)</span>'; |
| 114 | + |
| 115 | + strictEqual( $lc.html(), expected, 'Message prefix' ); |
| 116 | + |
| 117 | + // Combination of options prefix, params and keys |
| 118 | + x = 'bazz'; |
| 119 | + html = '<div><span title-msg="title"><html:msg key="label" /></span></div>'; |
| 120 | + $lc = $( html ).localize( { |
| 121 | + prefix: 'foo-', |
| 122 | + keys: { |
| 123 | + 'title': x + '-title', |
| 124 | + 'label': x + '-label' |
| 125 | + }, |
| 126 | + params: { |
| 127 | + 'title': [sitename, '3 minutes ago'], |
| 128 | + 'label': [sitename, '3 minutes ago'] |
| 129 | + |
| 130 | + } |
| 131 | + } ); |
| 132 | + expected = '<span title="Read more about bazz at Wikipedia (last modified: 3 minutes ago)">The Bazz (Wikipedia)</span>'; |
| 133 | + |
| 134 | + strictEqual( $lc.html(), expected, 'Message prefix' ); |
| 135 | +} ); |
Property changes on: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.localize.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 136 | + native |
Index: trunk/phase3/resources/jquery/jquery.localize.js |
— | — | @@ -34,7 +34,10 @@ |
35 | 35 | $.fn.localize = function( options ) { |
36 | 36 | options = $.extend( { 'prefix': '', 'keys': {}, 'params': {} }, options ); |
37 | 37 | function msg( key ) { |
38 | | - return mw.msg( options.prefix + ( key in options.keys ? options.keys[key] : key ), ( key in options.params ? options.params[key] : [] ) ) |
| 38 | + var args = key in options.params ? options.params[key] : []; |
| 39 | + // Format: mw.msg( key [, p1, p2, ...] ) |
| 40 | + args.unshift( options.prefix + ( key in options.keys ? options.keys[key] : key ) ); |
| 41 | + return mw.msg.apply( mw, args ); |
39 | 42 | }; |
40 | 43 | return $(this) |
41 | 44 | .find( 'msg,html\\:msg' ) |