我们决定给它一个旋转,我们开始使用Angular2进行新项目.到目前为止一直很好,但此时我们正面临着一个问题.在这一点上,对于Angular2,i18n的正确方法是什么?我们研究了一下,发现了这个:
然而,最后一次提交已超过5个月......看起来不像是积极的开发.
有人试过使用angular-translate或angular-gettext吗?或者也许使用Angular2最好包装像i18next这样的JS?有谁可以分享他们的想法?也许你遇到了同样的问题?
我尝试使用角度通用的Angular官方国际化工具.到目前为止,我能够使用以下过程翻译客户端渲染(感谢这个答案/sf/answers/2865107731/):
我在模板中的文档中添加了"i18n"属性:
./src/+app/about/about.component.html:
<h1 i18n="H1 of the about component">About</h1>
...
Run Code Online (Sandbox Code Playgroud)
然后我跑:
./node_modules/.bin/ng-xi18n
Run Code Online (Sandbox Code Playgroud)
生成基本messages.xlf文件.
然后,我将每个要支持的语言环境的文件复制为"locale"文件夹中的" messages.[locale] .xlf ".准备好后,我为每个包含其内容的导出字符串的xlf文件创建" messages.[locale] .ts ":
./locale/messages.fr.ts:
// TRANSLATION_FR is only for "messages.fr.ts" of course.
// I would create a TRANSLATION_ES const inside "messages.es.ts" for spanish for example.
export const TRANSLATION_FR: string = `<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="004b222ff9ef9dd4771b777950ca1d0e4cd4348a" datatype="html">
<source>About</source>
<target>A propos</target>
<note priority="1" from="description">H1 of the about component</note>
</trans-unit>
</body>
</file>
</xliff> …
Run Code Online (Sandbox Code Playgroud)