使用Angular创建翻译文件的最佳实践是什么?

huangapple go评论106阅读模式
英文:

what is the best practice for creating translation files with angular?

问题

我正在使用Angular进行前端项目开发。我需要实现翻译功能。我想知道创建翻译文件的最佳实践是什么。我考虑为每个组件创建一个翻译文件,而不是每种语言一个文件。

我还没有尝试过任何方法,只是想了解。

英文:

i'am working on a front-end project with angular. I need to implement translation.
I want to know what is the best practice to create translation files. i was thinking about creating a translation file for every component instead of a file per language.

i haven't tried anything i just want to know.

答案1

得分: 1

在这里你可以找到最佳解决方案 https://github.com/ngx-translate/core

安装模块和依赖项

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

在appModule中导入模块

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {AppComponent} from './app';

// AoT需要为工厂导出一个函数
export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http);
}

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient]
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

以下是如何使用TranslateHttpLoader从"/assets/i18n/[lang].json"加载翻译([lang]是你使用的语言,例如英语为en):

在HTML中使用管道:

<li>{{ 'generic.label.hello.world' | translate }}</li>

如果在/assets/i18n/en.json中有以下内容:

{
 "generic.label.hello.world": "Hello World"
}

应该可以工作。

英文:

Here you can find the best solution https://github.com/ngx-translate/core

Install the module and pendencies

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

Import the module on appModule

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {AppComponent} from './app';

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http);
}

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient]
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Here is how you would use the TranslateHttpLoader to load translations from "/assets/i18n/[lang].json" ([lang] is the lang that you're using, for english it could be en):

On html just use the pipe:

<li>{{ 'generic.label.hello.world' | translate }}</li>

if you have in your /assets/i18n/en.json

{
 "generic.label.hello.world": "Hello World"
}

It sould work

huangapple
  • 本文由 发表于 2023年3月8日 17:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定