当前位置: 首页 > news >正文

[Angular] Import TranslateModule in Angular 16

1.Background

Angular 更新至V16版后,支援 standalone,故移除了 NgModule,而TranslateModule 又要在AppModule中 import,那该如何做呢?

2.NPM packages installation

npm install @ngx-translate/core
npm install @ngx-translate/http-loade

3.Import TranslateModule in bootstrapApplication of main.ts

// import ngx-translate and the http loader
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule } from '@angular/common/http';// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {return new TranslateHttpLoader(http);
}bootstrapApplication(AppComponent, {providers: [importProvidersFrom(NzMessageServiceModule,NzDrawerServiceModule,NzModalModule,TranslateModule.forRoot({loader: {provide: TranslateLoader,useFactory: HttpLoaderFactory,deps: [HttpClient]}}))]
}).catch(err => console.error(err));

4. Create i18n files under assets folder.

For example: i18n\en.json, i18n\zh_CN.json, i18n\zh_TW.json

5. Add key value object in i18n files.

For example: en.json

{"test": {"title": "This is a test title."}
}

zh_CN.json

{"test": {"title": "这是一个测试标题."}
}

zh_TW.json

{"test": {"title": "這是一個測試標題."}
}

6. Import TranslateService service and TranslateModule module.

import { TranslateModule, TranslateService } from '@ngx-translate/core';...imports: [NzButtonModule, NzSelectModule, TranslateModule, CommonModule, FormsModule, NzCardModule, NzFormModule, NzInputModule]...constructor(private translate: TranslateService) {this.translate.setDefaultLang('en');this.translate.use('en');this.selectedLang = 'en';}

7. Using in HTML

<h1>{{ 'test.title' | translate }}</h1>

8. Using in component

const lang = this.translate.instant(key);

9. Change language

changeLanguage(): void {let lang = this.translate.getDefaultLang();this.translate.setDefaultLang(this.selectedLang);this.translate.use(this.selectedLang);}

10. The full source code

import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzSelectModule } from 'ng-zorro-antd/select';@Component({selector: 'app-pages-sample-translate',template: `<br /><h1>{{ 'test.title' | translate }}</h1><br /><nz-select [(ngModel)]="selectedLang" (ngModelChange)="log($event)"><nz-option nzLabel="English" nzValue="en"></nz-option><nz-option nzLabel="简体中文" nzValue="zh_CN"></nz-option><nz-option nzLabel="繁體中文" nzValue="zh_TW"></nz-option></nz-select><button nz-button nzType="primary" (click)="changeLanguage()">{{ 'test.changelang' | translate }}</button><button nz-button (click)="getLang('test.changelang')">Get lang with Component</button>`,styles: [`nz-select {width: 200px;}`],changeDetection: ChangeDetectionStrategy.OnPush,standalone: true,providers: [],imports: [NzButtonModule, NzSelectModule, TranslateModule, CommonModule, FormsModule, NzCardModule, NzFormModule, NzInputModule]
})
export class TranslateComponent implements OnInit {selectedLang: any = {};constructor(private translate: TranslateService) {this.translate.setDefaultLang('en');this.translate.use('en');this.selectedLang = 'en';}ngOnInit(): void {}changeLanguage(): void {let lang = this.translate.getDefaultLang();this.translate.setDefaultLang(this.selectedLang);this.translate.use(this.selectedLang);}log(value: any): void {this.changeLanguage();}getLang(key: string): void {const lang = this.translate.instant(key);console.log(lang);}
}

11. Test result

http://www.lryc.cn/news/99744.html

相关文章:

  • Web自动化测试高级定位xpath
  • 2023河南萌新联赛第(二)场:河南工业大学 F - 最短距离
  • 前端文件上传实践与后端处理——文件分块上传
  • SFP6012A-ASEMI代理海矽美快恢复二极管参数、尺寸、规格
  • githack的安装步骤+一次错误体验
  • 【Spring框架】SpringBoot创建和使用
  • 【C语言项目】多臂井径电子测井成像项目(一)
  • 力扣 56. 合并区间
  • 前端开发Vue3.0 标签setup语法『UI组件库』之『模态框』【业务提升必备】
  • 在CSDN学Golang云原生(Kubernetes二开)
  • chatglm-6b量化推理指标记录
  • Android kotlin系列讲解之最佳的UI体验 - Material Design 实战
  • 链表基础知识
  • process.env.npm_config_argv的值3个参数remain、cooked、original什么含义
  • 【飞书】飞书导出md文档 | 飞书markdown文档导出 | 解决飞书只能导出pdf word
  • 零信任网络架构与实现技术的研究与思考
  • Unity 性能优化二:内存问题
  • JavaScript与TypeScript的区别
  • 【NetCore】05-使用Autofac增强容器能力
  • sparksql参数
  • STM32读写内部Flash
  • golang文件锁,目录锁,syscall包的使用
  • 数据库数据恢复-Syabse数据库存储页底层数据杂乱的数据恢复案例
  • 移远通信推出新一代高算力智能模组SG885G-WF,为工业和消费级IoT应用带来全新性能标杆
  • 微信小程序开发,小程序类目符合,线上版本无权限申请wx.getLocation接口
  • vue2企业级项目(五)
  • 【HTML5】拖放详解及实现案例
  • Codeforces Round 888 (Div. 3)(视频讲解全部题目)
  • MySQL之深入InnoDB存储引擎——物理文件
  • Jquery操作html常用函数