跳至主要內容

多国语系设置

约 240 字

多国语系设置

读取浏览器设置语言

const lang = (localStorage.getItem('currentLanguage') 
    || window.navigator.language || '').includes('zh') ? 'zh' : 'en';

安装依赖

npm install @ngx-translate/core --save
npm install @ngx-translate/http-loader  --save

app.module.ts 导入

// 自定义的语言处理
import { HttpClientModule } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

// https://github.com/ngx-translate/core
export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
}

@NgModule({
  imports: [
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    })
  ]
});

添加自定义语言包

assets 文件夹下新建 i18n/*.json

zh-CN.json
{
	"delete": "删除",
	"generate compressed code": "生成压缩码",
	"download": "下载",
	"file": "文件"
}

app.component.ts 中初始化

import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  constructor(
    public translate: TranslateService
  ) {
    
  }

  async ngOnInit() {
    const lang = (
        localStorage.getItem('currentLanguage') || this.translate.getBrowserCultureLang() || ''
    ).includes('zh') ? 'zh' : 'en';
    
    switch(lang) {
      case 'zh':
        this.translate.use('zh-CN');
        break;
      case 'en':
        this.translate.use('en-US');
        break;
      default:
        this.translate.use('en-US');
        break;
    }
  }
}

使用多国语言

<a href="#">{{ 'download' | translate }}</a>

<a href="#" [title]="'name' | translate">title</a>
上次编辑于: