Skip to content

Commit

Permalink
[5.7] Add configuration option to fallback to previous locale (#4388)
Browse files Browse the repository at this point in the history
* Add configuration option to fallback to previous locale

If the new configuration option 'concrete.multilingual.use_previous_locale' is
set to true, let's reuse the previously used locale.

* Ensure current locale selected in switch_language
  • Loading branch information
mlocati authored and aembler committed Sep 22, 2016
1 parent b423b7f commit d05b780
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
12 changes: 7 additions & 5 deletions web/concrete/blocks/switch_language/controller.php
Expand Up @@ -80,18 +80,20 @@ public function view()
$c = \Page::getCurrentPage();
$al = Section::getBySectionOfSite($c);
$languages = array();
$locale = \Localization::activeLocale();
if (is_object($al)) {
$locale = null;
if ($al) {
$locale = $al->getLanguage();
}
if (!$locale) {
$locale = \Localization::activeLocale();
$al = Section::getByLocale($locale);
}
foreach ($ml as $m) {
$languages[$m->getCollectionID()] = $m->getLanguageText($locale);
}
$this->set('languages', $languages);
$this->set('languageSections', $ml);
if (is_object($al)) {
$this->set('activeLanguage', $al->getCollectionID());
}
$this->set('activeLanguage', $al? $al->getCollectionID() : null);
$dl = \Core::make('multilingual/detector');
$this->set('defaultLocale', $dl->getPreferredSection());
$this->set('locale', $locale);
Expand Down
55 changes: 30 additions & 25 deletions web/concrete/src/Multilingual/Service/Detector.php
Expand Up @@ -71,32 +71,37 @@ public static function getPreferredSection()

public static function setupSiteInterfaceLocalization(Page $c = null)
{
if (\User::isLoggedIn() && Config::get('concrete.multilingual.keep_users_locale')) {
return;
}
if (!$c) {
$c = Page::getCurrentPage();
}
// don't translate dashboard pages
$dh = \Core::make('helper/concrete/dashboard');
if ($dh->inDashboard($c)) {
return;
}

$ms = Section::getBySectionOfSite($c);
if (!is_object($ms)) {
$ms = static::getPreferredSection();
}

if (!$ms) {
return;
}

$locale = $ms->getLocale();

if (strlen($locale)) {
\Localization::changeLocale($locale);
$loc = \Localization::getInstance();
if (!(\User::isLoggedIn() && Config::get('concrete.multilingual.keep_users_locale'))) {
if (!$c) {
$c = Page::getCurrentPage();
}
// don't translate dashboard pages
$dh = \Core::make('helper/concrete/dashboard');
if ($dh->inDashboard($c)) {
return;
}
$locale = null;
$ms = Section::getBySectionOfSite($c);
if ($ms) {
$locale = $ms->getLocale();
}
if (!$locale) {
if (Config::get('concrete.multilingual.use_previous_locale') && Session::has('previous_locale')) {
$locale = Session::get('previous_locale');
}
if (!$locale) {
$ms = static::getPreferredSection();
if ($ms) {
$locale = $ms->getLocale();
}
}
}
if ($locale) {
$loc->setLocale($locale);
}
}
Session::set('previous_locale', $loc->getLocale());
}

public static function isEnabled()
Expand Down

0 comments on commit d05b780

Please sign in to comment.