Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/App/Traits/AppTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.8
* @since 2.9.9
*/

namespace Quantum\App\Traits;

use Quantum\Libraries\Logger\Factories\LoggerFactory;
use Quantum\Libraries\Lang\Exceptions\LangException;
use Quantum\Libraries\Lang\Factories\LangFactory;
use Quantum\Environment\Exceptions\EnvException;
use Quantum\Config\Exceptions\ConfigException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Environment\Environment;
use Quantum\Libraries\Lang\Lang;
use Quantum\Tracer\ErrorHandler;
use Quantum\Config\Config;
use Quantum\Loader\Loader;
use Quantum\Config\Config;
use Quantum\Loader\Setup;
use ReflectionException;
use Quantum\App\App;
Expand Down Expand Up @@ -169,7 +169,7 @@ protected function setupErrorHandler()
*/
protected function loadLanguage()
{
$lang = Lang::getInstance();
$lang = LangFactory::get();

if ($lang->isEnabled()) {
$lang->load();
Expand Down
9 changes: 4 additions & 5 deletions src/Libraries/Lang/Exceptions/LangException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.7
* @since 2.9.9
*/

namespace Quantum\Libraries\Lang\Exceptions;
Expand All @@ -24,19 +24,18 @@ class LangException extends BaseException
{

/**
* @param string $name
* @return LangException
*/
public static function translationsNotFound(string $name): LangException
public static function translationsNotFound(): LangException
{
return new static(t('exception.translation_files_not_found', $name), E_WARNING);
return new static('Translation files not found', E_WARNING);
}

/**
* @return LangException
*/
public static function misconfiguredDefaultConfig(): LangException
{
return new static(t('exception.misconfigured_lang_default_config'), E_WARNING);
return new static('Misconfigured lang default config', E_WARNING);
}
}
77 changes: 77 additions & 0 deletions src/Libraries/Lang/Factories/LangFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.9
*/

namespace Quantum\Libraries\Lang\Factories;

use Quantum\Libraries\Lang\Exceptions\LangException;
use Quantum\Config\Exceptions\ConfigException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Libraries\Lang\Translator;
use Quantum\Libraries\Lang\Lang;
use Quantum\Http\Request;
use Quantum\Loader\Setup;
use ReflectionException;

/**
* Class LangFactory
* @package Quantum\Libraries\Lang
*/
class LangFactory
{

/**
* @var Lang|null Cached Lang instance
*/
private static $instance = null;

/**
* @return Lang
* @throws ConfigException
* @throws LangException
* @throws DiException
* @throws ReflectionException
*/
public static function get(): Lang
{
if (self::$instance !== null) {
return self::$instance;
}

if (!config()->has('lang')) {
config()->import(new Setup('config', 'lang'));
}

$isEnabled = filter_var(config()->get('lang.enabled'), FILTER_VALIDATE_BOOLEAN);

$langSegmentIndex = (int)config()->get('lang.url_segment');

if (!empty(route_prefix()) && $langSegmentIndex == 1) {
$langSegmentIndex++;
}

$lang = Request::getSegment($langSegmentIndex);

if (empty($lang) || !in_array($lang, (array)config()->get('lang.supported'))) {
$lang = config()->get('lang.default');
}

if (!$lang) {
throw LangException::misconfiguredDefaultConfig();
}

$translator = new Translator($lang);

return self::$instance = new Lang($lang, $isEnabled, $translator);
}
}
26 changes: 21 additions & 5 deletions src/Libraries/Lang/Helpers/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,51 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.5
* @since 2.9.9
*/

use Quantum\Libraries\Lang\Lang;
use Quantum\Libraries\Lang\Exceptions\LangException;
use Quantum\Libraries\Lang\Factories\LangFactory;
use Quantum\Config\Exceptions\ConfigException;
use Quantum\Di\Exceptions\DiException;

/**
* Gets the current lang
* @return string|null
* @throws ConfigException
* @throws DiException
* @throws LangException
* @throws ReflectionException
*/
function current_lang(): ?string
{
return Lang::getInstance()->getLang();
return LangFactory::get()->getLang();
}

/**
* Gets translation
* @param string $key
* @param $params
* @return string|null
* @throws ConfigException
* @throws ReflectionException
* @throws DiException
* @throws LangException
*/
function t(string $key, $params = null): ?string
{
return Lang::getInstance()->getTranslation($key, $params);
return LangFactory::get()->getTranslation($key, $params);
}

/**
* Outputs the translation
* @param string $key
* @param mixed $params
* @param $params
* @return void
* @throws ConfigException
* @throws DiException
* @throws LangException
* @throws ReflectionException
*/
function _t(string $key, $params = null)
{
Expand Down
Loading