0

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)

for_web
  • 3
  • 1

2 Answers2

0

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

Alexander Schranz
  • 2,154
  • 2
  • 25
  • 42
  • Thanks for the answer. Probably I have not fully explained my problem. In webspace I configure {host}/{language} and configure , and other . I need when calling sulu.rocks based on ip address, browser language and other rules redirect to sulu.rocks/{language}. Using default providers I could not implement it, so I decided to use my provider to implement the switching logic. – for_web Jun 02 '22 at 18:57
  • The included `sulu_website.default_locale.request_provider` should do it on the browsers language: https://docs.sulu.io/en/2.4/cookbook/change-default-locale-provider.html. The redirect is done here: https://github.com/sulu/sulu/blob/08863981d9e607dd1432db68eec2dac710732f74/src/Sulu/Bundle/WebsiteBundle/EventListener/RedirectExceptionSubscriber.php#L129 You maybe want to debug here: https://github.com/sulu/sulu/blob/08863981d9e607dd1432db68eec2dac710732f74/src/Sulu/Bundle/WebsiteBundle/EventListener/RedirectExceptionSubscriber.php#L117-L127 – Alexander Schranz Jun 03 '22 at 10:50
  • And maybe debug here: https://github.com/sulu/sulu/blob/6069d05f8efae4ad89c428adf4113dc326a0bf74/src/Sulu/Bundle/WebsiteBundle/Routing/ContentRouteProvider.php#L118-L126 – Alexander Schranz Jun 03 '22 at 10:50
  • And the webspace I did mean {localization} not {language} – Alexander Schranz Jun 03 '22 at 10:51
  • Thanks, I'll try to debug. The built-in request provider is not suitable because I need to impose additional rules on language definition, so I would like to use my locale provider. – for_web Jun 03 '22 at 11:59
0

The problem is solved, my mistake was that I expected the getDefaultLocale() method to be executed in my ISP, but a redirect to sulu.rocks/{language} was released and getDefaultLocale () was not executed there.

for_web
  • 3
  • 1