209 lines
6.5 KiB
PHP
209 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace KupShop\KupShopBundle\Util\Locale;
|
|
|
|
use KupShop\AdminBundle\Util\LegacyAdminCredentials;
|
|
use KupShop\KupShopBundle\Config;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\KupShopBundle\Util\Compat\SymfonyBridge;
|
|
use KupShop\KupShopBundle\Util\System\PathFinder;
|
|
use Symfony\Bridge\Monolog\Logger;
|
|
|
|
class PHPArrayTranslator
|
|
{
|
|
/**
|
|
* @var PathFinder
|
|
*/
|
|
private $pathFinder;
|
|
|
|
/**
|
|
* @var LanguageContext
|
|
*/
|
|
private $languageContext;
|
|
|
|
public function __construct(PathFinder $pathFinder, LanguageContext $languageContext)
|
|
{
|
|
$this->pathFinder = $pathFinder;
|
|
$this->languageContext = $languageContext;
|
|
}
|
|
|
|
public function getLanguage($admin)
|
|
{
|
|
if ($admin) {
|
|
return $this->getAdminLanguage();
|
|
} else {
|
|
return $this->getShopLanguage();
|
|
}
|
|
}
|
|
|
|
public function getShopLanguage()
|
|
{
|
|
return $this->languageContext->getActiveId();
|
|
}
|
|
|
|
public function getAdminLanguage()
|
|
{
|
|
global $cfg;
|
|
|
|
$isAdministration = $isAdministration ?? isAdministration();
|
|
$admin = null;
|
|
if ($isAdministration) {
|
|
$adminCredentials = ServiceContainer::getService(LegacyAdminCredentials::class);
|
|
$admin = $adminCredentials->getAdminById($adminCredentials->getAdminID());
|
|
}
|
|
|
|
$language = $isAdministration ? $admin['language'] ?? null : null;
|
|
|
|
return $language ?? $cfg['Lang']['language_admin'] ?? 'czech';
|
|
}
|
|
|
|
public function loadShopMainTranslations()
|
|
{
|
|
$txt_str = [];
|
|
|
|
$lng = $this->getShopLanguage();
|
|
|
|
// sestaveni jmena souboru
|
|
$translations = 'lang/lang.'.$lng.'.php';
|
|
|
|
// Load shared translations
|
|
$script = $this->pathFinder->engineWebPath($translations);
|
|
if (file_exists($script)) {
|
|
$this->loadTranslationFile($txt_str, $script);
|
|
} else {
|
|
$script = $this->pathFinder->engineWebPath('lang/lang.cs.php');
|
|
$this->loadTranslationFile($txt_str, $script);
|
|
}
|
|
|
|
// Load local overrides
|
|
$script = $this->pathFinder->shopPath($translations);
|
|
if (!isFunctionalTests() && file_exists($script)) {
|
|
$this->loadTranslationFile($txt_str, $script);
|
|
}
|
|
|
|
return $txt_str;
|
|
}
|
|
|
|
public function translate(&$txt_str, $key, $section = null, $silent = false, $isAdministration = null)
|
|
{
|
|
if (isAdministration()) {
|
|
if (empty($section)) {
|
|
$section = getVal('type');
|
|
}
|
|
|
|
if (empty($section)) {
|
|
$section = substr(getVal('s'), 0, -4);
|
|
}
|
|
} else {
|
|
if (empty($section)) {
|
|
$section = getVal('s');
|
|
}
|
|
}
|
|
|
|
if (empty($section)) {
|
|
throw new \Exception('Prázdná sekce překladu');
|
|
}
|
|
|
|
$isAdministration = $isAdministration ?? isAdministration();
|
|
if (empty($txt_str)) {
|
|
$txt_str = $this->loadShopMainTranslations();
|
|
$this->loadTranslations($txt_str, 'main', $isAdministration, $this->getLanguage($isAdministration));
|
|
}
|
|
|
|
// kdyz neexistuje sekce, zkusit load
|
|
if (!array_key_exists($section, $txt_str)) {
|
|
$translations = $this->loadTranslations($txt_str, $section, $isAdministration, $this->getLanguage($isAdministration));
|
|
}
|
|
|
|
// kdyz neexistuje text, poslat to do chyboveho hlaseni
|
|
if (!array_key_exists($section, $txt_str) || !array_key_exists($key, $txt_str[$section])) {
|
|
// zalogovat chybu
|
|
// $error = "MISSING LANGUAGE STRING: Lang=" . $cfg ['Lang'] ['language'] . " String=\$txt_str['" . $section . "']['" . $key . "']";
|
|
// logError ( __FILE__, __LINE__, $error, false );
|
|
|
|
if ($silent) {
|
|
return false;
|
|
}
|
|
|
|
if (!isDevelopment() && !isAdministration()) {
|
|
/** @var Logger $logger */
|
|
$logger = ServiceContainer::getService('logger');
|
|
$logger->warning("Missing php translation: \"{$key}\", category: \"{$section}\"", [
|
|
'key' => $key,
|
|
'category' => $section,
|
|
'language' => $this->getLanguage($isAdministration),
|
|
'url' => SymfonyBridge::getCurrentRequest()?->getUri() ?? getShopUniqueName(),
|
|
]);
|
|
}
|
|
|
|
return "{$section}:{$key}";
|
|
}
|
|
$str = $txt_str[$section][$key];
|
|
|
|
return $str;
|
|
}
|
|
|
|
public function loadTranslations(&$txt_str, $category, $admin, $lng)
|
|
{
|
|
if (is_array($txt_str) && array_key_exists($category, $txt_str)) {
|
|
return $txt_str;
|
|
}
|
|
|
|
// Translation path
|
|
$translations = "lang/{$lng}/{$category}.php";
|
|
|
|
// Load shared translation
|
|
if ($admin) {
|
|
$script = $this->pathFinder->adminPath($translations);
|
|
$bundleScript = loadBundlesAdmin($translations);
|
|
} else {
|
|
$script = $this->pathFinder->engineWebPath($translations);
|
|
$bundleScript = loadBundles('Resources/'.$translations);
|
|
}
|
|
|
|
if (file_exists($script)) {
|
|
$this->loadTranslationFile($txt_str, $script);
|
|
} else {
|
|
// Fallback to czech only in admin and if not found in bundle. On web it is better to know about missing translations
|
|
if ($lng != 'czech' && $admin && !$bundleScript) {
|
|
loadLanguage($category, 'czech');
|
|
} else {
|
|
$txt_str[$category] = [];
|
|
}
|
|
}
|
|
|
|
// Load from bundles
|
|
if ($bundleScript) {
|
|
$this->loadTranslationFile($txt_str, $bundleScript);
|
|
}
|
|
|
|
// Do not load translations from shop in tests
|
|
if (isFunctionalTests()) {
|
|
return $txt_str;
|
|
}
|
|
|
|
// Load shop overrides
|
|
if ($admin) {
|
|
// admin can be named differently (kupkolo _z), so use relative path
|
|
// => shop admin translations can only be used in admin
|
|
$script = Config::get()['Path']['admin'].$translations;
|
|
} else {
|
|
$script = $this->pathFinder->shopPath($translations);
|
|
}
|
|
if (file_exists($script)) {
|
|
$this->loadTranslationFile($txt_str, $script);
|
|
}
|
|
|
|
return $txt_str;
|
|
}
|
|
|
|
protected function loadTranslationFile(&$txt_str, $script)
|
|
{
|
|
// Keep it here!! Included files depends on it
|
|
global $cfg;
|
|
|
|
require $script;
|
|
}
|
|
}
|