Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep BO from using two different translators in parallel #14966

Merged
merged 6 commits into from
Aug 13, 2019
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
23 changes: 19 additions & 4 deletions classes/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Localization\Locale;
use PrestaShopBundle\Translation\Loader\SqlTranslationLoader;
use PrestaShopBundle\Translation\TranslatorComponent as Translator;
Expand Down Expand Up @@ -362,18 +363,32 @@ public function updateCustomer(Customer $customer)
}

/**
* Returns a translator depending on service container availability and if the method
* is called by the installer or not.
*
* @param bool $isInstaller Set to true if the method is called by the installer
*
* @return Translator
*/
public function getTranslator()
public function getTranslator($isInstaller = false)
{
if (null !== $this->translator) {
return $this->translator;
}

$translator = $this->getTranslatorFromLocale($this->language->locale);
$this->translator = $translator;
$sfContainer = SymfonyContainer::getInstance();
matthieu-rolland marked this conversation as resolved.
Show resolved Hide resolved

return $translator;
if ($isInstaller || null === $sfContainer) {
// symfony's container isn't available in front office, so we load and configure the translator component
$this->translator = $this->getTranslatorFromLocale($this->language->locale);
eternoendless marked this conversation as resolved.
Show resolved Hide resolved
} else {
$this->translator = $sfContainer->get('translator');
// We need to set the locale here because in legacy BO pages, the translator is used
// before the TranslatorListener does its job of setting the locale according to the Request object
$this->translator->setLocale($this->language->locale);
eternoendless marked this conversation as resolved.
Show resolved Hide resolved
}

return $this->translator;
}

/**
Expand Down
1 change: 1 addition & 0 deletions classes/controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public function init()
if (null === $this->getContainer()) {
$this->container = $this->buildContainer();
}

$localeRepo = $this->get(self::SERVICE_LOCALE_REPOSITORY);
eternoendless marked this conversation as resolved.
Show resolved Hide resolved
$this->context->currentLocale = $localeRepo->getLocale(
$this->context->language->getLocale()
Expand Down
2 changes: 1 addition & 1 deletion install-dev/classes/controllerHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function __construct()
$this->session->lang ?: false
);

$this->translator = Context::getContext()->getTranslator();
$this->translator = Context::getContext()->getTranslator(true);

if (isset($this->session->lang)) {
$lang = $this->session->lang;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ services:
tags:
- {name: translation.loader, alias: db}

prestashop.translation.sql_loader:
class: PrestaShopBundle\Translation\Loader\SqlTranslationLoader
tags:
- {name: translation.loader, alias: db}

prestashop.translation.legacy_file_reader:
class: PrestaShopBundle\Translation\Loader\LegacyFileReader
arguments:
Expand Down