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

Fix back-office translations when multishop and multiple languages #28392

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion config/config.inc.php
Expand Up @@ -221,8 +221,30 @@
if (isset($cookie->id_lang) && $cookie->id_lang) {
$language = new Language($cookie->id_lang);
}
if (!isset($language) || !Validate::isLoadedObject($language) || !$language->isAssociatedToShop()) {

$isNotValidLanguage = !isset($language) || !Validate::isLoadedObject($language);
// `true` if language is defined from multishop or backoffice (`$employee` variable defined) session
$isLanguageDefinedFromSession = (isset($language) && $language->isAssociatedToShop()) || defined('_PS_ADMIN_DIR_');

$useDefaultLanguage = $isNotValidLanguage || !$isLanguageDefinedFromSession;
if ($useDefaultLanguage) {
Comment on lines +229 to +230
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$useDefaultLanguage = $isNotValidLanguage || !$isLanguageDefinedFromSession;
if ($useDefaultLanguage) {
if ($isNotValidLanguage || !$isLanguageDefinedFromSession) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the conditions are quite complex (double negation, OR with isNot on one side and Is on the other) splitting this into multiple lines with clear naming seems more human-readable to me


// Default value for most cases
$language = new Language(Configuration::get('PS_LANG_DEFAULT'));

// if `PS_LANG_DEFAULT` not a valid language for current shop then
// use first valid language of the shop as default language.
if($language->isMultishop() && !$language->isAssociatedToShop()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fallback is legit in the current context because we have inconsistent configuration, we allow to define a default language which can be different from the languages associated to the shop, hence this forced fallback

We need to open an issue to avoid this behaviour, in the language edition we should prevent unassociating the default language, thus forcing the user to change it accordingly before he unassociates it

$shopLanguages = $language->getLanguages(true, Context::getContext()->shop->id, false);

if(isset($shopLanguages[0]['id_lang'])) {
$shopDefaultLanguage = new Language($shopLanguages[0]['id_lang']);

if(Validate::isLoadedObject($language)) {
$language = $shopDefaultLanguage;
}
}
}
}

$context->language = $language;
Expand Down