summaryrefslogtreecommitdiff
path: root/resources/mediawiki.language/mediawiki.language.js
blob: 631d13dfb4a75404e12d128c5903b67b2567cbc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * Localized Language support attempts to mirror some of the functionality of
 * Language.php in MediaWiki.
 * This adds methods for transforming message text.
 */
( function ( mw, $ ) {

var language = {

	/**
	 * Process the PLURAL template substitution
	 *
	 * @param {object} template Template object
	 * @format template
	 *  {
	 *      'title': [title of template],
	 *      'parameters': [template parameters]
	 *  }
	 * @example {{Template:title|params}}
	 */
	procPLURAL: function ( template ) {
		if ( template.title && template.parameters && mw.language.convertPlural ) {
			// Check if we have forms to replace
			if ( template.parameters.length === 0 ) {
				return '';
			}
			// Restore the count into a Number ( if it got converted earlier )
			var count = mw.language.convertNumber( template.title, true );
			// Do convertPlural call
			return mw.language.convertPlural( parseInt( count, 10 ), template.parameters );
		}
		// Could not process plural return first form or nothing
		if ( template.parameters[0] ) {
			return template.parameters[0];
		}
		return '';
	},

	/**
	 * Plural form transformations, needed for some languages.
	 *
	 * @param count integer Non-localized quantifier
	 * @param forms array List of plural forms
	 * @return string Correct form for quantifier in this language
	 */
	convertPlural: function ( count, forms ) {
		var pluralRules,
			formCount,
			form,
			index,
			equalsPosition,
			pluralFormIndex = 0;

		if ( !forms || forms.length === 0 ) {
			return '';
		}

		// Handle for explicit n= forms
		for ( index = 0; index < forms.length; index++ ) {
			form = forms[index];
			if ( /^\d+=/.test( form ) ) {
				equalsPosition = form.indexOf( '=' );
				formCount = parseInt( form.substring( 0, equalsPosition ), 10 );
				if ( formCount === count ) {
					return form.substr( equalsPosition + 1 );
				}
				forms[index] = undefined;
			}
		}

		// Remove explicit plural forms from the forms.
		forms = $.map( forms, function ( form ) {
			return form;
		} );

		pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
		if ( !pluralRules ) {
			// default fallback.
			return ( count === 1 ) ? forms[0] : forms[1];
		}
		pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules );
		pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 );
		return forms[pluralFormIndex];
	},

	/**
	 * Pads an array to a specific length by copying the last one element.
	 *
	 * @param forms array Number of forms given to convertPlural
	 * @param count integer Number of forms required
	 * @return array Padded array of forms
	 */
	preConvertPlural: function ( forms, count ) {
		while ( forms.length < count ) {
			forms.push( forms[ forms.length-1 ] );
		}
		return forms;
	},

	/**
	 * Provides an alternative text depending on specified gender.
	 * Usage {{gender:[gender|user object]|masculine|feminine|neutral}}.
	 * If second or third parameter are not specified, masculine is used.
	 *
	 * These details may be overriden per language.
	 *
	 * @param gender string male, female, or anything else for neutral.
	 * @param forms array List of gender forms
	 *
	 * @return string
	 */
	gender: function ( gender, forms ) {
		if ( !forms || forms.length === 0 ) {
			return '';
		}
		forms = mw.language.preConvertPlural( forms, 2 );
		if ( gender === 'male' ) {
			return forms[0];
		}
		if ( gender === 'female' ) {
			return forms[1];
		}
		return ( forms.length === 3 ) ? forms[2] : forms[0];
	},

	/**
	 * Grammatical transformations, needed for inflected languages.
	 * Invoked by putting {{grammar:form|word}} in a message.
	 * The rules can be defined in $wgGrammarForms global or grammar
	 * forms can be computed dynamically by overriding this method per language
	 *
	 * @param word {String}
	 * @param form {String}
	 * @return {String}
	 */
	convertGrammar: function ( word, form ) {
		var grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
		if ( grammarForms && grammarForms[form] ) {
			return grammarForms[form][word] || word;
		}
		return word;
	}

};

$.extend( mw.language, language );

}( mediaWiki, jQuery ) );