1、默认翻译语言设置
`app/config.yml`
~~~
framework:
translator: { fallbacks: ["%locale%"] }
default_locale: "%locale%"
~~~
2、翻译文件存放位置
~~~
app/Resources/translations/
app/Resources/<bundle name>/translations/
XxxBundle/Resources/translations/
~~~
3、翻译文件命名规则
`domain.locale.loader`
~~~
messages.en.yml
admin.fr.php
sonata.zh.xliff
~~~
4、翻译文件格式示例
~~~
<!-- messages.zh.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>Hello</source>
<target>你好</target>
</trans-unit>
</body>
</file>
</xliff>
~~~
~~~
# messages.zh.yml
Hello: 你好
~~~
~~~
// messages.zh.php
return array(
'Hello' => '你好',
);
~~~
5、翻译基本用法
Controller下:
`$this->get('translator')->trans($id, array $parameters = array(), $domain = null, $locale = null);`
~~~
use Symfony\Component\HttpFoundation\Response;
public function indexAction()
{
$sentence = 'xxxxxx……'.$name;
$translated = $this->get('translator')->trans($sentence);
return new Response($translated);
}
~~~
Twig下:
~~~
{% trans %}
{{ sentence }}
{% endtrans %}
~~~
~~~
{{ sentence|trans($parameters,$domain,$locale) }}
举例:
{{ sentence|trans({'%name%':'Lilei'},'messages','en') }}
~~~
6、翻译语言临时更换设置
`$this->get('translator')->setLocale($locale);`
- 安装
- 配置
- web服务器
- DependencyInjection
- Serializer
- Ajax
- Annotation
- Controller
- Entity
- Components【组件】
- HttpFoundation【HTTP处理】
- Routing【路由处理】
- Form【表单处理】
- Validator【验证处理】
- ClassLoader【类加载】
- Templating【模板】
- Security【处理安全问题】
- Translation【语言翻译】
- Bundles【功能包】
- Doctrine
- entityManager
- entityRespository
- createQueryBuilder
- createNativeQuery
- find
- DoctrineBundle
- doctrine_config
- DoctrineFixturesBundle
- KnpMenuBundle
- KnpPaginatorBundle
- FOSUserBundle
- FOSRestBundle
- FOSCommentBundle
- FOSElasticaBundle
- SonataCoreBundle
- SonataBlockBundle
- SonataMediaBundle
- SonataAdminBundle
- DoctrineORMAdminBundle
- Sonata
- Install_Sonata
- Config_Sonata
- BlockService
- Custom_BlockService
- Custom_Admin
- Problems