Translate Angular >=4 with ngx-translate and multiple modules
----------------------------------------
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).
Translate Angular >=4 with ngx-translate and multiple modules的更多相关文章
- Angular中 build的时候遇到的错误--There are multiple modules with names that only differ in casing
今天早上遇到一个Angular的编译的时候的错误 具体信息: There are multiple modules with names that only differ in casing.This ...
- There are multiple modules with names that only differ in casing. 黄色warning
There are multiple modules with names that only differ in casing.有多个模块同名仅大小写不同This can lead to unexp ...
- vue项目警告There are multiple modules with names that only differ in casing
执行npm run dev后出现了警告提示: warning in ./src/components/Public/yearSelectCell.vue There are multiple modu ...
- There are multiple modules with names that only differ in casing.
client?4c0e:153 ./src/components/Paginate.vue There are multiple modules with names that only differ ...
- vue引入警告:There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these
在写vue项目的时候 当我使用 : import dataSource from '../overseaProduct/house/dataSource'; 引入dataSource文件的时候:控制台 ...
- There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
There are multiple modules with names that only differ in casing.This can lead to unexpected behavio ...
- 项目警告:There are multiple modules with names that only differ in casing.This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.Use equal casing. Compare these modul
记录个自己遇到的问题: 上星期项目刚拉取下来的时候运行没有任何警告,晚上回去vscode提示更新新的东西,当时没管就立即更新了,第二天重启项目直接一大堆警告冒了出来: There are multip ...
- vue报错There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these mod
今天在开发一个新项目时,当安装完依赖包启动项目后报了一个这个错 There are multiple modules with names that only differ in casing.Thi ...
- 多个文件名大小写不同,是因为运行代码是大写E,用vscode运行的是小写e,解决方案:手动npm run dev #There are multiple modules with names that only differ in casing.
多个文件名大小写不同,是因为运行代码是大写E,用vscode运行的是小写e,解决方案:手动npm run dev #There are multiple modules with names that ...
随机推荐
- Python自用笔记
函数:raw_input()和input() 注意:在python3.x中,已经删除raw_input(),取而代之的是input(),当然这仅仅是重命名,用法还是一样.因此在这里介绍的是python ...
- 03. Pandas 2| 时间序列
1.时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() 日期解析方法:pa ...
- IDEA设置
一:代码提示 二:自动导入
- LoadRunner中 host-mapping的Capture Level说明
lr录制后空白,那么就要弄明白lr中host-mapping的Capture Level选项socket level data.winnet level data.socket level andwi ...
- Badboy录制Jmter脚本
提纲 1.特性和用途 2.下载和安装 3.界面介绍 4.录制脚本(注意:badboy默认是打开就开始录制,需要在step双击后进行取消默认设置) 5.添加断言(参数化设置,注意:badboy默认只运行 ...
- day 39 mycql 数据库之约束
egon笔记: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 UNIQUE KEY (UK) 标识该字段的值是唯一的 AUTO_INCREMENT 标识该字段的值自动增 ...
- 利用log4添加log
應用log4net.dll 新建Global.asax,在cs文件中添加 protected void Application_Start(object sender, EventArgs e) ...
- datetime.timedelta类
datetime.timedelta对象代表两个时间之间的时间差,两个date或datetime对象相减就可以返回一个timedelta对象. Python中datetime模块中的timedelta ...
- HDU 1175 连连看 (DFS+剪枝)
<题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...
- react+typescript报错集锦<持续更新>
typescript报错集锦 错误:Import sources within a group must be alphabetized.tslint(ordered-imports) 原因:impo ...