首先,要配置好你的sonataadminbundle。
1.开启symfony的翻译功能
1 2 3 4 5 6 7 8 |
# app/config/config.yml parameters: locale: zh_CN framework: #esi: ~ translator: { fallbacks: ["%locale%"] } |
我们因为是中文网站,所以设置语言为中文。设置为中文后,sonata会自动加载中文翻译,但是还有一些我们自定义的字段需要指定翻译来完成。
2.开启label_translator_strategy
在你bundle下的services.xml中,找到你之前设置的News admin类,添加 label_translator_strategy=”sonata.admin.label.strategy.underscore”
1 2 3 4 5 6 7 8 9 10 11 12 |
<services> <service id="app.admin.news" class="AppBundle\Admin\NewsAdmin"> <tag name="sonata.admin" manager_type="orm" group="资讯管理" label="资讯" label_translator_strategy="sonata.admin.label.strategy.underscore" /> <argument /> <argument>AppBundle\Entity\News</argument> <argument>AppBundle:NewsAdmin</argument> <call method="setTranslationDomain"> <argument>AppBundle</argument> </call> </service> </services> |
label_translator_strategy有很多配置方法。label_translator_strategy=”sonata.admin.label.strategy.underscore”是sonata官方最为推荐的一种。
记住:别忘了 <call method=”setTranslationDomain”> <argument>AppBundle</argument> </call>
3.创建翻译文件
在bundle下创建文件AppBundle.zh_CN.xliff到Resources/translations目录:
AppBundle/Resources/translations/AppBundle.zh_CN.xliff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0"?> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <file source-language="en" datatype="plaintext" original="" > <body> <trans-unit id="breadcrumb.link_news_list"> <source>breadcrumb.link_news_list</source> <target>新闻列表页</target> </trans-unit> <trans-unit id="form.label_id"> <source>form.label_id</source> <target>id</target> </trans-unit> <trans-unit id="form.label_title"> <source>form.label_title</source> <target>新闻标题</target> </trans-unit> <trans-unit id="form.label_content"> <source>form.label_content</source> <target>新闻内容</target> </trans-unit> <trans-unit id="list.label_createtime"> <source>list.label_createtime</source> <target>创建时间</target> </trans-unit> </body> </file> </xliff> |
4.了解翻译字段标识
打开我的新闻admin页面你会看到还有一些没有翻译
对了,你明白了吧。list.label_imagepath就是我们trans-unit的id和source。
好了我们设置一下看看,在AppBundle.zh_CN.xliff文件中添加:
1 2 3 4 |
<trans-unit id="list.label_imagepath"> <source>list.label_imagepath</source> <target>图片</target> </trans-unit> |
看看效果
还不错哦!
总结描述:
SonataAdminBundle的翻译文件为SonataAdminBundle.[your_language].xliff保存在vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Resources/translations/SonataAdminBundle.[your_language].xliff目录下,如果你想修改默认的翻译,可以覆写文件在此目录中
1 |
app/Resource/SonataAdminBundle/translations/ |
完整路径是:
1 |
app/Resource/SonataAdminBundle/translations/SonataAdminBundle.[your_language].xliff |
如果你想翻译字段,你就需要在你的bundle中创建翻译文件了(就像上面介绍的)。例如:
1 |
src/YourAlias/YourNameOfBundle/Resources/translations/YourAliasYourNameOfBundle.[your_language].xliff |
如果你在admin类中
1 |
src/YourAlias/YourNameOfBundle/Admin/YourClassAdmin.php |
你想在你的用户模块下改变翻译,sonata使用FosUserBundle来完成,你需要继承文件FOSUserBundle.[your_language].yml:
1 |
app/Resource/FOSUSerBundle/translations/FOSUserBundle.[your_language].yml |
并让他类似于SonataUserBundle
你可以使用它来完成一天的美好工作!
如果你想定制的更加详细,为每一个字段都可以随意定制名称,例如可能有些字段重复了,但名称在不同的表里表示不同的含义,这怎么办:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * @param ListMapper $listMapper */ protected function configureListFields(ListMapper $listMapper) { $listMapper ->add('id') ->add('description', null, [ 'label' => '咨询需求' ]) ; } |
在configureListFields方法里,为字段添加 label配置选项。 以前的description字段名称为“描述”,我们定制后字段就变成“咨询需求”了