向 symfony 3 项目添加多语言选项
Adding multi language option to a symfony 3 project
阅读 symfony 3 的文档,我感到很困惑,我不确定我是否做对了所有事情。这是我的一个普通控制器在开始时的样子:
class IndexController extends Controller
{
/**
* @Route("/", name="index")
*/
public function indexAction(Request $request)
{
$articles = $this->getDoctrine()
->getRepository(Article::class)->findAll();
return $this->render("index.html.twig", array(
'articles' => $articles
));
}
}
我想添加多语言选项。在 symfony 之前,我看到通过简单地保存 session 语言和一个用于更改它的按钮来做到这一点。在 symfony 中,我添加了一个 translations 文件夹,并为每种语言添加了一个文件。
//messages.en.yml
base.menu.1: Home
base.menu.2: Products
base.menu.3: Brands
//messages.bg.yml
base.menu.1: Начало
base.menu.2: Продукти
base.menu.3: Марки
在此之后,我在一些标题中看到我可以像这样在我的路线中添加我的 _locale:
@Route("{_locale}/", name="index")
这确实奏效了。我可以通过进入 bg/ 或 en/ 来更改主页的语言。
但是语言环境变量没有保存在任何地方。如果我转到其他页面,它不知道之前设置的是什么语言。所以我阅读了更多,我是否可以通过将我的所有路线保存在 routing.yml 中来解决这个问题,如下所示:
index:
path: /{_locale}/
defaults: { _controller: AppBundle:Index:index }
requirements:
_locale: '%app.locales%'
然后设置我的配置:
parameters:
locale: bg
app.locales: bg|en
framework:
translator: { fallbacks: ['%locale%'] }
所有这一切都有效,除了我必须将我的路由从他们的控制器移动到 routing.yml。我想问一下这是否是正确的方法来完成所有这些,因为我不确定,文档不是 100% 清楚(很可能我只是无法理解),并且找不到任何好的标题.
无论是通过Annotation还是通过YAML中的配置,最终的结果都是一样的。如果您不小心,您将陷入 for/against 注释的争论,我会继续说我个人不推荐它们。您的 routing.yml
方法是我推荐的方法,应该工作得很好。
我相信您已经在 How to Work with the User's Locale and what you're looking for is Making the Locale "Sticky" during a User's Session 上阅读了 Symfony 的 post。
Symfony stores the locale setting in the Request, which means that this setting is not automtically saved ("sticky") across requests. But, you can store the locale in the session, so that it's used on subsequent requests.
使用'make locale sticky'方法,如上所述,
然后使用
设置语言环境
$request->setLocale($locale);
$request->getSession()->set('_locale', $locale);
//now redirect as the locale change will take affect on the next pageload
(同时设置并重定向)
阅读 symfony 3 的文档,我感到很困惑,我不确定我是否做对了所有事情。这是我的一个普通控制器在开始时的样子:
class IndexController extends Controller
{
/**
* @Route("/", name="index")
*/
public function indexAction(Request $request)
{
$articles = $this->getDoctrine()
->getRepository(Article::class)->findAll();
return $this->render("index.html.twig", array(
'articles' => $articles
));
}
}
我想添加多语言选项。在 symfony 之前,我看到通过简单地保存 session 语言和一个用于更改它的按钮来做到这一点。在 symfony 中,我添加了一个 translations 文件夹,并为每种语言添加了一个文件。
//messages.en.yml
base.menu.1: Home
base.menu.2: Products
base.menu.3: Brands
//messages.bg.yml
base.menu.1: Начало
base.menu.2: Продукти
base.menu.3: Марки
在此之后,我在一些标题中看到我可以像这样在我的路线中添加我的 _locale:
@Route("{_locale}/", name="index")
这确实奏效了。我可以通过进入 bg/ 或 en/ 来更改主页的语言。
但是语言环境变量没有保存在任何地方。如果我转到其他页面,它不知道之前设置的是什么语言。所以我阅读了更多,我是否可以通过将我的所有路线保存在 routing.yml 中来解决这个问题,如下所示:
index:
path: /{_locale}/
defaults: { _controller: AppBundle:Index:index }
requirements:
_locale: '%app.locales%'
然后设置我的配置:
parameters:
locale: bg
app.locales: bg|en
framework:
translator: { fallbacks: ['%locale%'] }
所有这一切都有效,除了我必须将我的路由从他们的控制器移动到 routing.yml。我想问一下这是否是正确的方法来完成所有这些,因为我不确定,文档不是 100% 清楚(很可能我只是无法理解),并且找不到任何好的标题.
无论是通过Annotation还是通过YAML中的配置,最终的结果都是一样的。如果您不小心,您将陷入 for/against 注释的争论,我会继续说我个人不推荐它们。您的 routing.yml
方法是我推荐的方法,应该工作得很好。
我相信您已经在 How to Work with the User's Locale and what you're looking for is Making the Locale "Sticky" during a User's Session 上阅读了 Symfony 的 post。
Symfony stores the locale setting in the Request, which means that this setting is not automtically saved ("sticky") across requests. But, you can store the locale in the session, so that it's used on subsequent requests.
使用'make locale sticky'方法,如上所述,
然后使用
设置语言环境$request->setLocale($locale);
$request->getSession()->set('_locale', $locale);
//now redirect as the locale change will take affect on the next pageload
(同时设置并重定向)