4 * Localized Language support attempts to mirror some of the functionality of
5 * Language.php in MediaWiki. This object contains methods for loading and
6 * transforming message text.
12 * Process the PLURAL template substitution
14 * @param {object} template Template object
17 * 'title': [title of template],
18 * 'parameters': [template parameters]
20 * @example {{Template:title|params}}
22 'procPLURAL': function( template ) {
23 if ( template.title && template.parameters && mw.language.convertPlural ) {
24 // Check if we have forms to replace
25 if ( template.parameters.length == 0 ) {
28 // Restore the count into a Number ( if it got converted earlier )
29 var count = mw.language.convertNumber( template.title, true );
30 // Do convertPlural call
31 return mw.language.convertPlural( parseInt( count, 10 ), template.parameters );
33 // Could not process plural return first form or nothing
34 if ( template.parameters[0] ) {
35 return template.parameters[0];
40 * Plural form transformations, needed for some languages.
42 * @param count integer Non-localized quantifier
43 * @param forms array List of plural forms
44 * @return string Correct form for quantifier in this language
46 'convertPlural': function( count, forms ){
47 if ( !forms || forms.length == 0 ) {
50 return ( parseInt( count, 10 ) == 1 ) ? forms[0] : forms[1];
53 * Pads an array to a specific length by copying the last one element.
55 * @param forms array Number of forms given to convertPlural
56 * @param count integer Number of forms required
57 * @return array Padded array of forms
59 'preConvertPlural': function( forms, count ) {
60 while ( forms.length < count ) {
61 forms.push( forms[ forms.length-1 ] );
66 * Converts a number using digitTransformTable.
68 * @param {num} number Value to be converted
69 * @param {boolean} integer Convert the return value to an integer
71 'convertNumber': function( num, integer ) {
72 if ( !mw.language.digitTransformTable ) {
75 // Set the target Transform table:
76 var transformTable = mw.language.digitTransformTable;
77 // Check if the "restore" to Latin number flag is set:
79 if ( parseInt( num, 10 ) == num ) {
83 for ( var i in transformTable ) {
84 tmp[ transformTable[ i ] ] = i;
88 var numberString = '' + num;
89 var convertedNumber = '';
90 for ( var i = 0; i < numberString.length; i++ ) {
91 if ( transformTable[ numberString[i] ] ) {
92 convertedNumber += transformTable[numberString[i]];
94 convertedNumber += numberString[i];
97 return integer ? parseInt( convertedNumber, 10 ) : convertedNumber;
99 // Digit Transform Table, populated by language classes where applicable
100 'digitTransformTable': null
102 } )( jQuery, mediaWiki );