2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
2014-03-17 02:56:43 +01:00
|
|
|
namespace Translator {
|
2013-02-18 03:48:21 +01:00
|
|
|
|
|
|
|
const PATH = 'locales/';
|
|
|
|
|
|
|
|
|
|
|
|
function translate($identifier)
|
|
|
|
{
|
|
|
|
$args = \func_get_args();
|
|
|
|
|
|
|
|
\array_shift($args);
|
2015-01-30 19:45:23 +01:00
|
|
|
\array_unshift($args, get($identifier, $identifier, $args));
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-02-26 00:00:36 +01:00
|
|
|
foreach ($args as &$arg) {
|
|
|
|
$arg = htmlspecialchars($arg, ENT_QUOTES, 'UTF-8', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return \call_user_func_array(
|
|
|
|
'sprintf',
|
|
|
|
$args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function translate_no_escaping($identifier)
|
|
|
|
{
|
|
|
|
$args = \func_get_args();
|
|
|
|
|
|
|
|
\array_shift($args);
|
|
|
|
\array_unshift($args, get($identifier, $identifier));
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
return \call_user_func_array(
|
|
|
|
'sprintf',
|
|
|
|
$args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function number($number)
|
|
|
|
{
|
|
|
|
return number_format(
|
|
|
|
$number,
|
|
|
|
get('number.decimals', 2),
|
|
|
|
get('number.decimals_separator', '.'),
|
|
|
|
get('number.thousands_separator', ',')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function currency($amount)
|
|
|
|
{
|
|
|
|
$position = get('currency.position', 'before');
|
|
|
|
$symbol = get('currency.symbol', '$');
|
|
|
|
$str = '';
|
|
|
|
|
|
|
|
if ($position === 'before') {
|
|
|
|
$str .= $symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
$str .= number($amount);
|
|
|
|
|
|
|
|
if ($position === 'after') {
|
|
|
|
$str .= ' '.$symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-13 03:08:55 +02:00
|
|
|
function datetime($format, $timestamp)
|
|
|
|
{
|
2014-12-02 18:46:47 +01:00
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
|
|
|
|
$format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
|
|
|
|
$format = preg_replace('#(?<!%)((?:%%)*)%k#', '\1%#H', $format);
|
|
|
|
}
|
2015-01-30 19:45:23 +01:00
|
|
|
|
2013-07-04 02:20:50 +02:00
|
|
|
return strftime($format, (int) $timestamp);
|
2013-04-13 03:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-20 23:53:15 +01:00
|
|
|
function get($identifier, $default = '', array $values = array())
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
$locales = container();
|
2015-01-30 19:45:23 +01:00
|
|
|
$translation = $default;
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2015-01-30 19:45:23 +01:00
|
|
|
if (isset($locales[$identifier])) {
|
|
|
|
if (is_array($locales[$identifier])) {
|
2015-03-20 23:53:15 +01:00
|
|
|
$translation = plural($identifier, $default, $values);
|
2016-04-18 01:44:45 +02:00
|
|
|
} else {
|
2015-01-30 19:45:23 +01:00
|
|
|
$translation = $locales[$identifier];
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
2015-01-30 19:45:23 +01:00
|
|
|
|
|
|
|
return $translation;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-20 23:53:15 +01:00
|
|
|
function plural($identifier, $default, array $values)
|
|
|
|
{
|
|
|
|
$locales = container();
|
|
|
|
$plural = 0;
|
|
|
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (is_numeric($value)) {
|
|
|
|
$value = abs($value);
|
|
|
|
$plural = (int) $locales['plural']($value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 10:45:07 +02:00
|
|
|
for ($i = $plural; $i >= 0; --$i) {
|
2015-03-20 23:53:15 +01:00
|
|
|
if (isset($locales[$identifier][$i])) {
|
|
|
|
return $locales[$identifier][$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
function load($language)
|
|
|
|
{
|
2013-04-27 14:44:30 +02:00
|
|
|
setlocale(LC_TIME, $language.'.UTF-8');
|
2013-04-13 03:08:55 +02:00
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
$path = PATH.$language;
|
|
|
|
$locales = array();
|
|
|
|
|
|
|
|
if (is_dir($path)) {
|
|
|
|
$dir = new \DirectoryIterator($path);
|
|
|
|
|
|
|
|
foreach ($dir as $fileinfo) {
|
|
|
|
if (strpos($fileinfo->getFilename(), '.php') !== false) {
|
|
|
|
$locales = array_merge($locales, include $fileinfo->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container($locales);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function container($locales = null)
|
|
|
|
{
|
|
|
|
static $values = array();
|
|
|
|
|
2013-04-13 03:27:51 +02:00
|
|
|
if ($locales !== null) {
|
2013-02-18 03:48:21 +01:00
|
|
|
$values = $locales;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function tne()
|
|
|
|
{
|
2014-03-17 02:56:43 +01:00
|
|
|
return call_user_func_array('\Translator\translate_no_escaping', func_get_args());
|
2014-02-26 00:00:36 +01:00
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function t()
|
|
|
|
{
|
2014-03-17 02:56:43 +01:00
|
|
|
return call_user_func_array('\Translator\translate', func_get_args());
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
2013-04-13 03:08:55 +02:00
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function c()
|
|
|
|
{
|
2014-03-17 02:56:43 +01:00
|
|
|
return call_user_func_array('\Translator\currency', func_get_args());
|
2013-05-26 19:05:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function n()
|
|
|
|
{
|
2014-03-17 02:56:43 +01:00
|
|
|
return call_user_func_array('\Translator\number', func_get_args());
|
2013-05-26 19:05:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function dt()
|
|
|
|
{
|
2014-03-17 02:56:43 +01:00
|
|
|
return call_user_func_array('\Translator\datetime', func_get_args());
|
2013-04-13 03:08:55 +02:00
|
|
|
}
|
2014-03-17 02:56:43 +01:00
|
|
|
}
|