Don't link to PHP @ php.net when running HHVM
[mediawiki.git] / resources / src / mediawiki.language / mediawiki.language.js
bloba0b556914d8406ff8eef0719f74f08115a0c045b
1 /*
2 * Methods for transforming message syntax.
3 */
4 ( function ( mw, $ ) {
6 /**
7 * @class mw.language
8 */
9 $.extend( mw.language, {
11 /**
12 * Process the PLURAL template substitution
14 * @private
15 * @param {Object} template Template object
16 * @param {string} template.title
17 * @param {Array} template.parameters
18 * @return {string}
20 procPLURAL: function ( template ) {
21 if ( template.title && template.parameters && mw.language.convertPlural ) {
22 // Check if we have forms to replace
23 if ( template.parameters.length === 0 ) {
24 return '';
26 // Restore the count into a Number ( if it got converted earlier )
27 var count = mw.language.convertNumber( template.title, true );
28 // Do convertPlural call
29 return mw.language.convertPlural( parseInt( count, 10 ), template.parameters );
31 // Could not process plural return first form or nothing
32 if ( template.parameters[0] ) {
33 return template.parameters[0];
35 return '';
38 /**
39 * Plural form transformations, needed for some languages.
41 * @param {number} count Non-localized quantifier
42 * @param {Array} forms List of plural forms
43 * @return {string} Correct form for quantifier in this language
45 convertPlural: function ( count, forms ) {
46 var pluralRules,
47 formCount,
48 form,
49 index,
50 equalsPosition,
51 pluralFormIndex = 0;
53 if ( !forms || forms.length === 0 ) {
54 return '';
57 // Handle for explicit n= forms
58 for ( index = 0; index < forms.length; index++ ) {
59 form = forms[index];
60 if ( /^\d+=/.test( form ) ) {
61 equalsPosition = form.indexOf( '=' );
62 formCount = parseInt( form.substring( 0, equalsPosition ), 10 );
63 if ( formCount === count ) {
64 return form.substr( equalsPosition + 1 );
66 forms[index] = undefined;
70 // Remove explicit plural forms from the forms.
71 forms = $.map( forms, function ( form ) {
72 return form;
73 } );
75 if ( forms.length === 0 ) {
76 return '';
79 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
80 if ( !pluralRules ) {
81 // default fallback.
82 return ( count === 1 ) ? forms[0] : forms[1];
84 pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
85 pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
86 return forms[pluralFormIndex];
89 /**
90 * Pads an array to a specific length by copying the last one element.
92 * @private
93 * @param {Array} forms Number of forms given to convertPlural
94 * @param {number} count Number of forms required
95 * @return {Array} Padded array of forms
97 preConvertPlural: function ( forms, count ) {
98 while ( forms.length < count ) {
99 forms.push( forms[ forms.length - 1 ] );
101 return forms;
105 * Provides an alternative text depending on specified gender.
107 * Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
108 * If second or third parameter are not specified, masculine is used.
110 * These details may be overriden per language.
112 * @param {string} gender 'male', 'female', or anything else for neutral.
113 * @param {Array} forms List of gender forms
114 * @return string
116 gender: function ( gender, forms ) {
117 if ( !forms || forms.length === 0 ) {
118 return '';
120 forms = mw.language.preConvertPlural( forms, 2 );
121 if ( gender === 'male' ) {
122 return forms[0];
124 if ( gender === 'female' ) {
125 return forms[1];
127 return ( forms.length === 3 ) ? forms[2] : forms[0];
131 * Grammatical transformations, needed for inflected languages.
132 * Invoked by putting `{{grammar:form|word}}` in a message.
134 * The rules can be defined in $wgGrammarForms global or computed
135 * dynamically by overriding this method per language.
137 * @param {string} word
138 * @param {string} form
139 * @return {string}
141 convertGrammar: function ( word, form ) {
142 var grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
143 if ( grammarForms && grammarForms[form] ) {
144 return grammarForms[form][word] || word;
146 return word;
149 } );
151 }( mediaWiki, jQuery ) );