diff --git a/Gruntfile.js b/Gruntfile.js index 0115618..64c4f42 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -70,6 +70,7 @@ module.exports = function (grunt) { 'resources/cache', 'resources/sessions', 'resources/database', + 'resources/lang/**/*', 'resources/templates/**/*', 'resources/schemas/**/*', 'static/**/*', diff --git a/app/Web/Lang.php b/app/Web/Lang.php new file mode 100644 index 0000000..9979f40 --- /dev/null +++ b/app/Web/Lang.php @@ -0,0 +1,105 @@ +lang = $lang; + $this->path = $path; + + } + + /** + * @return Lang + */ + public static function getInstance() + { + + if (self::$instance === null) { + self::$instance = self::build(); + } + + return self::$instance; + } + + /** + * @param string $lang + * @param string $path + * @return Lang + */ + public static function build($lang = self::DEFAULT_LANG, $path = null) + { + + if (strlen($lang) !== 2) { + $lang = strtolower(substr($lang, 0, 2)); + } + + self::$instance = new self($lang, $path); + + return self::$instance; + } + + + /** + * @param $key + * @param array $args + * @return string + */ + public function get($key, $args = []) + { + return $this->getString($key, $this->lang, $args); + } + + /** + * @param $key + * @param $lang + * @param $args + * @return string + */ + private function getString($key, $lang, $args) + { + + if (array_key_exists($lang, $this->cache)) { + $transDict = $this->cache[$lang]; + } elseif (file_exists($this->path . $lang . '.lang.php')) { + $transDict = include $this->path . $lang . '.lang.php'; + $this->cache[$lang] = $transDict; + } else { + $transDict = []; + } + + if (array_key_exists($key, $transDict)) { + return vsprintf($transDict[$key], $args); + } + + if ($lang !== self::DEFAULT_LANG) { + return $this->getString($key, self::DEFAULT_LANG, $args); + } + + return $key; + } +} \ No newline at end of file diff --git a/bootstrap/app.php b/bootstrap/app.php index 4e3b328..37eb4fe 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -1,6 +1,7 @@ null, 'password' => null, ], + 'lang' => 'en', ], require __DIR__ . '/../config.php'); if (!$config['displayErrorDetails']) { @@ -57,6 +59,12 @@ $container['database'] = function ($container) use (&$config) { return DB::getInstance(); }; +Lang::build($config['lang'], __DIR__. '/../resources/lang/'); + +$container['lang'] = function ($container) { + return Lang::getInstance(); +}; + $container['view'] = function ($container) use (&$config) { $view = new \Slim\Views\Twig(__DIR__ . '/../resources/templates', [ @@ -75,6 +83,7 @@ $container['view'] = function ($container) use (&$config) { $view->getEnvironment()->addGlobal('request', $container->get('request')); $view->getEnvironment()->addGlobal('alerts', Session::getAlert()); $view->getEnvironment()->addGlobal('session', Session::all()); + $view->getEnvironment()->addGlobal('lang', $container->get('lang')); $view->getEnvironment()->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION); return $view; }; diff --git a/resources/lang/en.lang.php b/resources/lang/en.lang.php new file mode 100644 index 0000000..25058db --- /dev/null +++ b/resources/lang/en.lang.php @@ -0,0 +1,5 @@ +