无法使用ngx转换/核心获得动态翻译的文本-angular 2打字稿 [英] Not able to get dynamic translation of text using ngx translate/core - angular 2 typescript

查看:92
本文介绍了无法使用ngx转换/核心获得动态翻译的文本-angular 2打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我有来自Json文件的动态文本. 我正在使用像这样的translate.get()方法:

I have dynamic text that is coming from Json file. I am using translate.get() method like this:

this.translate.get('keyInJson').subscribe(res => { 
                this.valueFromJson = res;
/*
creating an object using above text
*/
            });

由于这是异步的,因此在页面呈现时我无法获得翻译后的文本. 我尝试将上述方法包装在Observables,Promises中,但是在页面加载期间无法获取文本的翻译版本. 经过尝试不同的方法,我能够获得翻译后的文本,但是代码变得太复杂且不可靠.

As this is asynchronous I am not able to get the translated text when the page renders. I tried wrapping above method inside Observables , Promises but it's not able to get translated version of text during page load. I was able to get the translated text after trying different approaches but code became too complex and not reliable.

预期/期望的行为 应该加载文本的翻译版本

Expected/desired behavior Should load translated version of text

问题重现 动态生成文本,而不是在html上对其进行硬编码,然后尝试呈现翻译后的版本.

Reproduction of the problem Dynamically generate the text instead of hardcoding it on html and then try to render translated version.

环境 Angular2,Typescript,Ionic 2

Environment Angular2 , Typescript, Ionic 2

推荐答案

@ nkadu1

即时(键:字符串|数组,interpolateParams ?:对象):字符串|对象:获取键(或键数组)的即时转换值.此方法为同步,默认文件加载器为异步. 您有责任知道何时加载您的翻译,使用此方法是安全的.

instant(key: string|Array, interpolateParams?: Object): string|Object: Gets the instant translated value of a key (or an array of keys). This method is synchronous and the default file loader is asynchronous. You are responsible for knowing when your translations have been loaded and it is safe to use this method.

const translated = this.translate.instant('keyInJson');


@masterach TranslateHttpLoader 是您要找的东西. 这是一篇文章,可能对您有帮助.


@masterach TranslateHttpLoader is what you're looking for. Here's an article which might be helpful for you.

这篇关于无法使用ngx转换/核心获得动态翻译的文本-angular 2打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆