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' );
76 return [ Language::isSupportedLanguage( $code ) ];
89 $this->
checkType(
'isKnownLanguageTag', 1, $code,
'string' );
90 return [ Language::isKnownLanguageTag( $code ) ];
100 $this->
checkType(
'isValidCode', 1, $code,
'string' );
101 return [ Language::isValidCode( $code ) ];
111 $this->
checkType(
'isValidBuiltInCode', 1, $code,
'string' );
112 return [ (bool)Language::isValidBuiltInCode( $code ) ];
123 $this->
checkType(
'fetchLanguageName', 1, $code,
'string' );
125 return [ Language::fetchLanguageName( $code, $inLanguage ) ];
136 $this->
checkTypeOptional(
'fetchLanguageNames', 1, $inLanguage,
'string',
null );
138 return [ Language::fetchLanguageNames( $inLanguage, $include ) ];
148 $this->
checkType(
'getFallbacksFor', 1, $code,
'string' );
149 $ret = Language::getFallbacksFor( $code );
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 ) {
173 $this->langCache[$code] = Language::factory( $code );
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' ) {
266 $gender = User::getDefaultOption(
'gender' );
269 $title = Title::newFromText( $username );
271 $username =
$title->getText();
275 $user = User::newFromName( $username );
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 );
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
Library for creating and parsing MW-style timestamps.
convertGrammar( $lang, $args)
convertGrammar handler
convertPlural( $lang, $args)
convertPlural handler
formatDate( $lang, $args)
formatDate handler
getDurationIntervals( $lang, $args)
getDurationIntervals handler
formatNum( $lang, $args)
formatNum handler
isValidBuiltInCode( $code)
Handler for isValidBuiltInCode.
formatDuration( $lang, $args)
formatDuration handler
fetchLanguageName( $code, $inLanguage)
Handler for fetchLanguageName.
getContLangCode()
Handler for getContLangCode.
isValidCode( $code)
Handler for isValidCode.
fetchLanguageNames( $inLanguage, $include)
Handler for fetchLanguageNames.
getFallbacksFor( $code)
Handler for fetchLanguageNames.
isSupportedLanguage( $code)
Handler for isSupportedLanguage.
gender( $lang, $args)
gender handler
isKnownLanguageTag( $code)
Handler for isKnownLanguageTag.
languageMethod( $name, $args)
Language object method handler.
This class provides some basic services that Lua libraries will probably need.
getLuaType( $var)
Get the Lua type corresponding to the type of the variable.
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
getEngine()
Get the engine.
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
getParserOptions()
Get the parser options.
if(!isset( $args[0])) $lang