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 ) {
22 if ( template.title && template.parameters && mw.language.convertPlural ) {
23 // Check if we have forms to replace
24 if ( template.parameters.length === 0 ) {
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 );
32 // Could not process plural return first form or nothing
33 if ( template.parameters[ 0 ] ) {
34 return template.parameters[ 0 ];
40 * Plural form transformations, needed for some languages.
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
47 convertPlural: function ( count, forms, explicitPluralForms ) {
51 if ( explicitPluralForms && ( explicitPluralForms[ count ] !== undefined ) ) {
52 return explicitPluralForms[ count ];
55 if ( !forms || forms.length === 0 ) {
59 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
62 return ( count === 1 ) ? forms[ 0 ] : forms[ 1 ];
64 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
65 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
66 return forms[ pluralFormIndex ];
70 * Pads an array to a specific length by copying the last one element.
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
77 preConvertPlural: function ( forms, count ) {
78 while ( forms.length < count ) {
79 forms.push( forms[ forms.length - 1 ] );
85 * Provides an alternative text depending on specified gender.
87 * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
88 * If second or third parameter are not specified, masculine is used.
90 * These details may be overridden per language.
92 * @param {string} gender 'male', 'female', or anything else for neutral.
93 * @param {Array} forms List of gender forms
96 gender: function ( gender, forms ) {
97 if ( !forms || forms.length === 0 ) {
100 forms = mw.language.preConvertPlural( forms, 2 );
101 if ( gender === 'male' ) {
104 if ( gender === 'female' ) {
107 return ( forms.length === 3 ) ? forms[ 2 ] : forms[ 0 ];
111 * Grammatical transformations, needed for inflected languages.
112 * Invoked by putting `{{grammar:case|word}}` in a message.
114 * The rules can be defined in $wgGrammarForms global or computed
115 * dynamically by overriding this method per language.
117 * @param {string} word
118 * @param {string} form
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 ];
132 transformations = mediaWiki.language.getData( userLanguage, 'grammarTransformations' );
134 if ( !( transformations && transformations[ form ] ) ) {
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 ];
147 for ( i = 0; i < patterns.length; i++ ) {
148 rule = patterns[ i ];
149 sourcePattern = rule[ 0 ];
151 if ( sourcePattern === '@metadata' ) {
155 regexp = new RegExp( sourcePattern );
156 replacement = rule[ 1 ];
158 if ( word.match( regexp ) ) {
159 return word.replace( regexp, replacement );
167 * Turn a list of string into a simple list using commas and 'and'.
169 * See Language::listToText in languages/Language.php
171 * @param {string[]} list
174 listToText: function ( list ) {
178 for ( ; i < list.length; 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' );
189 setSpecialCharacters: function ( data ) {
190 this.specialCharacters = data;
194 }( mediaWiki, jQuery ) );