| 1 |
( function () { |
| 2 |
Object.assign( mw.language, { |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
convertPlural: function ( count, forms, explicitPluralForms ) { |
| 13 |
let pluralFormIndex = 0; |
| 14 |
|
| 15 |
if ( explicitPluralForms && ( explicitPluralForms[ count ] !== undefined ) ) { |
| 16 |
return explicitPluralForms[ count ]; |
| 17 |
} |
| 18 |
|
| 19 |
if ( !forms || forms.length === 0 ) { |
| 20 |
return ''; |
| 21 |
} |
| 22 |
|
| 23 |
const pluralRules = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'pluralRules' ); |
| 24 |
if ( !pluralRules ) { |
| 25 |
|
| 26 |
return ( count === 1 ) ? forms[ 0 ] : forms[ 1 ]; |
| 27 |
} |
| 28 |
pluralFormIndex = mw.cldr.getPluralForm( count, pluralRules ); |
| 29 |
pluralFormIndex = Math.min( pluralFormIndex, forms.length - 1 ); |
| 30 |
return forms[ pluralFormIndex ]; |
| 31 |
}, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
preConvertPlural: function ( forms, count ) { |
| 42 |
while ( forms.length < count ) { |
| 43 |
forms.push( forms[ forms.length - 1 ] ); |
| 44 |
} |
| 45 |
return forms; |
| 46 |
}, |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
gender: function ( gender, forms ) { |
| 61 |
if ( !forms || forms.length === 0 ) { |
| 62 |
return ''; |
| 63 |
} |
| 64 |
forms = mw.language.preConvertPlural( forms, 2 ); |
| 65 |
if ( gender === 'male' ) { |
| 66 |
return forms[ 0 ]; |
| 67 |
} |
| 68 |
if ( gender === 'female' ) { |
| 69 |
return forms[ 1 ]; |
| 70 |
} |
| 71 |
return ( forms.length === 3 ) ? forms[ 2 ] : forms[ 0 ]; |
| 72 |
}, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
convertGrammar: function ( word, form ) { |
| 86 |
const userLanguage = mw.config.get( 'wgUserLanguage' ); |
| 87 |
|
| 88 |
const forms = mw.language.getData( userLanguage, 'grammarForms' ); |
| 89 |
if ( forms && forms[ form ] ) { |
| 90 |
return forms[ form ][ word ]; |
| 91 |
} |
| 92 |
|
| 93 |
const transformations = mw.language.getData( userLanguage, 'grammarTransformations' ); |
| 94 |
|
| 95 |
if ( !( transformations && transformations[ form ] ) ) { |
| 96 |
return word; |
| 97 |
} |
| 98 |
|
| 99 |
let patterns = transformations[ form ]; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if ( typeof patterns === 'string' ) { |
| 105 |
patterns = transformations[ patterns ]; |
| 106 |
} |
| 107 |
|
| 108 |
for ( let i = 0; i < patterns.length; i++ ) { |
| 109 |
const rule = patterns[ i ]; |
| 110 |
const sourcePattern = rule[ 0 ]; |
| 111 |
|
| 112 |
if ( sourcePattern === '@metadata' ) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
const regexp = new RegExp( sourcePattern ); |
| 117 |
const replacement = rule[ 1 ]; |
| 118 |
|
| 119 |
if ( word.match( regexp ) ) { |
| 120 |
return word.replace( regexp, replacement ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return word; |
| 125 |
}, |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
listToText: function ( list ) { |
| 136 |
let text = ''; |
| 137 |
|
| 138 |
for ( let i = 0; i < list.length; i++ ) { |
| 139 |
text += list[ i ]; |
| 140 |
if ( list.length - 2 === i ) { |
| 141 |
text += mw.message( 'and' ).escaped() + mw.message( 'word-separator' ).escaped(); |
| 142 |
} else if ( list.length - 1 !== i ) { |
| 143 |
text += mw.message( 'comma-separator' ).escaped(); |
| 144 |
} |
| 145 |
} |
| 146 |
return text; |
| 147 |
}, |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
bcp47: function ( languageTag ) { |
| 157 |
let isFirstSegment = true, |
| 158 |
isPrivate = false; |
| 159 |
|
| 160 |
languageTag = languageTag.toLowerCase(); |
| 161 |
|
| 162 |
const bcp47Map = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'bcp47Map' ); |
| 163 |
if ( bcp47Map && Object.prototype.hasOwnProperty.call( bcp47Map, languageTag ) ) { |
| 164 |
languageTag = bcp47Map[ languageTag ]; |
| 165 |
} |
| 166 |
|
| 167 |
const segments = languageTag.split( '-' ); |
| 168 |
const formatted = segments.map( ( segment ) => { |
| 169 |
let newSegment; |
| 170 |
|
| 171 |
|
| 172 |
if ( isPrivate ) { |
| 173 |
newSegment = segment.toLowerCase(); |
| 174 |
|
| 175 |
} else if ( segment.length === 2 && !isFirstSegment ) { |
| 176 |
newSegment = segment.toUpperCase(); |
| 177 |
|
| 178 |
} else if ( segment.length === 4 && !isFirstSegment ) { |
| 179 |
newSegment = segment.charAt( 0 ).toUpperCase() + segment.slice( 1 ).toLowerCase(); |
| 180 |
|
| 181 |
} else { |
| 182 |
newSegment = segment.toLowerCase(); |
| 183 |
} |
| 184 |
|
| 185 |
isPrivate = segment.toLowerCase() === 'x'; |
| 186 |
isFirstSegment = false; |
| 187 |
|
| 188 |
return newSegment; |
| 189 |
} ); |
| 190 |
|
| 191 |
return formatted.join( '-' ); |
| 192 |
} |
| 193 |
} ); |
| 194 |
|
| 195 |
}() ); |