原文: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).

Translate Angular >=4 with ngx-translate and multiple modules的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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文件的时候:控制台 ...

  6. 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 ...

  7. 项目警告: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 ...

  8. 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 ...

  9. 多个文件名大小写不同,是因为运行代码是大写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 ...

随机推荐

  1. js防止安卓手机软键盘弹出挤压页面导致变形的方法

    5防止安卓手机软键盘弹出挤压页面导致变形的方法 输入框定位在底部,手机端打开,输入框聚焦后软键盘打开为什么会瞬间自动关闭呢? 先看看问题: 1.原来是这样的: 2.在苹果手机里面是正常的: 3.到了安 ...

  2. Cyclic Nacklace hdu3746 kmp 最小循环节

    题意:给出一段字符串  求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是:  如果nex[len]为0  那么答案显然是补len 否则  答案为循环 ...

  3. Windows 如何备份恢复Ubuntu系统文件

    之前安装的Win10 + Ubuntu18.04双系统,Ubuntu由于特殊原因崩了,所以打算重装系统 但是进不去Ubuntu系统,Ubuntu中的文件无法备份 试了win10的优盘启动工具中的DG, ...

  4. Nginx的配置安装和使用

    http://blog.csdn.net/e421083458/article/details/30086413 以后继续更新

  5. Python并发复习2 - 多线程模块threading

    一.多线程的调用 threading 模块建立在thread 模块之上.thread模块以低级.原始的方式来处理和控制线程,而threading 模块通过对thread进行二次封装, 提供了更方便的a ...

  6. FTP传输协议的应用详解

    FTP的目标:1)促进程序.数据文件按的共享;2)鼓励使用远程计算机;3)使用户不必面对不同主机上不同文件系统的差异;4)对数据进行高效可靠的传输FTP的作用:就是让用户连接上一个远程计算机,察看远程 ...

  7. C#导出Excel时间格式问题

    Range range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[RowCount + 1, ColCount]);ra ...

  8. UVA 2519 Radar Installtion

    思路: #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> ...

  9. LR逻辑回归文章

    http://blog.csdn.net/suipingsp/article/details/41822313

  10. Java并发编程(十二)-- 阻塞队列

    在介绍Java的阻塞队列之前,我们简单介绍一下队列. 队列 队列是一种数据结构.它有两个基本操作:在队列尾部加人一个元素,和从队列头部移除一个元素就是说,队列以一种先进先出的方式管理数据,如果你试图向 ...