Fix exception thrown in mw.util.addPortletLink
[mediawiki.git] / resources / mediawiki.language / mediawiki.language.js
blobfa7aa8d506f490439c81b194c19869119b473803
1 /**
2  * Base language object
3  *
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.
7  */
8 ( function( $, mw ) {
10 mw.language = {
11         /**
12          * Process the PLURAL template substitution
13          *
14          * @param {object} template Template object
15          * @format template
16          *      {
17          *              'title': [title of template],
18          *              'parameters': [template parameters]
19          *      }
20          * @example {{Template:title|params}}
21          */
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 ) {
26                                 return '';
27                         }
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 );
32                 }
33                 // Could not process plural return first form or nothing
34                 if ( template.parameters[0] ) {
35                         return template.parameters[0];
36                 }
37                 return '';
38         },
39         /**
40          * Plural form transformations, needed for some languages.
41          *
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
45          */
46         'convertPlural': function( count, forms ){
47                 if ( !forms || forms.length == 0 ) {
48                         return '';
49                 }
50                 return ( parseInt( count, 10 ) == 1 ) ? forms[0] : forms[1];
51         },
52         /**
53          * Pads an array to a specific length by copying the last one element.
54          *
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
58          */
59         'preConvertPlural': function( forms, count ) {
60                 while ( forms.length < count ) {
61                         forms.push( forms[ forms.length-1 ] );
62                 }
63                 return forms;
64         },
65         /**
66          * Converts a number using digitTransformTable.
67          *
68          * @param {num} number Value to be converted
69          * @param {boolean} integer Convert the return value to an integer
70          */
71         'convertNumber': function( num, integer ) {
72                 if ( !mw.language.digitTransformTable ) {
73                         return num;
74                 }
75                 // Set the target Transform table:
76                 var transformTable = mw.language.digitTransformTable;
77                 // Check if the "restore" to Latin number flag is set:
78                 if ( integer ) {
79                         if ( parseInt( num, 10 ) == num ) {
80                                 return num;
81                         }
82                         var tmp = [];
83                         for ( var i in transformTable ) {
84                                 tmp[ transformTable[ i ] ] = i;
85                         }
86                         transformTable = tmp;
87                 }
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]];
93                         } else {
94                                 convertedNumber += numberString[i];
95                         }
96                 }
97                 return integer ? parseInt( convertedNumber, 10 ) : convertedNumber;
98         },
99         // Digit Transform Table, populated by language classes where applicable
100         'digitTransformTable': null
102 } )( jQuery, mediaWiki );