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 * @return {string} Correct form for quantifier in this language
45 convertPlural: function ( count
, forms
) {
53 if ( !forms
|| forms
.length
=== 0 ) {
57 // Handle for explicit n= forms
58 for ( index
= 0; index
< forms
.length
; index
++ ) {
60 if ( /^\d+=/.test( form
) ) {
61 equalsPosition
= form
.indexOf( '=' );
62 formCount
= parseInt( form
.substring( 0, equalsPosition
), 10 );
63 if ( formCount
=== count
) {
64 return form
.substr( equalsPosition
+ 1 );
66 forms
[index
] = undefined;
70 // Remove explicit plural forms from the forms.
71 forms
= $.map( forms
, function ( form
) {
75 if ( forms
.length
=== 0 ) {
79 pluralRules
= mw
.language
.getData( mw
.config
.get( 'wgUserLanguage' ), 'pluralRules' );
82 return ( count
=== 1 ) ? forms
[0] : forms
[1];
84 pluralFormIndex
= mw
.cldr
.getPluralForm( count
, pluralRules
);
85 pluralFormIndex
= Math
.min( pluralFormIndex
, forms
.length
- 1 );
86 return forms
[pluralFormIndex
];
90 * Pads an array to a specific length by copying the last one element.
93 * @param {Array} forms Number of forms given to convertPlural
94 * @param {number} count Number of forms required
95 * @return {Array} Padded array of forms
97 preConvertPlural: function ( forms
, count
) {
98 while ( forms
.length
< count
) {
99 forms
.push( forms
[ forms
.length
- 1 ] );
105 * Provides an alternative text depending on specified gender.
107 * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
108 * If second or third parameter are not specified, masculine is used.
110 * These details may be overriden per language.
112 * @param {string} gender 'male', 'female', or anything else for neutral.
113 * @param {Array} forms List of gender forms
116 gender: function ( gender
, forms
) {
117 if ( !forms
|| forms
.length
=== 0 ) {
120 forms
= mw
.language
.preConvertPlural( forms
, 2 );
121 if ( gender
=== 'male' ) {
124 if ( gender
=== 'female' ) {
127 return ( forms
.length
=== 3 ) ? forms
[2] : forms
[0];
131 * Grammatical transformations, needed for inflected languages.
132 * Invoked by putting `{{grammar:form|word}}` in a message.
134 * The rules can be defined in $wgGrammarForms global or computed
135 * dynamically by overriding this method per language.
137 * @param {string} word
138 * @param {string} form
141 convertGrammar: function ( word
, form
) {
142 var grammarForms
= mw
.language
.getData( mw
.config
.get( 'wgUserLanguage' ), 'grammarForms' );
143 if ( grammarForms
&& grammarForms
[form
] ) {
144 return grammarForms
[form
][word
] || word
;
151 }( mediaWiki
, jQuery
) );