如何设置默认语言环境并将其从URL Symfony5中隐藏

时间:2020-06-26 08:43:40

标签: symfony

我有example.com网站,并希望对此进行解释。但我也希望默认语言是乌克兰语,因此url中没有语言环境参数。

url =>页面语言

example.com => ukrainian
example.com/ru => russian
example.com/en => english

example.com/add_new_post => ukrainian
example.com/ru/add_new_post => russian
example.com/en/add_new_post => english

example.com/ua必须将您重定向到example.com/example.com
example.com/ua/add_new_post必须将您重定向到example.com/add_new_post/example.com/add_new_post

我尝试像这样配置应用程序 app / config / services.yaml

parameters:
    locale: ua
    app_locales: '|ua|en|ru'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        bind:
            $locales: '%app_locales%'
            $defaultLocale: '%locale%'

app / config / packages / framework.yaml

framework:
    secret: '%env(APP_SECRET)%'
    default_locale: '%locale%'

app / config / packages / translation.yaml

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

app / config / routes / annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix: /{_locale}
    requirements:
        _locale: '%app_locales%'
    defaults:
        _locale: '%locale%'
        
kernel:
    resource: ../../src/Kernel.php
    type: annotation

但只能得到
example.com/ruexample.com/enexample.com/ru/add_new_postexample.com/ru/add_new_post的工作方式就像我想要的一样

example.comexample.com/ua在乌克兰语中是同一页面,
example.com/add_new_post显示未找到路由错误,
example.com//add_new_post使用乌克兰语。

0 个答案:

没有答案
相关问题