Stn*_*ire 29 internationalization webpack angular-universal angular
我尝试使用角度通用的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)
最后,我的client.ts文件如下所示:
./src/client.ts:
[...]
// i18n
import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';
import { TRANSLATION_FR } from '../locale/messages.fr';
import { MainModule } from './browser.module';
export const platformRef = platformUniversalDynamic();
// on document ready bootstrap Angular 2
export function main() {
return platformRef.bootstrapModule(MainModule, {
providers: [
{provide: TRANSLATIONS, useValue: TRANSLATION_FR},
{provide: TRANSLATIONS_FORMAT, useValue: "xlf"},
{provide: LOCALE_ID, useValue: 'fr'}
]
});
}
bootloader(main);
Run Code Online (Sandbox Code Playgroud)
这可以使"客户端"应用程序按预期工作." 关于 "被" A propos " 取代.但是,因为角度通用预渲染服务器端的页面使用表达文本在客户端引导完成之前不会被翻译.
因此,当您第一次进入页面时,您会看到" 关于 "大约1秒钟,然后客户端才会用" A propos " 替换它.
解决方案似乎显而易见,只需在服务器端运行翻译服务!但我不知道该怎么做.
我的server.ts看起来像这样:
./src/server.ts
[...]
// i18n
import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';
import { TRANSLATION_FR } from '../locale/messages.fr';
const app = express();
const ROOT = path.join(path.resolve(__dirname, '..', 'dist'));
// Express View
app.engine('.html', createEngine({
ngModule: MainModule,
providers: [
/**
* HERE IS THE IMPORTANT PART.
* I tried to declare providers but it has no effect.
*/
{provide: TRANSLATIONS, useValue: TRANSLATION_FR},
{provide: TRANSLATIONS_FORMAT, useValue: "xlf"},
{provide: LOCALE_ID, useValue: 'fr'}
]
}));
app.set('port', process.env.PORT || 3000);
app.set('views', ROOT);
app.set('view engine', 'html');
[...]
function ngApp(req, res) {
res.render('index', {
req,
res,
preboot: false,
baseUrl: '/',
requestUrl: req.originalUrl,
originUrl: `http://localhost:${ app.get('port') }`
});
}
app.get('*', ngApp);
// Server
let server = app.listen(app.get('port'), () => {
console.log(`Listening on: http://localhost:${server.address().port}`);
});
Run Code Online (Sandbox Code Playgroud)
我没有像客户端那样直接访问bootstrapModule方法.该供应商的"关键createEngine "参数对象是已经存在于原始server.ts代码.
我错过了什么?
解决方案是为每种语言预先构建包,并让代理检测哪个包作为默认包。
与 AOT 编译器合并 AOT(提前)编译器是生成小型、快速、可立即运行的应用程序包的构建过程的一部分。
使用 AOT 编译器进行国际化时,必须为每种语言预先构建单独的应用程序包,并根据服务器端语言检测或 url 参数提供适当的包。
您还需要指示 AOT 编译器使用您的翻译文件。为此,您可以在 ngserve 或 ngbuild 命令中使用三个选项:
--i18nFile:翻译文件的路径。--i18nFormat:翻译文件的格式。--locale:区域设置 ID。下面的示例显示了如何提供本指南前面部分中创建的法语文件:
Run Code Online (Sandbox Code Playgroud)ng build --aot --i18nFile=src/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr
归档时间: |
|
查看次数: |
2642 次 |
最近记录: |