[JobQueue] Added missing delete query.
[mediawiki.git] / resources / mediawiki.language / mediawiki.language.js
blob514dbd5ef87f26ed01dae2a18586c08723ed4dc8
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, $ ) {
8 var language = {
10 /**
11 * Process the PLURAL template substitution
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}}
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 '';
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];
36 return '';
39 /**
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 ) {
47 var pluralRules,
48 pluralFormIndex = 0;
50 if ( !forms || forms.length === 0 ) {
51 return '';
53 pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
54 if ( !pluralRules ) {
55 // default fallback.
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];
63 /**
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 ] );
74 return forms;
77 /**
78 * Converts a number using digitTransformTable.
80 * @param {num} number Value to be converted
81 * @param {boolean} integer Convert the return value to an integer
83 convertNumber: function ( num, integer ) {
84 var i, tmp, transformTable, numberString, convertedNumber;
86 if ( !mw.language.digitTransformTable ) {
87 return num;
89 // Set the target Transform table:
90 transformTable = mw.language.digitTransformTable;
91 // Check if the "restore" to Latin number flag is set:
92 if ( integer ) {
93 if ( parseInt( num, 10 ) === num ) {
94 return num;
96 tmp = [];
97 for ( i in transformTable ) {
98 tmp[ transformTable[ i ] ] = i;
100 transformTable = tmp;
102 numberString = '' + num;
103 convertedNumber = '';
104 for ( i = 0; i < numberString.length; i++ ) {
105 if ( transformTable[ numberString[i] ] ) {
106 convertedNumber += transformTable[numberString[i]];
107 } else {
108 convertedNumber += numberString[i];
111 return integer ? parseInt( convertedNumber, 10 ) : convertedNumber;
115 * Provides an alternative text depending on specified gender.
116 * Usage {{gender:[gender|user object]|masculine|feminine|neutral}}.
117 * If second or third parameter are not specified, masculine is used.
119 * These details may be overriden per language.
121 * @param gender string male, female, or anything else for neutral.
122 * @param forms array List of gender forms
124 * @return string
126 gender: function ( gender, forms ) {
127 if ( !forms || forms.length === 0 ) {
128 return '';
130 forms = mw.language.preConvertPlural( forms, 2 );
131 if ( gender === 'male' ) {
132 return forms[0];
134 if ( gender === 'female' ) {
135 return forms[1];
137 return ( forms.length === 3 ) ? forms[2] : forms[0];
141 * Grammatical transformations, needed for inflected languages.
142 * Invoked by putting {{grammar:form|word}} in a message.
143 * The rules can be defined in $wgGrammarForms global or grammar
144 * forms can be computed dynamically by overriding this method per language
146 * @param word {String}
147 * @param form {String}
148 * @return {String}
150 convertGrammar: function ( word, form ) {
151 var grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
152 if ( grammarForms && grammarForms[form] ) {
153 return grammarForms[form][word] || word;
155 return word;
158 // Digit Transform Table, populated by language classes where applicable
159 digitTransformTable: mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'digitTransformTable' )
162 $.extend( mw.language, language );
164 }( mediaWiki, jQuery ) );