// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"doctrine/dbal": "^3.9.4", // 3.9.4
"doctrine/doctrine-bundle": "^2.14", // 2.14.0
"doctrine/doctrine-migrations-bundle": "^3.4.2", // 3.4.2
"doctrine/orm": "^3.3.3", // 3.3.3
"league/commonmark": "^2.7", // 2.7.0
"symfony/asset": "7.2.*", // v7.2.0
"symfony/asset-mapper": "7.2.*", // v7.2.5
"symfony/console": "7.2.*", // v7.2.6
"symfony/crowdin-translation-provider": "7.2.*", // v7.2.0
"symfony/dotenv": "7.2.*", // v7.2.0
"symfony/flex": "^2.5.1", // v2.5.1
"symfony/framework-bundle": "7.2.*", // v7.2.5
"symfony/runtime": "7.2.*", // v7.2.3
"symfony/stimulus-bundle": "^2.25", // v2.25.2
"symfony/translation": "7.2.*", // v7.2.7
"symfony/twig-bundle": "7.2.*", // v7.2.0
"symfony/yaml": "7.2.*", // v7.2.6
"symfonycasts/tailwind-bundle": "^0.10.0", // v0.10.0
"twig/extra-bundle": "^2.12|^3.21", // v3.21.0
"twig/markdown-extra": "^3.21", // v3.21.0
"twig/string-extra": "^3.21", // v3.21.0
"twig/twig": "^2.12|^3.21.1" // v3.21.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^4.1", // 4.1.0
"phpunit/phpunit": "^9.6.23", // 9.6.23
"symfony/browser-kit": "7.2.*", // v7.2.4
"symfony/css-selector": "7.2.*", // v7.2.0
"symfony/debug-bundle": "7.2.*", // v7.2.0
"symfony/maker-bundle": "^1.63", // v1.63.0
"symfony/monolog-bundle": "^3.0", // v3.10.0
"symfony/phpunit-bridge": "^7.2.6", // v7.2.6
"symfony/stopwatch": "7.2.*", // v7.2.4
"symfony/web-profiler-bundle": "7.2.*", // v7.2.4
"zenstruck/browser": "^1.9.1", // v1.9.1
"zenstruck/foundry": "^2.5" // v2.5.0
}
}
4 Comments
Hello,
I really would like how to do the "automatic language switcher" from the users browsers information.
And I'd prefer to keep all languages prefixed.
So I just keep the
in the
routes.yamlfile?But how to set a route for
'/'now in my controller?!Thank you!
Sure, so when someone goes to
/, you want to detect their preferred language and redirect them to that languages homepage?Here's what I would do:
# config/routes.yaml controllers: resource: path: ../src/Controller/ namespace: App\Controller type: attribute trailing_slash_on_root: false prefix: en: /en fr: /fr es: /es # add this raw_homepage: path: / controller: App\Controller\RawHomepageController::homepageNow, create the
RawHomepageController:namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; final class RawHomepageController extends AbstractController { public function homepage( Request $request, #[Autowire(param: 'kernel.enabled_locales')] array $enabledLocales, #[Autowire(param: 'kernel.default_locale')] string $defaultLocale, ): Response { // find the user's locales that also are in our enabled locales $matches = array_intersect($request->getLanguages(), $enabledLocales); // use the first match - if none, use our default $locale = reset($matches) ?: $defaultLocale; return $this->redirectToRoute('app_homepage', ['_locale' => $locale]); } }Now,
/is no longer a 404 and you're redirected to/{locale}(or/enby default).If you have a bunch of controllers you don't want prefixed (maybe your admin section), you can use subdirectories in your
src/Controllerdirectory:# config/routes.yaml localized_controllers: resource: path: ../src/Controller/Localized/ namespace: App\Controller\Localized type: attribute trailing_slash_on_root: false prefix: en: /en fr: /fr es: /es unlocalized_controllers: resource: path: ../src/Controller/Unlocalized/ namespace: App\Controller\Unlocalized type: attributeNow, routes in
src/Controller/Localizedwill be prefixed, and ones insrc/Controller/Unlocalizedwill not.I hope this helps!
--Kevin
Wow! Thank you so very much Kevin❗️
this helped me a lot! ⛵️
by the way, call me a nitpicker:
In the transcription code example (3:02) the tick marks are missing. Don't they?
I'm glad this helped!
Haha good catch, you're right, I'm missing the quotes in the code block. It isn't technically required (it works just fine without), but YAML has made syntax changes in the past that require certain text to be wrapped in quotes. Best to use quotes when possible.
"Houston: no signs of life"
Start the conversation!