- implemented php side of plural translation

- using it to generate labels dynamically for the expire options
(deprecating the [expire_labels] configuration).
- added translation of the human readable data sizes to support the
french octet
- fixed IEC label for kibibytes
This commit is contained in:
El RIDO
2015-09-06 19:21:17 +02:00
parent c83ba8256f
commit b060d57524
9 changed files with 154 additions and 50 deletions

View File

@@ -33,7 +33,36 @@ class filter
}
/**
* format a given number of bytes
* format a given time string into a human readable label (localized)
*
* accepts times in the format "[integer][time unit]"
*
* @access public
* @static
* @param string $time
* @throws Exception
* @return string
*/
public static function time_humanreadable($time)
{
if (preg_match('/^(\d+) *(\w+)$/', $time, $matches) !== 1) {
throw new Exception("Error parsing time format '$time'", 30);
}
switch ($matches[2]) {
case 'sec':
$unit = 'second';
break;
case 'min':
$unit = 'minute';
break;
default:
$unit = rtrim($matches[2], 's');
}
return i18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]);
}
/**
* format a given number of bytes in IEC 80000-13:2008 notation (localized)
*
* @access public
* @static
@@ -42,13 +71,13 @@ class filter
*/
public static function size_humanreadable($size)
{
$iec = array('B', 'kiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$i = 0;
while ( ( $size / 1024 ) >= 1 ) {
$size = $size / 1024;
$i++;
}
return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . $iec[$i];
return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . i18n::_($iec[$i]);
}
/**

View File

@@ -17,14 +17,23 @@
*/
class i18n
{
/**
* language
*
* @access protected
* @static
* @var string
*/
protected static $_language = 'en';
/**
* translation cache
*
* @access private
* @access protected
* @static
* @var array
*/
private static $_translations = array();
protected static $_translations = array();
/**
* translate a string, alias for translate()
@@ -53,12 +62,30 @@ class i18n
{
if (empty($messageId)) return $messageId;
if (count(self::$_translations) === 0) self::loadTranslations();
$messages = $messageId;
if (is_array($messageId))
{
$messageId = count($messageId) > 1 ? $messageId[1] : $messageId[0];
}
if (!array_key_exists($messageId, self::$_translations))
{
self::$_translations[$messageId] = $messageId;
self::$_translations[$messageId] = $messages;
}
$args = func_get_args();
$args[0] = self::$_translations[$messageId];
if (is_array(self::$_translations[$messageId]))
{
$number = (int) $args[1];
$key = self::_getPluralForm($number);
$max = count(self::$_translations[$messageId]) - 1;
if ($key > $max) $key = $max;
$args[0] = self::$_translations[$messageId][$key];
$args[1] = $number;
}
else
{
$args[0] = self::$_translations[$messageId];
}
return call_user_func_array('sprintf', $args);
}
@@ -91,6 +118,7 @@ class i18n
// load translations
if ($match != 'en')
{
self::$_language = $match;
self::$_translations = json_decode(
file_get_contents($path . DIRECTORY_SEPARATOR . $match . '.json'),
true
@@ -137,6 +165,27 @@ class i18n
return $languages;
}
/**
* determines the plural form to use based on current language and given number
*
* From: http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
*
* @param int $n
* @return int
*/
protected static function _getPluralForm($n)
{
switch (self::$_language) {
case 'fr':
return ($n > 1 ? 1 : 0);
case 'pl':
return ($n == 1 ? 0 : $n%10 >= 2 && $n %10 <=4 && ($n%100 < 10 || $n%100 >= 20) ? 1 : 2);
// en, de
default:
return ($n != 1 ? 1 : 0);
}
}
/**
* compares two language preference arrays and returns the preferred match
*

View File

@@ -579,12 +579,8 @@ class zerobin
// label all the expiration options
$expire = array();
foreach ($this->_conf['expire_options'] as $key => $value) {
$expire[$key] = i18n::_(
array_key_exists($key, $this->_conf['expire_labels']) ?
$this->_conf['expire_labels'][$key] :
$key
);
foreach ($this->_conf['expire_options'] as $time => $seconds) {
$expire[$time] = ($seconds == 0) ? i18n::_(ucfirst($time)): filter::time_humanreadable($time);
}
$page = new RainTPL;