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 ...
随机推荐
- css3一道闪光
<style type="text/css"> .overimg{ position: relative; display: block; /* overflow: h ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第二集之新建虚拟机】
1, 2, 3, 4,(如果选择版本时,发现选项中没有centos,可以选择other linux2.6.x kernel) 5,(虚拟机命名:Centos_用途_IP) 6,(选择Split vir ...
- Azkaban
Azkaban安装部署 https://azkaban.github.io/azkaban/docs/2.5/ 安装Azkaban ) 在/opt/module/目录下创建azkaban目录 [kri ...
- JavaSE| 流程控制
程序流程控制 流程控制语句结构: .顺序结构 语句的关系是从上到下依次执行的顺序关系,中间没有任何判断和跳转: 它是最基本的结构,Java程序总体来说都是从main()依次执行 .分支结构: 根据条件 ...
- Python中type和object
type 所有类是type生成的 a = 1 b = "abc" print("type a:{}".format(type(a))) print(" ...
- Spring(四)使用注解注入Bean
注解简单介绍 是代码里面的特殊标记,使用注解完成功能. 注解写法@ 注解名称(属性名=属性值). 注解可以作用在类.方法.属性上面. 使用流程: 在ApplicationContext.xml中开启注 ...
- Sleep,Hibernate and Hybrid
Sleep is a power-saving state that allows a computer to quickly resume full-power operation (typical ...
- ContextRefreshedEvent事件使用注意事项(Spring)
0 概述ContextRefreshedEvent 事件会在Spring容器初始化完成会触发该事件.我们在实际工作也可以能会监听该事件去做一些事情,但是有时候使用不当也会带来一些问题. 1 防止重复触 ...
- 《重回耶路撒冷——犹太人的三千年》(Return to Jerusalem)读后感
写在前面 书名:<重回耶路撒冷——犹太人的三千年>(Return to Jerusalem) 作者:张力升 来源:长清图书馆 阅读用时:其实年前拿到书,本来想寒假在家看的,但是在家一点儿都 ...
- PXE无人值守安装
简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作 ...