Merge "Change CdbException to subclass Exception"
[mediawiki.git] / resources / mediawiki.language / mediawiki.language.js
blob631d13dfb4a75404e12d128c5903b67b2567cbc9
1 /**
2  * Localized Language support attempts to mirror some of the functionality of
3  * Language.php in MediaWiki.
4  * This adds methods for transforming message text.
5  */
6 ( function ( mw, $ ) {
8 var language = {
10         /**
11          * Process the PLURAL template substitution
12          *
13          * @param {object} template Template object
14          * @format template
15          *  {
16          *      'title': [title of template],
17          *      'parameters': [template parameters]
18          *  }
19          * @example {{Template:title|params}}
20          */
21         procPLURAL: function ( template ) {
22                 if ( template.title && template.parameters && mw.language.convertPlural ) {
23                         // Check if we have forms to replace
24                         if ( template.parameters.length === 0 ) {
25                                 return '';
26                         }
27                         // Restore the count into a Number ( if it got converted earlier )
28                         var count = mw.language.convertNumber( template.title, true );
29                         // Do convertPlural call
30                         return mw.language.convertPlural( parseInt( count, 10 ), template.parameters );
31                 }
32                 // Could not process plural return first form or nothing
33                 if ( template.parameters[0] ) {
34                         return template.parameters[0];
35                 }
36                 return '';
37         },
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                 var pluralRules,
48                         formCount,
49                         form,
50                         index,
51                         equalsPosition,
52                         pluralFormIndex = 0;
54                 if ( !forms || forms.length === 0 ) {
55                         return '';
56                 }
58                 // Handle for explicit n= forms
59                 for ( index = 0; index < forms.length; index++ ) {
60                         form = forms[index];
61                         if ( /^\d+=/.test( form ) ) {
62                                 equalsPosition = form.indexOf( '=' );
63                                 formCount = parseInt( form.substring( 0, equalsPosition ), 10 );
64                                 if ( formCount === count ) {
65                                         return form.substr( equalsPosition + 1 );
66                                 }
67                                 forms[index] = undefined;
68                         }
69                 }
71                 // Remove explicit plural forms from the forms.
72                 forms = $.map( forms, function ( form ) {
73                         return form;
74                 } );
76                 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
77                 if ( !pluralRules ) {
78                         // default fallback.
79                         return ( count === 1 ) ? forms[0] : forms[1];
80                 }
81                 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
82                 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
83                 return forms[pluralFormIndex];
84         },
86         /**
87          * Pads an array to a specific length by copying the last one element.
88          *
89          * @param forms array Number of forms given to convertPlural
90          * @param count integer Number of forms required
91          * @return array Padded array of forms
92          */
93         preConvertPlural: function ( forms, count ) {
94                 while ( forms.length < count ) {
95                         forms.push( forms[ forms.length-1 ] );
96                 }
97                 return forms;
98         },
100         /**
101          * Provides an alternative text depending on specified gender.
102          * Usage {{gender:[gender|user object]|masculine|feminine|neutral}}.
103          * If second or third parameter are not specified, masculine is used.
104          *
105          * These details may be overriden per language.
106          *
107          * @param gender string male, female, or anything else for neutral.
108          * @param forms array List of gender forms
109          *
110          * @return string
111          */
112         gender: function ( gender, forms ) {
113                 if ( !forms || forms.length === 0 ) {
114                         return '';
115                 }
116                 forms = mw.language.preConvertPlural( forms, 2 );
117                 if ( gender === 'male' ) {
118                         return forms[0];
119                 }
120                 if ( gender === 'female' ) {
121                         return forms[1];
122                 }
123                 return ( forms.length === 3 ) ? forms[2] : forms[0];
124         },
126         /**
127          * Grammatical transformations, needed for inflected languages.
128          * Invoked by putting {{grammar:form|word}} in a message.
129          * The rules can be defined in $wgGrammarForms global or grammar
130          * forms can be computed dynamically by overriding this method per language
131          *
132          * @param word {String}
133          * @param form {String}
134          * @return {String}
135          */
136         convertGrammar: function ( word, form ) {
137                 var grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
138                 if ( grammarForms && grammarForms[form] ) {
139                         return grammarForms[form][word] || word;
140                 }
141                 return word;
142         }
146 $.extend( mw.language, language );
148 }( mediaWiki, jQuery ) );