Symfony framework translate text in the application based on the locale of the user. If the user's locale cannot be determined, then the default locale is used. This tutorial shows how to set default locale in Symfony 7 application.
The default locale can set by using default_locale
option under the framework
key:
framework:
default_locale: de
To test, create a translation file for default locale:
hello_world: Hallo Welt
In your controller, try to translate the message:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class TestController
{
#[Route('/')]
public function index(TranslatorInterface $translator): Response
{
return new Response($translator->trans('hello_world'));
}
}
Leave a Comment
Cancel reply