From 03fc8a1bce033cc8334f588f3c95baffa1291b54 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 24 May 2015 16:30:40 -0400 Subject: Avoid creating multiple instances of Translator --- app/Core/Translator.php | 25 +++++++++++++++++++++++++ app/functions.php | 25 ++++--------------------- 2 files changed, 29 insertions(+), 21 deletions(-) (limited to 'app') diff --git a/app/Core/Translator.php b/app/Core/Translator.php index 0f5a77cc..e3d19692 100644 --- a/app/Core/Translator.php +++ b/app/Core/Translator.php @@ -26,6 +26,31 @@ class Translator */ 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 * diff --git a/app/functions.php b/app/functions.php index 439375be..e4b38cdd 100644 --- a/app/functions.php +++ b/app/functions.php @@ -9,8 +9,7 @@ use Core\Translator; */ function t() { - $t = new Translator; - return call_user_func_array(array($t, 'translate'), func_get_args()); + return call_user_func_array(array(Translator::getInstance(), 'translate'), func_get_args()); } /** @@ -20,19 +19,7 @@ function t() */ function e() { - $t = new Translator; - 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); + return call_user_func_array(array(Translator::getInstance(), 'translateNoEscaping'), func_get_args()); } /** @@ -42,8 +29,7 @@ function c($value) */ function n($value) { - $t = new Translator; - return $t->number($value); + return Translator::getInstance()->number($value); } /** @@ -53,8 +39,7 @@ function n($value) */ function dt($format, $timestamp) { - $t = new Translator; - return $t->datetime($format, $timestamp); + return Translator::getInstance()->datetime($format, $timestamp); } /** @@ -67,5 +52,3 @@ function p($value, $t1, $t2) { return $value > 1 ? $t2 : $t1; } - - -- cgit v1.2.3