10 public function register() {
14 $this->maxLangCacheSize = $this->
getEngine()->getOption(
'maxLangCacheSize' );
18 'isSupportedLanguage',
35 'getDurationIntervals',
36 'parseFormattedNumber',
43 foreach ( $statics as $name ) {
44 $lib[$name] = [ $this, $name ];
47 foreach ( $methods as $name ) {
48 $lib[$name] =
function () use ( $ths, $name ) {
49 $args = func_get_args();
50 return $ths->languageMethod( $name,
$args );
53 return $this->
getEngine()->registerInterface(
'mw.language.lua', $lib );
73 $this->
checkType(
'isSupportedLanguage', 1, $code,
'string' );
89 $this->
checkType(
'isKnownLanguageTag', 1, $code,
'string' );
100 $this->
checkType(
'isValidCode', 1, $code,
'string' );
111 $this->
checkType(
'isValidBuiltInCode', 1, $code,
'string' );
123 $this->
checkType(
'fetchLanguageName', 1, $code,
'string' );
136 $this->
checkTypeOptional(
'fetchLanguageNames', 1, $inLanguage,
'string',
null );
148 $this->
checkType(
'getFallbacksFor', 1, $code,
'string' );
151 if ( count( $ret ) ) {
152 $ret = array_combine( range( 1, count( $ret ) ), $ret );
166 $name = strval( $name );
167 $code = array_shift(
$args );
168 if ( !isset( $this->langCache[$code] ) ) {
169 if ( count( $this->langCache ) > $this->maxLangCacheSize ) {
178 $lang = $this->langCache[$code];
182 return [
$lang->$name() ];
193 case 'parseFormattedNumber':
194 if ( is_numeric(
$args[0] ) ) {
217 $number = array_shift(
$args );
218 $this->
checkType(
'convertPlural', 1, $number,
'number' );
219 if ( is_array(
$args[0] ) ) {
222 $forms = array_values( array_map(
'strval',
$args ) );
223 return [
$lang->convertPlural( $number, $forms ) ];
248 $username = trim( array_shift(
$args ) );
250 if ( is_array(
$args[0] ) ) {
253 $forms = array_values( array_map(
'strval',
$args ) );
256 if ( count( $forms ) === 0 ) {
258 } elseif ( count( $forms ) === 1 ) {
259 return [ $forms[0] ];
262 if ( $username ===
'male' || $username ===
'female' ) {
271 $username =
$title->getText();
277 $genderCache = MediaWikiServices::getInstance()->getGenderCache();
278 $gender = $genderCache->getGenderOf( $user, __METHOD__ );
279 } elseif ( $username ===
'' ) {
281 if ( $parserOptions->getInterfaceMessage() ) {
282 $genderCache = MediaWikiServices::getInstance()->getGenderCache();
283 $gender = $genderCache->getGenderOf( $parserOptions->getUser(), __METHOD__ );
287 return [
$lang->gender( $gender, $forms ) ];
299 $this->
checkType(
'formatNum', 1, $num,
'number' );
302 if ( isset(
$args[1] ) ) {
305 $noCommafy = !empty( $options[
'noCommafy'] );
307 return [
$lang->formatNum( $num, $noCommafy ) ];
323 list( $format, $date, $local ) =
$args;
324 $langcode =
$lang->getCode();
326 if ( $date ===
'' ) {
329 $date = $timestamp->getTimestamp( TS_ISO_8601 );
332 # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
333 if ( preg_match(
'/^[0-9]{4}$/', $date ) ) {
334 $date =
'00:00 ' . $date;
341 if ( isset( $this->timeCache[$format][$cacheKey][$langcode][$local] ) ) {
342 $ttl = $this->timeCache[$format][$cacheKey][$langcode][$local][1];
343 if ( $useTTL && $ttl !==
null ) {
346 return [ $this->timeCache[$format][$cacheKey][$langcode][$local][0] ];
349 # Default input timezone is UTC.
351 $utc =
new DateTimeZone(
'UTC' );
352 $dateObject =
new DateTime( $date, $utc );
353 }
catch ( Exception $ex ) {
354 throw new Scribunto_LuaError(
"bad argument #2 to 'formatDate' (not a valid timestamp)" );
357 # Set output timezone.
363 $tz =
new DateTimeZone( date_default_timezone_get() );
368 $dateObject->setTimezone( $tz );
370 $ts = $dateObject->format(
'YmdHis' );
373 throw new Scribunto_LuaError(
"mw.language:formatDate() only supports years from 0" );
374 } elseif ( $ts >= 100000000000000 ) {
375 throw new Scribunto_LuaError(
"mw.language:formatDate() only supports years up to 9999" );
379 $ret =
$lang->sprintfDate( $format, $ts, $tz, $ttl );
380 $this->timeCache[$format][$cacheKey][$langcode][$local] = [ $ret, $ttl ];
381 if ( $useTTL && $ttl !==
null ) {
398 list( $seconds, $chosenIntervals ) =
$args;
399 $langcode =
$lang->getCode();
400 $chosenIntervals = array_values( $chosenIntervals );
402 $ret =
$lang->formatDuration( $seconds, $chosenIntervals );
414 $this->
checkType(
'getDurationIntervals', 1,
$args[0],
'number' );
417 list( $seconds, $chosenIntervals ) =
$args;
418 $langcode =
$lang->getCode();
419 $chosenIntervals = array_values( $chosenIntervals );
421 $ret =
$lang->getDurationIntervals( $seconds, $chosenIntervals );