Upgrade to rc5

This commit is contained in:
Bastian Allgeier
2020-12-10 11:24:42 +01:00
parent 3fec0d7c93
commit c378376bc9
257 changed files with 13009 additions and 1846 deletions

View File

@@ -3,6 +3,7 @@
namespace Kirby\Toolkit;
use Closure;
use NumberFormatter;
/**
* Localization class, roughly inspired by VueI18n
@@ -43,6 +44,13 @@ class I18n
*/
public static $fallback = 'en';
/**
* Cache of `NumberFormatter` objects by locale
*
* @var array
*/
protected static $decimalNumberFormatters = [];
/**
* Returns the fallback code
*
@@ -78,6 +86,25 @@ class I18n
return $count === 1 ? 'singular' : 'plural';
}
/**
* Formats a number
*
* @param int|float $number
* @param string $locale
* @return string
*/
public static function formatNumber($number, string $locale = null): string
{
$locale = $locale ?? static::locale();
$formatter = static::decimalNumberFormatter($locale);
if ($formatter !== null) {
$number = $formatter->format($number);
}
return (string)$number;
}
/**
* Returns the locale code
*
@@ -189,6 +216,24 @@ class I18n
return static::$translations;
}
/**
* Returns (and creates) a decimal number formatter for a given locale
*
* @return \NumberFormatter|null
*/
protected static function decimalNumberFormatter(string $locale): ?NumberFormatter
{
if (isset(static::$decimalNumberFormatters[$locale])) {
return static::$decimalNumberFormatters[$locale];
}
if (extension_loaded('intl') !== true || class_exists('NumberFormatter') !== true) {
return null;
}
return static::$decimalNumberFormatters[$locale] = new NumberFormatter($locale, NumberFormatter::DECIMAL);
}
/**
* Translates amounts
*
@@ -202,10 +247,13 @@ class I18n
* @param string $key
* @param int $count
* @param string $locale
* @param bool $formatNumber If set to `false`, the count is not formatted
* @return mixed
*/
public static function translateCount(string $key, int $count, string $locale = null)
public static function translateCount(string $key, int $count, string $locale = null, bool $formatNumber = true)
{
$locale = $locale ?? static::locale();
$translation = static::translate($key, null, $locale);
if ($translation === null) {
@@ -226,6 +274,10 @@ class I18n
}
}
if ($formatNumber === true) {
$count = static::formatNumber($count, $locale);
}
return str_replace('{{ count }}', $count, $message);
}
}