Preparing for multilanguage support
This commit is contained in:
parent
cc68de1779
commit
0409a598d7
4 changed files with 120 additions and 0 deletions
|
@ -70,6 +70,7 @@ module.exports = function (grunt) {
|
|||
'resources/cache',
|
||||
'resources/sessions',
|
||||
'resources/database',
|
||||
'resources/lang/**/*',
|
||||
'resources/templates/**/*',
|
||||
'resources/schemas/**/*',
|
||||
'static/**/*',
|
||||
|
|
105
app/Web/Lang.php
Normal file
105
app/Web/Lang.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace App\Web;
|
||||
|
||||
|
||||
class Lang
|
||||
{
|
||||
|
||||
const DEFAULT_LANG = 'en';
|
||||
const LANG_PATH = __DIR__ . '/../../resources/lang/';
|
||||
|
||||
/** @var array */
|
||||
protected $cache = [];
|
||||
|
||||
/** @var string */
|
||||
protected $path;
|
||||
|
||||
/** @var string */
|
||||
protected $lang;
|
||||
|
||||
/** @var Lang */
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* Lang constructor.
|
||||
* @param $lang
|
||||
* @param $path
|
||||
*/
|
||||
public function __construct($lang, $path)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use App\Database\DB;
|
||||
use App\Web\Lang;
|
||||
use App\Web\Session;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
|
@ -27,6 +28,7 @@ $config = array_replace_recursive([
|
|||
'username' => 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;
|
||||
};
|
||||
|
|
5
resources/lang/en.lang.php
Normal file
5
resources/lang/en.lang.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
|
||||
];
|
Loading…
Reference in a new issue