简体   繁体   English

ngx-translate与数据库中的值?

[英]ngx-translate with values in database?

I'm making an angular2 app using ngx-translate module 我正在使用ngx-translate模块制作angular2应用程序

the service use .json documents to find the translations 该服务使用.json文档来查找翻译

first... i noticed that the local json documents route are "/src/assets/i18n/" like the next image: 首先......我注意到本地json文档路由是“/ src / assets / i18n /”,如下图所示:

在此输入图像描述

so, the translate service use the default language to translate the diferents texts 因此,翻译服务使用默认语言来翻译差异文本

this is how my app.component.ts works: 这是我的app.component.ts的工作方式:

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


@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent implements OnInit {
  constructor(translate: TranslateService) {
    translate.addLangs(['es']);
    translate.setDefaultLang('es');

    const browserLang: string = translate.getBrowserLang();
    translate.use(browserLang.match(/en|es/) ? browserLang : 'es');
  }

  ngOnInit() {
  }

}

So the service use "en.json", "es.json" and "fr.json" to match the translation. 因此服务使用“en.json”,“es.json”和“fr.json”来匹配翻译。

So my question is 所以我的问题是

How to avoid the "json" documents and match the texts with the text from the database? 如何避免“json”文档并将文本与数据库中的文本进行匹配?

So the client never have to been rebuild to get the new translation updates, just adding to the database and automatically translating without the rebuild 因此,客户端永远不必重建以获得新的翻译更新,只需添加到数据库并自动翻译而无需重建

Ngx-translate has a method setTranslation that accepts the Lang key and a JSON object - you can load your JSON object from anywhere (eg: a database) and set them using this Ngx-translate有一个接受Lang键和JSON对象的方法setTranslation - 你可以从任何地方加载你的JSON对象(例如:数据库)并使用它来设置它们

setTranslation(lang: string, translations: Object, shouldMerge: boolean = false): Manually sets an object of translations for a given language, set shouldMerge to true if you want to append the translations instead of replacing them

Docs: https://github.com/ngx-translate/core/blob/master/README.md#api 文档: https//github.com/ngx-translate/core/blob/master/README.md#api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM