Avoid creating multiple instances of Translator

This commit is contained in:
Frederic Guillot
2015-05-24 16:30:40 -04:00
parent eeac2329ba
commit 03fc8a1bce
2 changed files with 29 additions and 21 deletions

View File

@@ -26,6 +26,31 @@ class Translator
*/ */
private static $locales = array(); private static $locales = array();
/**
* Instance
*
* @static
* @access private
* @var Translator
*/
private static $instance = null;
/**
* Get instance
*
* @static
* @access public
* @return Translator
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
/** /**
* Get a translation * Get a translation
* *

View File

@@ -9,8 +9,7 @@ use Core\Translator;
*/ */
function t() function t()
{ {
$t = new Translator; return call_user_func_array(array(Translator::getInstance(), 'translate'), func_get_args());
return call_user_func_array(array($t, 'translate'), func_get_args());
} }
/** /**
@@ -20,19 +19,7 @@ function t()
*/ */
function e() function e()
{ {
$t = new Translator; return call_user_func_array(array(Translator::getInstance(), 'translateNoEscaping'), func_get_args());
return call_user_func_array(array($t, 'translateNoEscaping'), func_get_args());
}
/**
* Translate a currency
*
* @return string
*/
function c($value)
{
$t = new Translator;
return $t->currency($value);
} }
/** /**
@@ -42,8 +29,7 @@ function c($value)
*/ */
function n($value) function n($value)
{ {
$t = new Translator; return Translator::getInstance()->number($value);
return $t->number($value);
} }
/** /**
@@ -53,8 +39,7 @@ function n($value)
*/ */
function dt($format, $timestamp) function dt($format, $timestamp)
{ {
$t = new Translator; return Translator::getInstance()->datetime($format, $timestamp);
return $t->datetime($format, $timestamp);
} }
/** /**
@@ -67,5 +52,3 @@ function p($value, $t1, $t2)
{ {
return $value > 1 ? $t2 : $t1; return $value > 1 ? $t2 : $t1;
} }