3

I am working on a website for a French customer. My translations.yml looks like this:

framework:
    default_locale: en
    translator:
        default_path: '%kernel.project_dir%/translations'
        fallbacks:
            - fr

I write everything in english, and then run this command:

php bin/console translation:update --force fr

It generates .xlf, files with these parameters:

source-language="en" target-language="fr"

It works as expected.

I would like to make the login page French as well. That page is visited, when the user is not logged in yet, so default_locale is used.

I changed default_locale to fr and fallbacks to en.

The login page is displayed in French as expected, but now the translation:update script is broken. If I run it, the resulting translation files have these incorrect parameters:

source-language="fr" target-language="fr"

How is it possible to use a different source language for translations and default locale for pages, where the user is not logged in? Or is it possible to use the locale from the user agent in that case?

yivi
  • 42,438
  • 18
  • 116
  • 138
Iter Ator
  • 8,226
  • 20
  • 73
  • 164

1 Answers1

0

If you use locale in URL (see doc), you can force the parameter locale like this in your PHP code:

/**
 * Login page in French by default
 * Except if the user specifies the locale in the URL
 * 
 * @Route("/", name="login_default", locale="fr")
 * @Route("/{_locale}", name="login_default_lang")
 */
public function index()
{
   return $this->render('login_page.html');
}  

For other pages in English by default:

/**
 * Other page in English by default
 * Except if the user specifies the locale in the URL
 * 
 * @Route("/", name="other_default", locale="en")
 * @Route("/{_locale}", name="other_default_lang")
 */
public function index()
{
   return $this->render('other_page.html');
}  
Liscare
  • 336
  • 2
  • 14