Merge "Form improvements when creating another's account"
[lhc/web/wiklou.git] / resources / mediawiki.language / mediawiki.language.js
1 /**
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.
5 */
6 ( function ( mw, $ ) {
7
8 var language = {
9
10 /**
11 * Process the PLURAL template substitution
12 *
13 * @param {object} template Template object
14 * @format template
15 * {
16 * 'title': [title of template],
17 * 'parameters': [template parameters]
18 * }
19 * @example {{Template:title|params}}
20 */
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 ) {
25 return '';
26 }
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 );
31 }
32 // Could not process plural return first form or nothing
33 if ( template.parameters[0] ) {
34 return template.parameters[0];
35 }
36 return '';
37 },
38
39 /**
40 * Plural form transformations, needed for some languages.
41 *
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
45 */
46 convertPlural: function ( count, forms ) {
47 var pluralRules,
48 formCount,
49 explicitPluralPattern = new RegExp( '\\d+=', 'i' ),
50 form,
51 index,
52 equalsPosition,
53 pluralFormIndex = 0;
54
55 if ( !forms || forms.length === 0 ) {
56 return '';
57 }
58
59 // Handle for explicit n= forms
60 for ( index = 0; index < forms.length; index++ ) {
61 form = forms[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 );
67 }
68 forms[index] = undefined;
69 }
70 }
71
72 // Remove explicit plural forms from the forms.
73 forms = $.map( forms, function ( form ) {
74 return form;
75 } );
76
77 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
78 if ( !pluralRules ) {
79 // default fallback.
80 return ( count === 1 ) ? forms[0] : forms[1];
81 }
82 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
83 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
84 return forms[pluralFormIndex];
85 },
86
87 /**
88 * Pads an array to a specific length by copying the last one element.
89 *
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
93 */
94 preConvertPlural: function ( forms, count ) {
95 while ( forms.length < count ) {
96 forms.push( forms[ forms.length-1 ] );
97 }
98 return forms;
99 },
100
101 /**
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.
105 *
106 * These details may be overriden per language.
107 *
108 * @param gender string male, female, or anything else for neutral.
109 * @param forms array List of gender forms
110 *
111 * @return string
112 */
113 gender: function ( gender, forms ) {
114 if ( !forms || forms.length === 0 ) {
115 return '';
116 }
117 forms = mw.language.preConvertPlural( forms, 2 );
118 if ( gender === 'male' ) {
119 return forms[0];
120 }
121 if ( gender === 'female' ) {
122 return forms[1];
123 }
124 return ( forms.length === 3 ) ? forms[2] : forms[0];
125 },
126
127 /**
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
132 *
133 * @param word {String}
134 * @param form {String}
135 * @return {String}
136 */
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;
141 }
142 return word;
143 }
144
145 };
146
147 $.extend( mw.language, language );
148
149 }( mediaWiki, jQuery ) );