Avoid pointless use of isset() in LBFactoryMulti()
[mediawiki.git] / resources / src / mediawiki.language / mediawiki.language.js
blob3726a685273698219611c07bd363259107aaaae3
1 /*
2  * Methods for transforming message syntax.
3  */
4 ( function ( mw, $ ) {
6         /**
7          * @class mw.language
8          */
9         $.extend( mw.language, {
11                 /**
12                  * Process the PLURAL template substitution
13                  *
14                  * @private
15                  * @param {Object} template Template object
16                  * @param {string} template.title
17                  * @param {Array} template.parameters
18                  * @return {string}
19                  */
20                 procPLURAL: function ( template ) {
21                         var count;
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                                 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 {number} count Non-localized quantifier
43                  * @param {Array} forms List of plural forms
44                  * @param {Object} [explicitPluralForms] List of explicit plural forms
45                  * @return {string} Correct form for quantifier in this language
46                  */
47                 convertPlural: function ( count, forms, explicitPluralForms ) {
48                         var pluralRules,
49                                 pluralFormIndex = 0;
51                         if ( explicitPluralForms && ( explicitPluralForms[ count ] !== undefined ) ) {
52                                 return explicitPluralForms[ count ];
53                         }
55                         if ( !forms || forms.length === 0 ) {
56                                 return '';
57                         }
59                         pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
60                         if ( !pluralRules ) {
61                                 // default fallback.
62                                 return ( count === 1 ) ? forms[ 0 ] : forms[ 1 ];
63                         }
64                         pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
65                         pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
66                         return forms[ pluralFormIndex ];
67                 },
69                 /**
70                  * Pads an array to a specific length by copying the last one element.
71                  *
72                  * @private
73                  * @param {Array} forms Number of forms given to convertPlural
74                  * @param {number} count Number of forms required
75                  * @return {Array} Padded array of forms
76                  */
77                 preConvertPlural: function ( forms, count ) {
78                         while ( forms.length < count ) {
79                                 forms.push( forms[ forms.length - 1 ] );
80                         }
81                         return forms;
82                 },
84                 /**
85                  * Provides an alternative text depending on specified gender.
86                  *
87                  * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
88                  * If second or third parameter are not specified, masculine is used.
89                  *
90                  * These details may be overridden per language.
91                  *
92                  * @param {string} gender 'male', 'female', or anything else for neutral.
93                  * @param {Array} forms List of gender forms
94                  * @return {string}
95                  */
96                 gender: function ( gender, forms ) {
97                         if ( !forms || forms.length === 0 ) {
98                                 return '';
99                         }
100                         forms = mw.language.preConvertPlural( forms, 2 );
101                         if ( gender === 'male' ) {
102                                 return forms[ 0 ];
103                         }
104                         if ( gender === 'female' ) {
105                                 return forms[ 1 ];
106                         }
107                         return ( forms.length === 3 ) ? forms[ 2 ] : forms[ 0 ];
108                 },
110                 /**
111                  * Grammatical transformations, needed for inflected languages.
112                  * Invoked by putting `{{grammar:case|word}}` in a message.
113                  *
114                  * The rules can be defined in $wgGrammarForms global or computed
115                  * dynamically by overriding this method per language.
116                  *
117                  * @param {string} word
118                  * @param {string} form
119                  * @return {string}
120                  */
121                 convertGrammar: function ( word, form ) {
122                         var userLanguage, forms, transformations,
123                                 patterns, i, rule, sourcePattern, regexp, replacement;
125                         userLanguage = mw.config.get( 'wgUserLanguage' );
127                         forms = mw.language.getData( userLanguage, 'grammarForms' );
128                         if ( forms && forms[ form ] ) {
129                                 return forms[ form ][ word ];
130                         }
132                         transformations = mediaWiki.language.getData( userLanguage, 'grammarTransformations' );
134                         if ( !( transformations && transformations[ form ] ) ) {
135                                 return word;
136                         }
138                         patterns = transformations[ form ];
140                         // Some names of grammar rules are aliases for other rules.
141                         // In such cases the value is a string rather than object,
142                         // so load the actual rules.
143                         if ( typeof patterns === 'string' ) {
144                                 patterns = transformations[ patterns ];
145                         }
147                         for ( i = 0; i < patterns.length; i++ ) {
148                                 rule = patterns[ i ];
149                                 sourcePattern = rule[ 0 ];
151                                 if ( sourcePattern === '@metadata' ) {
152                                         continue;
153                                 }
155                                 regexp = new RegExp( sourcePattern );
156                                 replacement = rule[ 1 ];
158                                 if ( word.match( regexp ) ) {
159                                         return word.replace( regexp, replacement );
160                                 }
161                         }
163                         return word;
164                 },
166                 /**
167                  * Turn a list of string into a simple list using commas and 'and'.
168                  *
169                  * See Language::listToText in languages/Language.php
170                  *
171                  * @param {string[]} list
172                  * @return {string}
173                  */
174                 listToText: function ( list ) {
175                         var text = '',
176                                 i = 0;
178                         for ( ; i < list.length; i++ ) {
179                                 text += list[ i ];
180                                 if ( list.length - 2 === i ) {
181                                         text += mw.msg( 'and' ) + mw.msg( 'word-separator' );
182                                 } else if ( list.length - 1 !== i ) {
183                                         text += mw.msg( 'comma-separator' );
184                                 }
185                         }
186                         return text;
187                 },
189                 setSpecialCharacters: function ( data ) {
190                         this.specialCharacters = data;
191                 }
192         } );
194 }( mediaWiki, jQuery ) );