Symfony 4如何逐步更改语言 [英] Symfony 4 How to change language Step by Step

查看:102
本文介绍了Symfony 4如何逐步更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解页面翻译机制.我保存在yml文件中的特定语言的翻译可以通过两种方式获得:

I can not understand the page translation mechanism. Translations for a specific language I keep in the yml file Translation can be obtained in two ways:

  1. 在会话中保留当前选择的翻译

如何更改我的语言?该文档甚至为您提供了现成的代码 https://symfony.com/doc/current/session/locale_sticky_session.html

How can I change my language? The documentation even gives you a ready code https://symfony.com/doc/current/session/locale_sticky_session.html

仅在什么时候更改语言?我创建了一个单独的控制器,使用GET将参数传递给该控制器,然后在该控制器中执行代码?

Only at what point does the language change? I create a separate controller to which I pass a parameter using GET and execute the code in the controller?

$lang = $request->query->get('lang', null);
if(!is_null($lang)) { 
    request->setLocale($lang);
}
return $this->redirect($this->generateUrl('index.index'));

为了不在任何控制器中编写相同的代码,我应该有一个仅更改语言并将页面返回到前一个的控制器.仅通过重定向返回上一页不会使用新语言传递请求,只有文档中指定的类使用默认语言.

In order to not write the same code in any controller, I should have one controller that only changes the language and returns the page to the previous one. Only returning to the previous page via redirect does not pass the Request with a new language only the class given in the documentation uses the default language.

  1. 每次在链接中为页面提供翻译

我遇到的第一个问题是@Route("/")主页的默认路由不会通过localhost:8000/en/,因为Symfony将寻找en控制器,这是语言的选择,而不是控制器.解决方案是提供路径localhost:8000/index/en,但是目前我仅通过提供页面地址来阻止显示页面的可能性. @Route选项("/{_ locale}",默认值= {"_locale" ="en"})不起作用,我也不明白为什么,因为我总是必须指定pl/en等来显示语言

The first problem that I encounter is having the default routing for the @Route ("/") home page will not pass me localhost:8000/en/ because Symfony will look for the en controller, which is the choice of language, not the controller. The solution is to provide the path localhost:8000/index/en But at this moment I'm blocking the possibility of displaying the page by giving only the address of the page. The @Route option ("/{_ locale}", defaults = {"_locale" = "en"}) does not work and I do not understand why, because I always have to specify pl / en etc. to display the language.

阅读文档时,我应该执行以下操作:

When reading the documentation, I should do the following:

  1. 在config/packages/translation.yaml文件中的配置

default_locale:'en'<-指定默认语言 译者:〜 备用广告:['en']<-定义了当您通过_locale提供不正确的语言时将加载的语言

default_locale: 'en' <- specifies the default language translator: ~ fallbacks: ['en'] <- defines which language will be loaded if you provide an incorrect language via _locale

  1. 文件:config/services.yaml参数:语言环境:"en"
  2. config/translation.yaml文件

  1. File: config / services.yaml parameters: locale: 'en'
  2. The config / translation.yaml file

框架: default_locale:"zh-CN" 译者:〜

framework: default_locale: 'en' translator: ~

推荐答案

要使本地化对您的用户有效,您必须遵循以下三个教程: https://symfony.com/doc/current/translation.html https://symfony.com/doc/current/translation/locale.html https://symfony.com/doc/current/session/locale_sticky_session.html 第一个说明了如何设置翻译以及如何使用它们.第二篇解释了如何读取当前会话的语言环境,以及应使用_locale参数设置路由(因为前缀一开始是最佳选择).第三个教程介绍了如何使区域设置对用户会话保持粘性(这样,用户不必每次都在浏览器中键入正确的URL).所有这些都可以协同工作,而不是作为单独的选项.

To make localization work for your users you have to follow three tutorials: https://symfony.com/doc/current/translation.html https://symfony.com/doc/current/translation/locale.html and https://symfony.com/doc/current/session/locale_sticky_session.html The first one explains how to setup translations and how to use them. The second explains how to read the locale of the current session and that you should use _locale parameter to set your routes (as a prefix would be the best option in the beginning). The third tutorial explains how to make the locale sticky to user session (so that the user doesn't have to type the correct URL in the browser each time). All these work together, not as separate options.

这篇关于Symfony 4如何逐步更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆