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--装饰器2--理解
一.装饰无参函数①.原函数target为无参函数②.装饰函数的内置函数也必须要是无参函数③.运行原理相当于:target()=decorator(target)()---->最后的()相当于执行 ...
- 今天这篇内容分享Apache由http自动跳转到https的多种方法
本文主要和大家分享Apache http自动跳转到https的几种方法,非常不错,具有参考借鉴价值,需要的朋友参考下 本文主要和大家分享Apache http自动跳转到https的几种方法,当你的站点 ...
- Windows10系统网络连接问题
Thinkpad笔记本Windows10系统突然遇到网络连接问题: 问题:今天同事遇到一个WiFi连接问题,ThinkPad笔记本在公司和家里面都无法连接WiFi 寻找问题: 1.检查硬件问题 首先我 ...
- hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】
<题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...
- c#接口与抽象类的区别(一)
abstract 修饰符用于表示所修饰的类是不完整的,并且它只能用作基类.抽象类与非抽象类在以下方面是不同的: 抽象类不能直接实例化,并且对抽象类使用 new 运算符是编译时错误.虽然一些变量和值在编 ...
- vue(v-html)和scss的使用问题
<!--temp是一组p标签--> <div class="lyric-container" v-html="temp"></di ...
- orz gzy
然而orz gzy内嵌不进去...
- synchronized(){}同步代码块笔记(新手笔记,欢迎纠正)
/* 内容:同步代码块,目的是解决多线程中的安全问题.什么安全问题呢??就是在执行run方法时,假如线程-0刚刚获得执行权, *还没执行时,就挂那了,这时线程-1获得执行权,并进行执行,就有可能出现负 ...
- redis -- python操作连接redis简单示例
1.先安装 redis,pyredis sudo pip install redis sudo pip install python-redis 2.示例: importredis >>& ...
- 测试中,重现偶发的BUG问题。
1.严格按用例执行: 2.如果是作随机测试时,把测试步骤的点进行速记; 3.偶发BUG一般都是严重的,保留现场,让开发人员一起分析留下的现场(如数据的变化,界面窗口的变化等,找出问题的引子,那怕是千丝 ...