2 * Localized Language support attempts to mirror some of the functionality of
3 * Language.php in MediaWiki.
4 * This adds methods for transforming message text.
11 * Process the PLURAL template substitution
13 * @param {object} template Template object
16 * 'title': [title of template],
17 * 'parameters': [template parameters]
19 * @example {{Template:title|params}}
21 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 var 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 count integer Non-localized quantifier
43 * @param forms array List of plural forms
44 * @return string Correct form for quantifier in this language
46 convertPlural: function ( count
, forms
) {
49 explicitPluralPattern
= new RegExp( '\\d+=', 'i' ),
55 if ( !forms
|| forms
.length
=== 0 ) {
59 // Handle for explicit n= forms
60 for ( index
= 0; index
< forms
.length
; index
++ ) {
62 if ( explicitPluralPattern
.test( form
) ) {
63 equalsPosition
= form
.indexOf( '=' );
64 formCount
= parseInt( form
.substring( 0, equalsPosition
), 10 );
65 if ( formCount
=== count
) {
66 return form
.substr( equalsPosition
+ 1 );
68 forms
[index
] = undefined;
72 // Remove explicit plural forms from the forms.
73 forms
= $.map( forms
, function ( form
) {
77 pluralRules
= mw
.language
.getData( mw
.config
.get( 'wgUserLanguage' ), 'pluralRules' );
80 return ( count
=== 1 ) ? forms
[0] : forms
[1];
82 pluralFormIndex
= mw
.cldr
.getPluralForm( count
, pluralRules
);
83 pluralFormIndex
= Math
.min( pluralFormIndex
, forms
.length
- 1 );
84 return forms
[pluralFormIndex
];
88 * Pads an array to a specific length by copying the last one element.
90 * @param forms array Number of forms given to convertPlural
91 * @param count integer Number of forms required
92 * @return array Padded array of forms
94 preConvertPlural: function ( forms
, count
) {
95 while ( forms
.length
< count
) {
96 forms
.push( forms
[ forms
.length
-1 ] );
102 * Provides an alternative text depending on specified gender.
103 * Usage {{gender:[gender|user object]|masculine|feminine|neutral}}.
104 * If second or third parameter are not specified, masculine is used.
106 * These details may be overriden per language.
108 * @param gender string male, female, or anything else for neutral.
109 * @param forms array List of gender forms
113 gender: function ( gender
, forms
) {
114 if ( !forms
|| forms
.length
=== 0 ) {
117 forms
= mw
.language
.preConvertPlural( forms
, 2 );
118 if ( gender
=== 'male' ) {
121 if ( gender
=== 'female' ) {
124 return ( forms
.length
=== 3 ) ? forms
[2] : forms
[0];
128 * Grammatical transformations, needed for inflected languages.
129 * Invoked by putting {{grammar:form|word}} in a message.
130 * The rules can be defined in $wgGrammarForms global or grammar
131 * forms can be computed dynamically by overriding this method per language
133 * @param word {String}
134 * @param form {String}
137 convertGrammar: function ( word
, form
) {
138 var grammarForms
= mw
.language
.getData( mw
.config
.get( 'wgUserLanguage' ), 'grammarForms' );
139 if ( grammarForms
&& grammarForms
[form
] ) {
140 return grammarForms
[form
][word
] || word
;
147 $.extend( mw
.language
, language
);
149 }( mediaWiki
, jQuery
) );