X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=languages%2FLanguage.php;h=e6feb45edf10375b82ab67d52d17482051628734;hb=6419ce51ca8f8499937c04974539318a3aa64f2f;hp=8db7e843cb311d585f744a01edee584ce74ffb7d;hpb=fc8e5467a7a184d563eaae62a3646395dfd72676;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/Language.php b/languages/Language.php index 8db7e843cb..e6feb45edf 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -139,6 +139,22 @@ class Language { 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12' ); + /** + * @since 1.20 + * @var array + */ + static public $durationIntervals = array( + 'millennia' => 31557600000, + 'centuries' => 3155760000, + 'decades' => 315576000, + 'years' => 31557600, // 86400 * 365.25 + 'weeks' => 604800, + 'days' => 86400, + 'hours' => 3600, + 'minutes' => 60, + 'seconds' => 1, + ); + /** * Get a cached language object for a given language code * @param $code String @@ -1908,6 +1924,63 @@ class Language { return $this->sprintfDate( $df, $ts ); } + /** + * Takes a number of seconds and turns it into a text using values such as hours and minutes. + * + * @since 1.20 + * + * @param integer $seconds The amount of seconds. + * @param array $chosenIntervals The intervals to enable. + * + * @return string + */ + public function formatDuration( $seconds, array $chosenIntervals = array() ) { + $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals ); + + $segments = array(); + + foreach ( $intervals as $intervalName => $intervalValue ) { + $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) ); + $segments[] = $message->inLanguage( $this )->escaped(); + } + + return $this->listToText( $segments ); + } + + /** + * Takes a number of seconds and returns an array with a set of corresponding intervals. + * For example 65 will be turned into array( minutes => 1, seconds => 5 ). + * + * @since 1.20 + * + * @param integer $seconds The amount of seconds. + * @param array $chosenIntervals The intervals to enable. + * + * @return array + */ + public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) { + if ( empty( $chosenIntervals ) ) { + $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' ); + } + + $intervals = array_intersect_key( self::$durationIntervals, array_flip( $chosenIntervals ) ); + $sortedNames = array_keys( $intervals ); + $smallestInterval = array_pop( $sortedNames ); + + $segments = array(); + + foreach ( $intervals as $name => $length ) { + $value = floor( $seconds / $length ); + + if ( $value > 0 || ( $name == $smallestInterval && empty( $segments ) ) ) { + $seconds -= $value * $length; + $segments[$name] = $value; + } + } + + return $segments; + } + /** * Internal helper function for userDate(), userTime() and userTimeAndDate() * @@ -3233,7 +3306,18 @@ class Language { } return $word; } - + /** + * Get the grammar forms for the content language + * @return array of grammar forms + * @since 1.20 + */ + function getGrammarForms() { + global $wgGrammarForms; + if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) { + return $wgGrammarForms[$this->getCode()]; + } + return array(); + } /** * Provides an alternative text depending on specified gender. * Usage {{gender:username|masculine|feminine|neutral}}.