localizationsulu

Sulu CMS - Change the default locale provider


I need to open the language version of the site according to the browser language and IP address. I'm trying to use my Locale Provider as suggested in the documentation.I copy the RequestDefaultLocaleProvider.php to src folder, rename and registered it as a service in services.yaml and insert his ID to sulu_website.yaml. When I open the site method "getDefaultLocale" in my provider is not called and I can't open the desired version of the site. What am i doing wrong?

services.yaml

app.locale.my_locale_provider:
class: App\Locale\MyLocaleProvider
arguments:
    - '@Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface'
    - '@Symfony\Component\HttpFoundation\RequestStack'

sulu_website.yaml

sulu_website:
twig:
    attributes:
        urls: false
        path: false
default_locale:
    provider_service_id: app.locale.my_locale_provider

MyLocaleProvider.php

namespace App\Locale;
use Sulu\Bundle\WebsiteBundle\Locale\DefaultLocaleProviderInterface;

class MyLocaleProvider implements DefaultLocaleProviderInterface
{       
    public function __construct()
    {       
        dump("from constructor") ;       
    }

    public function getDefaultLocale()
    {   
        dump("from getDefaultLocale") ;
        return 'de';
        
    }
}

I also tried to use the recommended approach Symfony (subscribe to the onKernelRequest event and set the language I need in $request->setLocale() method but this also did not work)


Solution

  • The default locale provider is only taken into account when you are using the following url schema in your webspace:

    <url>{host}/{localization}</url>
    

    So if you have e.g. sulu.rocks/en and sulu.rocks/de and somebody is calling sulu.rocks/ the default locale provider is called and redirect then the user provided language.

    If you are running a locale on the rootLevel of your webspace, the default locale provider will not be called as it is alrea