2 * Methods for transforming message syntax.
9 $.extend( mw.language, {
12 * Process the PLURAL template substitution
15 * @param {Object} template Template object
16 * @param {string} template.title
17 * @param {Array} template.parameters
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 ) {
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 );
31 // Could not process plural return first form or nothing
32 if ( template.parameters[ 0 ] ) {
33 return template.parameters[ 0 ];
39 * Plural form transformations, needed for some languages.
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
46 convertPlural: function ( count, forms, explicitPluralForms ) {
50 if ( explicitPluralForms && explicitPluralForms[ count ] ) {
51 return explicitPluralForms[ count ];
54 if ( !forms || forms.length === 0 ) {
58 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
61 return ( count === 1 ) ? forms[ 0 ] : forms[ 1 ];
63 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
64 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
65 return forms[ pluralFormIndex ];
69 * Pads an array to a specific length by copying the last one element.
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
76 preConvertPlural: function ( forms, count ) {
77 while ( forms.length < count ) {
78 forms.push( forms[ forms.length - 1 ] );
84 * Provides an alternative text depending on specified gender.
86 * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
87 * If second or third parameter are not specified, masculine is used.
89 * These details may be overridden per language.
91 * @param {string} gender 'male', 'female', or anything else for neutral.
92 * @param {Array} forms List of gender forms
95 gender: function ( gender, forms ) {
96 if ( !forms || forms.length === 0 ) {
99 forms = mw.language.preConvertPlural( forms, 2 );
100 if ( gender === 'male' ) {
103 if ( gender === 'female' ) {
106 return ( forms.length === 3 ) ? forms[ 2 ] : forms[ 0 ];
110 * Grammatical transformations, needed for inflected languages.
111 * Invoked by putting `{{grammar:form|word}}` in a message.
113 * The rules can be defined in $wgGrammarForms global or computed
114 * dynamically by overriding this method per language.
116 * @param {string} word
117 * @param {string} form
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;
129 * Turn a list of string into a simple list using commas and 'and'.
131 * See Language::listToText in languages/Language.php
133 * @param {string[]} list
136 listToText: function ( list ) {
140 for ( ; i < list.length; i++ ) {
142 if ( list.length - 2 === i ) {
143 text += mw.msg( 'and' ) + mw.msg( 'word-separator' );
144 } else if ( list.length - 1 !== i ) {
145 text += mw.msg( 'comma-separator' );
151 setSpecialCharacters: function ( data ) {
152 this.specialCharacters = data;
156 }( mediaWiki, jQuery ) );