本文介绍了Angular2 和 webpack - i18n 插件与 ng2-translate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 angular2 构建一个 web 应用程序并将其与 webpack 捆绑在一起.提供多种语言的最佳方式是什么:

I want to build a web-application with angular2 and bundle this with webpack.What is the best way for providing multiple languages:

i18n-plugin: https://github.com/webpack/i18n-webpack-plugin

i18n-plugin: https://github.com/webpack/i18n-webpack-plugin

ng2-translate:https://github.com/ocombe/ng2-translate

ng2-translate: https://github.com/ocombe/ng2-translate

推荐答案

我使用说明书让它与 webpack 一起工作.xliff 文件必须像这样转换为 ts:

I got it working with webpack using the cookbook. The xliff file has to be converted to ts like so:

export const TRANSLATION_SV = `<?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="a73e2898b9e1126ed19dbabe4b5c3715a84db61a" datatype="html">
      <source>Category</source>
      <target>Kategori</target>
    </trans-unit>
    </body>
  </file>
</xliff>`;

然后必须在 main.ts 中添加它

Then in the main.ts it has to be added

import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID  } from '@angular/core';
import { TRANSLATION_SV } from './locale/messages.sv';

并插入到引导步骤:

platformBrowserDynamic().bootstrapModule(AppModule, {
    providers: [
      {provide: TRANSLATIONS, useValue: TRANSLATION_SV},
      {provide: TRANSLATIONS_FORMAT, useValue: "xlf"},
      {provide: LOCALE_ID, useValue:'sv'}
    ];
});

这篇关于Angular2 和 webpack - i18n 插件与 ng2-translate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:21