n00n
n00n

Reputation: 664

Change Browser language or project language to test localisation

this is my first time of developing with translation service.

I implemented it, seems to be working, but how can I test other languages?

Can I change the default language in my symfony project?

Or can I change the language transfered to my project via the browser? (I only found the settings to change browser GUI language)

Regards n00n

Upvotes: 0

Views: 467

Answers (2)

Ali Mhanna
Ali Mhanna

Reputation: 209

you can also do it inside controller using the session like this:

/**
 * Switch language
 *
 * @Route("/switchLanguage/{locale}/", name="switch_language")
 *
 * @param Request $request
 * @param string $locale
 *
 * @return RedirectResponse
 */
public function switchLanguageAction(Request $request, $locale): RedirectResponse
{
    $request->attributes->set('_locale', null);
    $this->get('session')->set('_locale', $locale);

    return $this->redirect($request->headers->get('referer'));
}

Upvotes: 1

Nairi Abgaryan
Nairi Abgaryan

Reputation: 666

Yes of course you can there is few approach for translation.

If you want save your project translation data in your database you must create entities for data translation and after that you can use global sql filter for selecting data each language.

And you can read about Symfony Translation Component.

For changing project language you can write listener.

like this

public function onKernelRequest(GetResponseEvent $event)
    {
        /** Set language parameter*/
        $lang = $event->getRequest()->query->get('lang', 'en'); //this is optional you can write another code for getting language.
        $event->getRequest()->setLocale($lang);
    }

This example if you want to set each request language. Read symfony translation component documentation there are more useful things.

Upvotes: 1

Related Questions