Merge "API: Recognize an "Api-User-Agent" header"
[mediawiki.git] / resources / src / mediawiki.language / mediawiki.language.js
blobdceae1178a3044741ffc0b5f325067a3b704e5cc
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                 if ( template.title && template.parameters && mw.language.convertPlural ) {
22                         // Check if we have forms to replace
23                         if ( template.parameters.length === 0 ) {
24                                 return '';
25                         }
26                         // Restore the count into a Number ( if it got converted earlier )
27                         var count = mw.language.convertNumber( template.title, true );
28                         // Do convertPlural call
29                         return mw.language.convertPlural( parseInt( count, 10 ), template.parameters );
30                 }
31                 // Could not process plural return first form or nothing
32                 if ( template.parameters[0] ) {
33                         return template.parameters[0];
34                 }
35                 return '';
36         },
38         /**
39          * Plural form transformations, needed for some languages.
40          *
41          * @param {number} count Non-localized quantifier
42          * @param {Array} forms List of plural forms
43          * @param {Object} [explicitPluralForms] List of explicit plural forms
44          * @return {string} Correct form for quantifier in this language
45          */
46         convertPlural: function ( count, forms, explicitPluralForms ) {
47                 var pluralRules,
48                         pluralFormIndex = 0;
50                 if ( explicitPluralForms && explicitPluralForms[count] ) {
51                         return explicitPluralForms[count];
52                 }
54                 if ( !forms || forms.length === 0 ) {
55                         return '';
56                 }
58                 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
59                 if ( !pluralRules ) {
60                         // default fallback.
61                         return ( count === 1 ) ? forms[0] : forms[1];
62                 }
63                 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
64                 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
65                 return forms[pluralFormIndex];
66         },
68         /**
69          * Pads an array to a specific length by copying the last one element.
70          *
71          * @private
72          * @param {Array} forms Number of forms given to convertPlural
73          * @param {number} count Number of forms required
74          * @return {Array} Padded array of forms
75          */
76         preConvertPlural: function ( forms, count ) {
77                 while ( forms.length < count ) {
78                         forms.push( forms[ forms.length - 1 ] );
79                 }
80                 return forms;
81         },
83         /**
84          * Provides an alternative text depending on specified gender.
85          *
86          * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
87          * If second or third parameter are not specified, masculine is used.
88          *
89          * These details may be overriden per language.
90          *
91          * @param {string} gender 'male', 'female', or anything else for neutral.
92          * @param {Array} forms List of gender forms
93          * @return string
94          */
95         gender: function ( gender, forms ) {
96                 if ( !forms || forms.length === 0 ) {
97                         return '';
98                 }
99                 forms = mw.language.preConvertPlural( forms, 2 );
100                 if ( gender === 'male' ) {
101                         return forms[0];
102                 }
103                 if ( gender === 'female' ) {
104                         return forms[1];
105                 }
106                 return ( forms.length === 3 ) ? forms[2] : forms[0];
107         },
109         /**
110          * Grammatical transformations, needed for inflected languages.
111          * Invoked by putting `{{grammar:form|word}}` in a message.
112          *
113          * The rules can be defined in $wgGrammarForms global or computed
114          * dynamically by overriding this method per language.
115          *
116          * @param {string} word
117          * @param {string} form
118          * @return {string}
119          */
120         convertGrammar: function ( word, form ) {
121                 var grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
122                 if ( grammarForms && grammarForms[form] ) {
123                         return grammarForms[form][word] || word;
124                 }
125                 return word;
126         },
128         /**
129          * Turn a list of string into a simple list using commas and 'and'.
130          *
131          * See Language::listToText in languages/Language.php
132          *
133          * @param {string[]} list
134          * @return {string}
135          */
136         listToText: function ( list ) {
137                 var text = '', i = 0;
138                 for ( ; i < list.length; i++ ) {
139                         text += list[i];
140                         if ( list.length - 2 === i ) {
141                                 text += mw.msg( 'and' ) + mw.msg( 'word-separator' );
142                         } else if ( list.length - 1 !== i ) {
143                                 text += mw.msg( 'comma-separator' );
144                         }
145                 }
146                 return text;
147         }
148 } );
150 }( mediaWiki, jQuery ) );