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
) {
50 if ( !forms
|| forms
.length
=== 0 ) {
53 pluralRules
= mw
.language
.getData( mw
.config
.get( 'wgUserLanguage' ), 'pluralRules' );
56 return ( count
=== 1 ) ? forms
[0] : forms
[1];
58 pluralFormIndex
= mw
.cldr
.getPluralForm( count
, pluralRules
);
59 pluralFormIndex
= Math
.min( pluralFormIndex
, forms
.length
- 1 );
60 return forms
[pluralFormIndex
];
64 * Pads an array to a specific length by copying the last one element.
66 * @param forms array Number of forms given to convertPlural
67 * @param count integer Number of forms required
68 * @return array Padded array of forms
70 preConvertPlural: function ( forms
, count
) {
71 while ( forms
.length
< count
) {
72 forms
.push( forms
[ forms
.length
-1 ] );
78 * Provides an alternative text depending on specified gender.
79 * Usage {{gender:[gender|user object]|masculine|feminine|neutral}}.
80 * If second or third parameter are not specified, masculine is used.
82 * These details may be overriden per language.
84 * @param gender string male, female, or anything else for neutral.
85 * @param forms array List of gender forms
89 gender: function ( gender
, forms
) {
90 if ( !forms
|| forms
.length
=== 0 ) {
93 forms
= mw
.language
.preConvertPlural( forms
, 2 );
94 if ( gender
=== 'male' ) {
97 if ( gender
=== 'female' ) {
100 return ( forms
.length
=== 3 ) ? forms
[2] : forms
[0];
104 * Grammatical transformations, needed for inflected languages.
105 * Invoked by putting {{grammar:form|word}} in a message.
106 * The rules can be defined in $wgGrammarForms global or grammar
107 * forms can be computed dynamically by overriding this method per language
109 * @param word {String}
110 * @param form {String}
113 convertGrammar: function ( word
, form
) {
114 var grammarForms
= mw
.language
.getData( mw
.config
.get( 'wgUserLanguage' ), 'grammarForms' );
115 if ( grammarForms
&& grammarForms
[form
] ) {
116 return grammarForms
[form
][word
] || word
;
123 $.extend( mw
.language
, language
);
125 }( mediaWiki
, jQuery
) );