【发布时间】:2018-11-09 03:53:33
【问题描述】:
请建议我将 Angular 应用程序翻译成不同语言的实现。我也使用过 ngx-translate。但是想要一次翻译整个应用程序?
【问题讨论】:
请建议我将 Angular 应用程序翻译成不同语言的实现。我也使用过 ngx-translate。但是想要一次翻译整个应用程序?
【问题讨论】:
您可以使用角度 i18n 翻译技术
创建最小的 Angular4 项目 我们使用@angular/cli 在终端新建一个名为“traduction”的项目:
ng new traduction --prefix tr
cd traduction
ng serve -o
安装和加载 ngx-translate
在项目文件夹“traduction”中的终端中使用 npm:
npm install @ngx-translate/core --save
npm install @ngx-translate/http-loader
将必要的模块导入 app.module.ts :
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
添加一个函数,该函数返回一个“TranslateHttpLoader”并将其导出(AoT 需要)。在这种情况下,我们创建的 HttpLoaderFactory 函数返回一个可以使用 Http 和 .json 加载翻译的对象,但您可以编写自己的类,例如使用全局 JavaScript 变量而不是加载文件,或者使用谷歌翻译:
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
然后我们需要将我们的模块导入@NgModule,这是告诉Angular将此模块加载到您的AppModule中的导入:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [],
bootstrap: [AppComponent]
})
如果 Angular 使用关键字“加载”或“子模块”而不是“导入”,那会容易得多,因为一开始很容易将 ES2015 导入与 NgModule 导入混淆。
注入 TranslateService
在“app.component.ts”中,我们现在初始化“TranslateService”,我们导入 TranslateService:
import { TranslateService } from '@ngx-translate/core';
然后在 AppComponent 类中注入服务并定义我们的默认语言。为了为下一步做好准备,我们添加了一个切换语言的功能。
constructor(private translate: TranslateService) {
translate.setDefaultLang('en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
创建 .json 翻译文件
我们现在在 assets/i18n 文件夹中创建翻译文件:
src/assets/i18n/en.json
{
"Title": "Translation example",
"Intro": "Hello I am Arthur, I am 42 years old."
}
src/assets/i18n/fr.json
{
"Title": "Exemple de traduction",
"Intro": "Bonjour je m'appelle Arthur, j'ai 42 ans."
}
这些是简单的 .json 文件,将由我们在“app.module.ts”中创建的“TranslateHttpLoader”加载。
翻译简单的标题和介绍
在 app.component.html 中,我们在“h1”标签内添加一个带有翻译“指令”的标题。该指令将获取标签内的文本并将其替换为匹配的翻译。如果您使用该指令,您必须确保标签内除了文本之外没有其他内容。
作为第二个示例,我们使用“TranslationPipe”来翻译带有定义为内联字符串的标签。由于有时我们想要替换翻译中的值,因此我们可以将数据对象传递到“翻译”管道。
<h1 translate>Title</h1>
<div>
{{ 'Intro' | translate:user }}
</div>
集成语言切换器
我们现在可以将上面在 app.component.ts 中看到的语言切换器功能附加到按钮上。在这种情况下,我们为每种语言创建一个按钮,并使用匹配的语言键调用 switchLanguage() 函数。
<button (click)="switchLanguage('en')">en</button>
<button (click)="switchLanguage('fr')">fr</button>
用变量翻译句子
如前所述,您有时会有包含变量的句子。在这个小例子中,我们在“app.component.ts”中有一个带有年龄和姓名的用户对象,我们想要翻译一个包含这些值的句子:
...
export class AppComponent {
user = {
name: 'Arthur',
age: 42
};
...
}
因为我们将这个对象传递到“翻译”管道,我们现在可以在翻译中使用它的属性和 {{ placeholder }} 表示法。
src/assets/i18n/en.json
{
"Title": "Translation example",
"Intro": "Hello I am {{name}}, I am {{age}} years old."
}
src/assets/i18n/fr.json
{
"Title": "Exemple de traduction",
"Intro": "Bonjour je m'appelle {{name}}, j'ai {{age}} ans."
}
使用嵌套的 .json 对象
如果您希望能够对翻译有更多控制权,例如翻译页面块(从最终用户的角度)或组件(从开发人员的角度),一种解决方案可能如下;使用 git repo 中描述的嵌套 .json 对象。 -json 文件中的示例可能如下所示:
{
"Title": "Translation example",
"Intro": "Hello I am {{name}}, I am {{age}} years old.",
"Startpage": {
"TranslationSections": "Hello World"
},
"Aboutpage": {
"TranslationSections": "We are letsboot"
}
}
{
"Title": "Exemple de traduction",
"Intro": "Bonjour je m'appelle {{name}}, j'ai {{age}} ans.",
"Startpage": {
"TranslationSections": "Bonjour Monde"
},
"Aboutpage": {
"TranslationSections": "Nous sommes letsboot"
}
}
并在相应的模板中:
<h1 translate>Title</h1>
<div>
{{ 'Intro' | translate:user }}
</div>
<div>
{{ 'Startpage.TranslationSections' | translate }}
</div>
<div>
{{ 'Aboutpage.TranslationSections' | translate }}
</div>
<br/>
<button (click)="switchLanguage('en')">en</button>
<button (click)="switchLanguage('fr')">fr</button>
【讨论】: