Symfony2:默认语言环境未应用于翻译

max*_*ime 4 php locale translation symfony

我想用Symfony翻译我的网站.我在配置中激活了翻译器.

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  %locale%
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true


# app/config/parameters.yml
# This file is auto-generated during the composer install
parameters:
    ...
    locale: fr
    secret: MySecretToken
Run Code Online (Sandbox Code Playgroud)

我的翻译文件位于app/config/Resources/messages.en.yml和app/config/Resources/messages.fr.yml中.

但是当我在我的控制器中执行此操作时:

$translator = $this->get('translator');
var_dump($translator->trans('hello'));
exit();
Run Code Online (Sandbox Code Playgroud)

它仍显示"hello"而不是"Bonjour".我用\ Locale :: getDefault()检查了我的PHP语言环境,它是"en",我的Symfony语言环境是"fr"(由翻译器对象的getLocale()获得).

当我在操作开始时手动设置语言环境时,它可以工作,但是我有很多操作,我想为整个网站设置默认语言环境.我认为config.yml可以为我做到这一点,但似乎事实并非如此......你看到我的问题来自哪里?谢谢 !

Vic*_*sky 7

尝试在app/cachedir中清除应用缓存.这是第一次非常重要.

您还必须在路由器中添加区域设置

  • @maxime请不要忘记接受答案,如果它帮助您解决问题:) (2认同)
  • +1提醒我讨厌的chache (2认同)