2013-12-22 00:38:19 +00:00
|
|
|
/*
|
|
|
|
|
* Methods for transforming message syntax.
|
2010-10-26 23:40:26 +00:00
|
|
|
*/
|
2012-07-24 20:45:20 +00:00
|
|
|
( function ( mw, $ ) {
|
2010-10-26 23:40:26 +00:00
|
|
|
|
2013-12-22 00:38:19 +00:00
|
|
|
/**
|
|
|
|
|
* @class mw.language
|
|
|
|
|
*/
|
|
|
|
|
$.extend( mw.language, {
|
2012-04-02 13:01:10 +00:00
|
|
|
|
2010-10-26 23:40:26 +00:00
|
|
|
/**
|
2010-11-12 23:10:58 +00:00
|
|
|
* Process the PLURAL template substitution
|
|
|
|
|
*
|
2013-12-22 00:38:19 +00:00
|
|
|
* @private
|
|
|
|
|
* @param {Object} template Template object
|
|
|
|
|
* @param {string} template.title
|
|
|
|
|
* @param {Array} template.parameters
|
|
|
|
|
* @return {string}
|
2010-10-26 23:40:26 +00:00
|
|
|
*/
|
2012-07-24 20:45:20 +00:00
|
|
|
procPLURAL: function ( template ) {
|
2011-01-31 19:33:16 +00:00
|
|
|
if ( template.title && template.parameters && mw.language.convertPlural ) {
|
2010-10-26 23:40:26 +00:00
|
|
|
// Check if we have forms to replace
|
2012-01-02 09:21:03 +00:00
|
|
|
if ( template.parameters.length === 0 ) {
|
2010-10-26 23:40:26 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
// Restore the count into a Number ( if it got converted earlier )
|
2011-01-31 19:33:16 +00:00
|
|
|
var count = mw.language.convertNumber( template.title, true );
|
2012-06-06 06:12:12 +00:00
|
|
|
// Do convertPlural call
|
2011-03-10 00:14:36 +00:00
|
|
|
return mw.language.convertPlural( parseInt( count, 10 ), template.parameters );
|
2010-10-26 23:40:26 +00:00
|
|
|
}
|
|
|
|
|
// Could not process plural return first form or nothing
|
2010-10-27 15:51:31 +00:00
|
|
|
if ( template.parameters[0] ) {
|
2010-10-26 23:40:26 +00:00
|
|
|
return template.parameters[0];
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
},
|
2012-07-24 20:45:20 +00:00
|
|
|
|
2010-10-26 23:40:26 +00:00
|
|
|
/**
|
|
|
|
|
* Plural form transformations, needed for some languages.
|
2010-11-12 23:10:58 +00:00
|
|
|
*
|
2013-12-22 00:38:19 +00:00
|
|
|
* @param {number} count Non-localized quantifier
|
|
|
|
|
* @param {Array} forms List of plural forms
|
|
|
|
|
* @return {string} Correct form for quantifier in this language
|
2010-10-26 23:40:26 +00:00
|
|
|
*/
|
Lint: Go-go-gadget jshint! Passing entire JS code base (again).
There were still some files not passing jshint, and for files
that did, we managed to screw 'em up again.
Added more explicit settings in .jshintrc to avoid relying on a
kind of default somewhere. There are too many default-factors:
closest(.jshintrc), ~/.jshintrc, IDE/editor, node-jshint..
Added node_modules/ and extensions/ to .jshintignore.
Previously "$ jshint ." would recurse over all kinds of
unrelated code. Extensions should have their own jshint
dotfiles. When linting from Jenkins this won't be a problem as
those will be ran per repo (so when linting core it will skip extensions and when in an extension dir, the core dotfiles
don't apply as they'll be out of scope).
Some of our modules are really messy and should be refactored
to be less spaghetti-ish and have more descriptive variable
names and more manageable function-level complexity.
But for this commit, I'm keeping it as much as-is as possible,
because its hard/large enough to review as it is.
A few errors are cited below to give an impression of the kind
of warnings I addressed (for cases where the diff isn't
so obvious):
* jquery.hidpi.js: line 110, col 15, Empty block.
* mediawiki.jqueryMsg.js: line 34, col 17, Too many var statements.
* mediawiki.jqueryMsg.js: line 145, col 33, Strings must use singlequote.
* mediawiki.action.edit.js: line 74, col 73, 'selectText' is defined but never used.
* startup.js: line 19, col 22, 'isCompatible' is defined but never used.
* jquery.byteLength.test.js: line 26, col 9, Identifier 'U_00A2' is not in camel case.
* jquery.localize.test.js: line 63, col 29, 'attrs' is defined but never used.
* mediawiki.cldr.test.js: line 72, col 27, 'mw' is not defined.
* mediawiki.jscompat.test.js: line 6, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 9, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 7, col 15, 'mw' is not defined.
* mediawiki.api.parse.test.js: line 14, col 24, '$' is not defined.
* mediawiki.api.test.js: line 43, col 28, 'data' is defined but never used.
Other fixes:
* Add closures fix implied global errors ($, mw and more),
and prevents local variables from becoming globals.
* Avoid jQ magic map arg etc. (per code conventions).
* Fix incorrect usage of jQuery methods (prop instead of attr,
prop false instead of removeProp, ..).
* Unquote keys in object literals for consistency, and
enforce single quotes (no magic quotes in javascript, as much
as we might think "\n" and '/n' are really exactly the same).
Chose single quotes over double quotes since that's what most
code already had and what conventions seemed to prefer
(both the old generic ones and the new per-lang ones since
2011/2012).
* Enforce camelCase variable names with jshint per code
conventions.
* $foo.on('x', fn).trigger('x') -> $foo.on('x', fn); fn()
(No event simulation overhead, unless intended of course)
* Incorrect indentation (ignore whitespace in the diff!).
* Avoid proprietary selectors like ':first' when .eq(0)
afterwards is just as possible (significantly faster in
jQuery due to mostly avoiding the Sizzle engine and going
native in modern browsers).
* When at it, convert deprecated jQuery methods to new ones.
Mostly just .delegate(sel, type, fn) -> .on(type, sel, fn).
* Addressed whitespace here and there.
Interesting:
* mediawiki.js: local function "compare" wasn't used anymore
(hasn't been in a while!) removed per jshint warning.
* mediawiki.special.recentchanges.js: Was a mess, only a few
lines of code, rewritten.
Pfew, let's hope it's the last one before we lint from Jenkins!
Change-Id: I23ad60a1d804c542d9b91454aaa20ce7be4ff289
2012-09-26 07:14:52 +00:00
|
|
|
convertPlural: function ( count, forms ) {
|
|
|
|
|
var pluralRules,
|
2013-07-13 11:11:56 +00:00
|
|
|
formCount,
|
|
|
|
|
form,
|
|
|
|
|
index,
|
|
|
|
|
equalsPosition,
|
Lint: Go-go-gadget jshint! Passing entire JS code base (again).
There were still some files not passing jshint, and for files
that did, we managed to screw 'em up again.
Added more explicit settings in .jshintrc to avoid relying on a
kind of default somewhere. There are too many default-factors:
closest(.jshintrc), ~/.jshintrc, IDE/editor, node-jshint..
Added node_modules/ and extensions/ to .jshintignore.
Previously "$ jshint ." would recurse over all kinds of
unrelated code. Extensions should have their own jshint
dotfiles. When linting from Jenkins this won't be a problem as
those will be ran per repo (so when linting core it will skip extensions and when in an extension dir, the core dotfiles
don't apply as they'll be out of scope).
Some of our modules are really messy and should be refactored
to be less spaghetti-ish and have more descriptive variable
names and more manageable function-level complexity.
But for this commit, I'm keeping it as much as-is as possible,
because its hard/large enough to review as it is.
A few errors are cited below to give an impression of the kind
of warnings I addressed (for cases where the diff isn't
so obvious):
* jquery.hidpi.js: line 110, col 15, Empty block.
* mediawiki.jqueryMsg.js: line 34, col 17, Too many var statements.
* mediawiki.jqueryMsg.js: line 145, col 33, Strings must use singlequote.
* mediawiki.action.edit.js: line 74, col 73, 'selectText' is defined but never used.
* startup.js: line 19, col 22, 'isCompatible' is defined but never used.
* jquery.byteLength.test.js: line 26, col 9, Identifier 'U_00A2' is not in camel case.
* jquery.localize.test.js: line 63, col 29, 'attrs' is defined but never used.
* mediawiki.cldr.test.js: line 72, col 27, 'mw' is not defined.
* mediawiki.jscompat.test.js: line 6, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 9, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 7, col 15, 'mw' is not defined.
* mediawiki.api.parse.test.js: line 14, col 24, '$' is not defined.
* mediawiki.api.test.js: line 43, col 28, 'data' is defined but never used.
Other fixes:
* Add closures fix implied global errors ($, mw and more),
and prevents local variables from becoming globals.
* Avoid jQ magic map arg etc. (per code conventions).
* Fix incorrect usage of jQuery methods (prop instead of attr,
prop false instead of removeProp, ..).
* Unquote keys in object literals for consistency, and
enforce single quotes (no magic quotes in javascript, as much
as we might think "\n" and '/n' are really exactly the same).
Chose single quotes over double quotes since that's what most
code already had and what conventions seemed to prefer
(both the old generic ones and the new per-lang ones since
2011/2012).
* Enforce camelCase variable names with jshint per code
conventions.
* $foo.on('x', fn).trigger('x') -> $foo.on('x', fn); fn()
(No event simulation overhead, unless intended of course)
* Incorrect indentation (ignore whitespace in the diff!).
* Avoid proprietary selectors like ':first' when .eq(0)
afterwards is just as possible (significantly faster in
jQuery due to mostly avoiding the Sizzle engine and going
native in modern browsers).
* When at it, convert deprecated jQuery methods to new ones.
Mostly just .delegate(sel, type, fn) -> .on(type, sel, fn).
* Addressed whitespace here and there.
Interesting:
* mediawiki.js: local function "compare" wasn't used anymore
(hasn't been in a while!) removed per jshint warning.
* mediawiki.special.recentchanges.js: Was a mess, only a few
lines of code, rewritten.
Pfew, let's hope it's the last one before we lint from Jenkins!
Change-Id: I23ad60a1d804c542d9b91454aaa20ce7be4ff289
2012-09-26 07:14:52 +00:00
|
|
|
pluralFormIndex = 0;
|
|
|
|
|
|
2012-01-02 09:21:03 +00:00
|
|
|
if ( !forms || forms.length === 0 ) {
|
2010-11-12 23:10:58 +00:00
|
|
|
return '';
|
2010-10-27 15:51:31 +00:00
|
|
|
}
|
2013-07-13 11:11:56 +00:00
|
|
|
|
|
|
|
|
// Handle for explicit n= forms
|
|
|
|
|
for ( index = 0; index < forms.length; index++ ) {
|
|
|
|
|
form = forms[index];
|
2013-07-23 15:03:04 +00:00
|
|
|
if ( /^\d+=/.test( form ) ) {
|
2013-07-13 11:11:56 +00:00
|
|
|
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;
|
|
|
|
|
} );
|
|
|
|
|
|
2013-10-24 05:16:14 +00:00
|
|
|
if ( forms.length === 0 ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
Lint: Go-go-gadget jshint! Passing entire JS code base (again).
There were still some files not passing jshint, and for files
that did, we managed to screw 'em up again.
Added more explicit settings in .jshintrc to avoid relying on a
kind of default somewhere. There are too many default-factors:
closest(.jshintrc), ~/.jshintrc, IDE/editor, node-jshint..
Added node_modules/ and extensions/ to .jshintignore.
Previously "$ jshint ." would recurse over all kinds of
unrelated code. Extensions should have their own jshint
dotfiles. When linting from Jenkins this won't be a problem as
those will be ran per repo (so when linting core it will skip extensions and when in an extension dir, the core dotfiles
don't apply as they'll be out of scope).
Some of our modules are really messy and should be refactored
to be less spaghetti-ish and have more descriptive variable
names and more manageable function-level complexity.
But for this commit, I'm keeping it as much as-is as possible,
because its hard/large enough to review as it is.
A few errors are cited below to give an impression of the kind
of warnings I addressed (for cases where the diff isn't
so obvious):
* jquery.hidpi.js: line 110, col 15, Empty block.
* mediawiki.jqueryMsg.js: line 34, col 17, Too many var statements.
* mediawiki.jqueryMsg.js: line 145, col 33, Strings must use singlequote.
* mediawiki.action.edit.js: line 74, col 73, 'selectText' is defined but never used.
* startup.js: line 19, col 22, 'isCompatible' is defined but never used.
* jquery.byteLength.test.js: line 26, col 9, Identifier 'U_00A2' is not in camel case.
* jquery.localize.test.js: line 63, col 29, 'attrs' is defined but never used.
* mediawiki.cldr.test.js: line 72, col 27, 'mw' is not defined.
* mediawiki.jscompat.test.js: line 6, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 9, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 7, col 15, 'mw' is not defined.
* mediawiki.api.parse.test.js: line 14, col 24, '$' is not defined.
* mediawiki.api.test.js: line 43, col 28, 'data' is defined but never used.
Other fixes:
* Add closures fix implied global errors ($, mw and more),
and prevents local variables from becoming globals.
* Avoid jQ magic map arg etc. (per code conventions).
* Fix incorrect usage of jQuery methods (prop instead of attr,
prop false instead of removeProp, ..).
* Unquote keys in object literals for consistency, and
enforce single quotes (no magic quotes in javascript, as much
as we might think "\n" and '/n' are really exactly the same).
Chose single quotes over double quotes since that's what most
code already had and what conventions seemed to prefer
(both the old generic ones and the new per-lang ones since
2011/2012).
* Enforce camelCase variable names with jshint per code
conventions.
* $foo.on('x', fn).trigger('x') -> $foo.on('x', fn); fn()
(No event simulation overhead, unless intended of course)
* Incorrect indentation (ignore whitespace in the diff!).
* Avoid proprietary selectors like ':first' when .eq(0)
afterwards is just as possible (significantly faster in
jQuery due to mostly avoiding the Sizzle engine and going
native in modern browsers).
* When at it, convert deprecated jQuery methods to new ones.
Mostly just .delegate(sel, type, fn) -> .on(type, sel, fn).
* Addressed whitespace here and there.
Interesting:
* mediawiki.js: local function "compare" wasn't used anymore
(hasn't been in a while!) removed per jshint warning.
* mediawiki.special.recentchanges.js: Was a mess, only a few
lines of code, rewritten.
Pfew, let's hope it's the last one before we lint from Jenkins!
Change-Id: I23ad60a1d804c542d9b91454aaa20ce7be4ff289
2012-09-26 07:14:52 +00:00
|
|
|
pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' );
|
2012-08-22 13:15:44 +00:00
|
|
|
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];
|
2010-10-26 23:40:26 +00:00
|
|
|
},
|
2012-07-24 20:45:20 +00:00
|
|
|
|
2010-10-26 23:40:26 +00:00
|
|
|
/**
|
|
|
|
|
* Pads an array to a specific length by copying the last one element.
|
2010-11-12 23:10:58 +00:00
|
|
|
*
|
2013-12-22 00:38:19 +00:00
|
|
|
* @private
|
|
|
|
|
* @param {Array} forms Number of forms given to convertPlural
|
|
|
|
|
* @param {number} count Number of forms required
|
|
|
|
|
* @return {Array} Padded array of forms
|
2010-10-26 23:40:26 +00:00
|
|
|
*/
|
2012-07-24 20:45:20 +00:00
|
|
|
preConvertPlural: function ( forms, count ) {
|
2010-10-26 23:40:26 +00:00
|
|
|
while ( forms.length < count ) {
|
2014-01-31 02:03:11 +00:00
|
|
|
forms.push( forms[ forms.length - 1 ] );
|
2010-11-12 23:10:58 +00:00
|
|
|
}
|
2010-10-26 23:40:26 +00:00
|
|
|
return forms;
|
|
|
|
|
},
|
2012-07-24 20:45:20 +00:00
|
|
|
|
2012-01-02 09:08:23 +00:00
|
|
|
/**
|
|
|
|
|
* Provides an alternative text depending on specified gender.
|
2013-12-22 00:38:19 +00:00
|
|
|
*
|
|
|
|
|
* Usage in message text: `{{gender:[gender|user object]|masculine|feminine|neutral}}`.
|
2012-01-02 09:08:23 +00:00
|
|
|
* If second or third parameter are not specified, masculine is used.
|
2012-06-06 06:12:12 +00:00
|
|
|
*
|
2012-01-02 09:08:23 +00:00
|
|
|
* These details may be overriden per language.
|
|
|
|
|
*
|
2013-12-22 00:38:19 +00:00
|
|
|
* @param {string} gender 'male', 'female', or anything else for neutral.
|
|
|
|
|
* @param {Array} forms List of gender forms
|
2012-01-02 09:08:23 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
Lint: Go-go-gadget jshint! Passing entire JS code base (again).
There were still some files not passing jshint, and for files
that did, we managed to screw 'em up again.
Added more explicit settings in .jshintrc to avoid relying on a
kind of default somewhere. There are too many default-factors:
closest(.jshintrc), ~/.jshintrc, IDE/editor, node-jshint..
Added node_modules/ and extensions/ to .jshintignore.
Previously "$ jshint ." would recurse over all kinds of
unrelated code. Extensions should have their own jshint
dotfiles. When linting from Jenkins this won't be a problem as
those will be ran per repo (so when linting core it will skip extensions and when in an extension dir, the core dotfiles
don't apply as they'll be out of scope).
Some of our modules are really messy and should be refactored
to be less spaghetti-ish and have more descriptive variable
names and more manageable function-level complexity.
But for this commit, I'm keeping it as much as-is as possible,
because its hard/large enough to review as it is.
A few errors are cited below to give an impression of the kind
of warnings I addressed (for cases where the diff isn't
so obvious):
* jquery.hidpi.js: line 110, col 15, Empty block.
* mediawiki.jqueryMsg.js: line 34, col 17, Too many var statements.
* mediawiki.jqueryMsg.js: line 145, col 33, Strings must use singlequote.
* mediawiki.action.edit.js: line 74, col 73, 'selectText' is defined but never used.
* startup.js: line 19, col 22, 'isCompatible' is defined but never used.
* jquery.byteLength.test.js: line 26, col 9, Identifier 'U_00A2' is not in camel case.
* jquery.localize.test.js: line 63, col 29, 'attrs' is defined but never used.
* mediawiki.cldr.test.js: line 72, col 27, 'mw' is not defined.
* mediawiki.jscompat.test.js: line 6, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 9, col 17, Strings must use singlequote.
* mediawiki.api.parse.test.js: line 7, col 15, 'mw' is not defined.
* mediawiki.api.parse.test.js: line 14, col 24, '$' is not defined.
* mediawiki.api.test.js: line 43, col 28, 'data' is defined but never used.
Other fixes:
* Add closures fix implied global errors ($, mw and more),
and prevents local variables from becoming globals.
* Avoid jQ magic map arg etc. (per code conventions).
* Fix incorrect usage of jQuery methods (prop instead of attr,
prop false instead of removeProp, ..).
* Unquote keys in object literals for consistency, and
enforce single quotes (no magic quotes in javascript, as much
as we might think "\n" and '/n' are really exactly the same).
Chose single quotes over double quotes since that's what most
code already had and what conventions seemed to prefer
(both the old generic ones and the new per-lang ones since
2011/2012).
* Enforce camelCase variable names with jshint per code
conventions.
* $foo.on('x', fn).trigger('x') -> $foo.on('x', fn); fn()
(No event simulation overhead, unless intended of course)
* Incorrect indentation (ignore whitespace in the diff!).
* Avoid proprietary selectors like ':first' when .eq(0)
afterwards is just as possible (significantly faster in
jQuery due to mostly avoiding the Sizzle engine and going
native in modern browsers).
* When at it, convert deprecated jQuery methods to new ones.
Mostly just .delegate(sel, type, fn) -> .on(type, sel, fn).
* Addressed whitespace here and there.
Interesting:
* mediawiki.js: local function "compare" wasn't used anymore
(hasn't been in a while!) removed per jshint warning.
* mediawiki.special.recentchanges.js: Was a mess, only a few
lines of code, rewritten.
Pfew, let's hope it's the last one before we lint from Jenkins!
Change-Id: I23ad60a1d804c542d9b91454aaa20ce7be4ff289
2012-09-26 07:14:52 +00:00
|
|
|
gender: function ( gender, forms ) {
|
2012-01-02 09:21:03 +00:00
|
|
|
if ( !forms || forms.length === 0 ) {
|
2012-01-02 09:08:23 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
forms = mw.language.preConvertPlural( forms, 2 );
|
|
|
|
|
if ( gender === 'male' ) {
|
|
|
|
|
return forms[0];
|
|
|
|
|
}
|
|
|
|
|
if ( gender === 'female' ) {
|
|
|
|
|
return forms[1];
|
|
|
|
|
}
|
2012-01-02 09:21:03 +00:00
|
|
|
return ( forms.length === 3 ) ? forms[2] : forms[0];
|
2012-01-02 09:08:23 +00:00
|
|
|
},
|
|
|
|
|
|
2012-04-02 13:01:10 +00:00
|
|
|
/**
|
|
|
|
|
* Grammatical transformations, needed for inflected languages.
|
2013-12-22 00:38:19 +00:00
|
|
|
* Invoked by putting `{{grammar:form|word}}` in a message.
|
|
|
|
|
*
|
|
|
|
|
* The rules can be defined in $wgGrammarForms global or computed
|
|
|
|
|
* dynamically by overriding this method per language.
|
2012-04-02 13:01:10 +00:00
|
|
|
*
|
2013-12-22 00:38:19 +00:00
|
|
|
* @param {string} word
|
|
|
|
|
* @param {string} form
|
|
|
|
|
* @return {string}
|
2012-04-02 13:01:10 +00:00
|
|
|
*/
|
|
|
|
|
convertGrammar: function ( word, form ) {
|
2012-06-06 06:12:12 +00:00
|
|
|
var grammarForms = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'grammarForms' );
|
2012-04-02 13:01:10 +00:00
|
|
|
if ( grammarForms && grammarForms[form] ) {
|
|
|
|
|
return grammarForms[form][word] || word;
|
|
|
|
|
}
|
|
|
|
|
return word;
|
2013-01-18 15:34:54 +00:00
|
|
|
}
|
2012-04-02 13:01:10 +00:00
|
|
|
|
2013-12-22 00:38:19 +00:00
|
|
|
} );
|
2012-04-02 13:01:10 +00:00
|
|
|
|
2012-07-24 20:45:20 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|