Merge "Remove last remnants of pre-1.16 live preview"
[lhc/web/wiklou.git] / resources / src / mediawiki.language / mediawiki.language.js
1 /*
2 * Methods for transforming message syntax.
3 */
4 ( function ( mw, $ ) {
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:form|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 grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
123 if ( grammarForms && grammarForms[ form ] ) {
124 return grammarForms[ form ][ word ] || word;
125 }
126 return word;
127 },
128
129 /**
130 * Turn a list of string into a simple list using commas and 'and'.
131 *
132 * See Language::listToText in languages/Language.php
133 *
134 * @param {string[]} list
135 * @return {string}
136 */
137 listToText: function ( list ) {
138 var text = '',
139 i = 0;
140
141 for ( ; i < list.length; i++ ) {
142 text += list[ i ];
143 if ( list.length - 2 === i ) {
144 text += mw.msg( 'and' ) + mw.msg( 'word-separator' );
145 } else if ( list.length - 1 !== i ) {
146 text += mw.msg( 'comma-separator' );
147 }
148 }
149 return text;
150 },
151
152 setSpecialCharacters: function ( data ) {
153 this.specialCharacters = data;
154 }
155 } );
156
157 }( mediaWiki, jQuery ) );