【问题标题】:symfony set local language from code - phpsymfony 从代码中设置本地语言 - php
【发布时间】:2021-11-12 11:27:33
【问题描述】:

我正在使用 PHP Symfony,并且我有一个多语言网站,为了测试一些我想从代码中设置网站语言的东西,无论用户在前端选择什么,我都这样做了:

    $request->setLocale('en');
    $request->getSession()->set('_locale', 'en');
    var_dump($request->getLocale());die;

我收到en 但这仍然不会改变我整个网站的语言。我尝试以用户身份切换语言并且有效,但我希望始终保持在代码中设置的英语,有什么建议吗?

【问题讨论】:

    标签: php symfony translation-scheme


    【解决方案1】:

    首先,您是否在配置中正确定义了语言? 例如这是我的配置:

    config/services.yaml:(用于默认值)

    parameters:
        locale: 'en'
        app_locales: en|fr #|de|es|pt|
    
    services:
        # default configuration for services in *this* file
        _defaults:
            autowire: true
            autoconfigure: true
            bind:
                $locale: "@=service('request_stack').getCurrentRequest().getLocale()"
                $defaultLocale: "%locale%"
                $listLocales: '%app_locales%'
    

    config/packages/translation.yaml

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

    config/packages/translation.yaml

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

    也许它对你没用,但是从 Symfony 4 开始,你可以在https://symfony.com/doc/current/routing.html#localized-routes-i18n 之后像这样在路由中强制区域设置:

    controllers:
        resource: ../../src/Controller/
        type: annotation
        prefix:
            en: /
            fr: /{_locale}
            de: /{_locale}
            es: /{_locale}
            pt: /{_locale}
        requirements:
            _locale: '%app_locales%'
        defaults:
            _locale: '%locale%'
    

    如果你想强制使用英语,你可以创建一个 EventSubscriber 强制使用英语: services.yaml:

        App\EventListerner\LocaleSubscriber:
            arguments: ['%kernel.default_locale%']
            tags: [kernel.event_subscriber]
    

    应用\事件监听器

    <?php
    
    namespace App\EventListerner;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpKernel\Event\RequestEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    
    /**
     * Set locale into session
     * Class LocaleSubscriber
     * @package App\EventSubscriber
     */
    class LocaleSubscriber implements EventSubscriberInterface
    {
    
        private string $defaultLocale;
    
        /**
         * LocaleSubscriber constructor.
         * @param string $defaultLocale
         */
        public function __construct(string $defaultLocale = 'en')
        {
            $this->defaultLocale = $defaultLocale;
        }
    
        /**
         * @param RequestEvent $event
         */
        public function onKernelRequest(RequestEvent $event): void
        {
            /** @var Request $request */
            $request = $event->getRequest();
    
            if (!$request->hasPreviousSession()) {
                return;
            }
    
            // Change here to force in english
            if ($locale = $request->attributes->get('_locale')) {
                $request->getSession()->set('_locale', $locale);
            } else {
                // if no explicit locale has been set on this request, use one from the session
                $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
            }
        }
    
        /**
         * @return array
         */
        public static function getSubscribedEvents(): array
        {
            return [
                KernelEvents::REQUEST => [
                    ['onKernelRequest', 20]
                ]
            ];
        }
    }
    
    

    【讨论】:

    • 如何从其他脚本和请求和语言参数中调用这个onKernelRequest函数?
    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多