use Symfony\Component\HttpFoundation\Request; // Create a request object $request = Request::createFromGlobals(); // Get the user's preferred language $preferredLanguage = $request->getPreferredLanguage(); // Set the language as a session variable $request->getSession()->set('language', $preferredLanguage);
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Translation\Translator; // Create a request object $request = Request::createFromGlobals(); // Get the user's preferred language $preferredLanguage = $request->getPreferredLanguage(); // Create a translator object $translator = new Translator($preferredLanguage); // Define translations $translator->addResource('messages', 'hello.world', $preferredLanguage, 'Hello, world!'); // Translate the response message $translatedMessage = $translator->trans('hello.world'); // Create the response object $response = new Response($translatedMessage); // Send the response $response->send();In both of these examples, we are using the Symfony\Component\HttpFoundation\Request package library to retrieve the user's preferred language.