好好爱自己!

Translate Angular >=4 with ngx-translate and multiple modules

原文:https://medium.com/@lopesgon/translate-angular-4-with-ngx-translate-and-multiple-modules-7d9f0252f139

----------------------------------------

I wanted to do a short story to briefly explain to those who are not familiar with ngx-translate in Angular 4 applications, as I was few minutes ago and managed to fix my issue.

Most of the time you only need to use the app.module.ts for a small application but usually, apps grow and new modules appear by the way. I will go to the point in a multiple module implementation of the ngx-translatelibrary.

Your app.module.ts should import:

import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

Then, you need export your loader function (insert your own path to your i18n assets):

export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

Once those done, simply add in NgModules Imports:

imports: [
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient],
}
})
],

Once your app.module.ts configured with ngx-translate, let’s implement it into your other modules. For this, I strongly recommend to use a shared.module as mentioned in ngx-translate repository, then you don’t need to worry about importing TranslateModule everytime. You only need to add this in your shared.module:

import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';

@NgModule({
exports: [
TranslateModule,
]
})

export class SharedModule { }

You are now ready! Just import your shared.module in your other modules:

import { NgModule } from '@angular/core';
import { SharedModule } from '../shared.module';

import { AnotherComponent } from './home.component';

@NgModule({
declarations: [
AnotherComponent
],
imports: [
SharedModule
]
})

export class AnotherModule { }

and add the TranslateService in the constructor of your module components, just like this:

import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'app-home',
templateUrl: './another.component.html',
})

export class AnotherComponent {
constructor(public translate: TranslateService) { }
}

Not a big deal nah? Strongly recommend to go through ngx-translate/corerepository which has a good documentation (this story is a simple app real example of the implementation).

posted @   立志做一个好的程序员  阅读(465)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-04-25 Why is chkconfig no longer available in Ubuntu?
2018-04-25 drag-html
2017-04-25 【转】配置windows路由表,使电脑同时连接内网外网方法
2017-04-25 局域网
2017-04-25 javascript 閉包
2016-04-25 js中的 AOP
2016-04-25 javascript 中的闭包

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示