9c1d00e15549ae83e1e46dc29e23eb1fbe3a7be3
[lhc/web/wiklou.git] / resources / src / mediawiki.language / mediawiki.language.js
1 /*
2 * Methods for transforming message syntax.
3 */
4 ( function () {
5
6 /**
7 * @class mw.language
8 */
9 $.extend( mw.language, {
10
11 /**
12 * Process the PLURAL template substitution
13 *
14 * @private
15 * @param {Object} template Template object
16 * @param {string} template.title
17 * @param {Array} template.parameters
18 * @return {string}
19 */
20 procPLURAL: function ( template ) {
21 var count;
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 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 {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
46 */
47 convertPlural: function ( count, forms, explicitPluralForms ) {
48 var pluralRules,
49 pluralFormIndex = 0;
50
51 if ( explicitPluralForms && ( explicitPluralForms[ count ] !== undefined ) ) {
52 return explicitPluralForms[ count ];
53 }
54
55 if ( !forms || forms.length === 0 ) {
56 return '';
57 }
58
59 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
60 if ( !pluralRules ) {
61 // default fallback.
62 return ( count === 1 ) ? forms[ 0 ] : forms[ 1 ];
63 }
64 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
65 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
66 return forms[ pluralFormIndex ];
67 },
68
69 /**
70 * Pads an array to a specific length by copying the last one element.
71 *
72 * @private
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
76 */
77 preConvertPlural: function ( forms, count ) {
78 while ( forms.length < count ) {
79 forms.push( forms[ forms.length - 1 ] );
80 }
81 return forms;
82 },
83
84 /**
85 * Provides an alternative text depending on specified gender.
86 *
87 * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
88 * If second or third parameter are not specified, masculine is used.
89 *
90 * These details may be overridden per language.
91 *
92 * @param {string} gender 'male', 'female', or anything else for neutral.
93 * @param {Array} forms List of gender forms
94 * @return {string}
95 */
96 gender: function ( gender, forms ) {
97 if ( !forms || forms.length === 0 ) {
98 return '';
99 }
100 forms = mw.language.preConvertPlural( forms, 2 );
101 if ( gender === 'male' ) {
102 return forms[ 0 ];
103 }
104 if ( gender === 'female' ) {
105 return forms[ 1 ];
106 }
107 return ( forms.length === 3 ) ? forms[ 2 ] : forms[ 0 ];
108 },
109
110 /**
111 * Grammatical transformations, needed for inflected languages.
112 * Invoked by putting `{{grammar:case|word}}` in a message.
113 *
114 * The rules can be defined in $wgGrammarForms global or computed
115 * dynamically by overriding this method per language.
116 *
117 * @param {string} word
118 * @param {string} form
119 * @return {string}
120 */
121 convertGrammar: function ( word, form ) {
122 var userLanguage, forms, transformations,
123 patterns, i, rule, sourcePattern, regexp, replacement;
124
125 userLanguage = mw.config.get( 'wgUserLanguage' );
126
127 forms = mw.language.getData( userLanguage, 'grammarForms' );
128 if ( forms && forms[ form ] ) {
129 return forms[ form ][ word ];
130 }
131
132 transformations = mw.language.getData( userLanguage, 'grammarTransformations' );
133
134 if ( !( transformations && transformations[ form ] ) ) {
135 return word;
136 }
137
138 patterns = transformations[ form ];
139
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 ];
145 }
146
147 for ( i = 0; i < patterns.length; i++ ) {
148 rule = patterns[ i ];
149 sourcePattern = rule[ 0 ];
150
151 if ( sourcePattern === '@metadata' ) {
152 continue;
153 }
154
155 regexp = new RegExp( sourcePattern );
156 replacement = rule[ 1 ];
157
158 if ( word.match( regexp ) ) {
159 return word.replace( regexp, replacement );
160 }
161 }
162
163 return word;
164 },
165
166 /**
167 * Turn a list of string into a simple list using commas and 'and'.
168 *
169 * See Language::listToText in languages/Language.php
170 *
171 * @param {string[]} list
172 * @return {string}
173 */
174 listToText: function ( list ) {
175 var text = '',
176 i = 0;
177
178 for ( ; i < list.length; i++ ) {
179 text += list[ 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' );
184 }
185 }
186 return text;
187 },
188
189 setSpecialCharacters: function ( data ) {
190 this.specialCharacters = data;
191 },
192
193 /**
194 * Formats language tags according the BCP47 standard.
195 * See LanguageCode::bcp47 for the PHP implementation.
196 *
197 * @param {string} languageTag Well-formed language tag
198 * @return {string}
199 */
200 bcp47: function ( languageTag ) {
201 var formatted,
202 isFirstSegment = true,
203 isPrivate = false,
204 segments = languageTag.split( '-' );
205
206 formatted = segments.map( function ( segment ) {
207 var newSegment;
208
209 // when previous segment is x, it is a private segment and should be lc
210 if ( isPrivate ) {
211 newSegment = segment.toLowerCase();
212 // ISO 3166 country code
213 } else if ( segment.length === 2 && !isFirstSegment ) {
214 newSegment = segment.toUpperCase();
215 // ISO 15924 script code
216 } else if ( segment.length === 4 && !isFirstSegment ) {
217 newSegment = segment.charAt( 0 ).toUpperCase() + segment.substring( 1 ).toLowerCase();
218 // Use lowercase for other cases
219 } else {
220 newSegment = segment.toLowerCase();
221 }
222
223 isPrivate = segment.toLowerCase() === 'x';
224 isFirstSegment = false;
225
226 return newSegment;
227 } );
228
229 return formatted.join( '-' );
230 }
231 } );
232
233 }() );