rainlab/translate-plugin

Language switcher has stopped working

ssi-com-pl opened this issue · 2 comments

Hi! I use this code to change language based on domain:

public function boot()
{

    \Cms\Classes\CmsController::extend(function($controller) {
        $controller->middleware(function($request, $next) {
            $translator = Translator::instance();
            $serverName = $request->server->get('SERVER_NAME');

            switch($serverName) {
                case 'mydomain.pl':
                    $translator->setLocale('pl');
                    break;
                case 'mydomain.eu':
                    $translator->setLocale('en');
                    break;
                case 'mydomain.com':
                    $translator->setLocale('en');
                    break;
                case 'mydomain.dk':
                    $translator->setLocale('en');
                    break;
                case 'mydomain.de':
                    $translator->setLocale('de');
                    break;
                }

            return $next($request);
        });
    });
}

Unfortunately my language switcher has stopped working:

<div class="langSwitcher">
    <a href="#" data-request="onSwitchLocale" data-request-data="locale: 'pl'"  class="{% if language == 'pl' %} active-lang {% endif %}">
        <img src="{{ 'assets/images/lang/pl.svg' |theme}}" width="25" height="19"><span>PL</span>
    </a>
    <a href="#" data-request="onSwitchLocale" data-request-data="locale: 'en'" class="{% if language == 'en' %} active-lang {% endif %}">
        <img src="{{ 'assets/images/lang/en.svg' |theme}}" width="25" height="19"><span>EN</span>
    </a>
    <a href="#" data-request="onSwitchLocale" data-request-data="locale: 'de'" class="{% if language == 'de' %} active-lang {% endif %}">
        <img src="{{ 'assets/images/lang/de.svg' |theme}}" width="25" height="19"><span>DE</span>
    </a>
</div>

How to make it work again?

The multisite feature handles this natively now, so it may be conflicting. Check these events that the multisite feature uses to set the locale internally. If you make an event with least priority (eg: -1) then it should always put your desired locale in place:

// Reset locale when active and edit sites change
Event::listen('system.site.setEditSite', function() {
    $this->activeLocale = ...;
});

Event::listen('system.site.setActiveSite', function() {
    $this->activeLocale = ...;
});

Hopefully it helps.

Sorry, forgot to mention I'm using October v2. How can I achieve this in October 2?