如果缺少密钥或正在加载翻译文件,请执行ngx-translate默认文本 [英] ngx-translate default text if key is missing or translation file is being loaded
问题描述
我正在设置一个新的Angular 7应用程序.我想设置默认文本进行翻译.因此,在翻译{{'wait'|翻译}},如果有任何后备内容,我想将文本立即等待"设置为默认文本.意味着如果正在加载数据或缺少密钥,则应该显示传递的值(在这种情况下为Waiting Now).
I'm setting up a new Angular 7 app. I want to set a default text for translation. So in the translation {{ 'wait' | translate }}, I want to set text 'Waiting Now' as default text if there are any fallback. Means if data is being loaded or key is missing then passed value (in this case Waiting Now) should appear.
我正在尝试做类似{{ 'Intro' | translate:'localizedText' }}
不是没用
{{ 'Intro' | translate:'localizedText' }}
我希望结果应该是
{{'简介'|翻译:"localizedText"}} =>简介(如果正在加载或缺少密钥)
{{ 'Intro' | translate:'localizedText' }} => Intro (If being loaded or missing key)
{{'简介'|翻译:"localizedText"}} =>翻译文字
{{ 'Intro' | translate:'localizedText' }} => translated text
推荐答案
I followed the instructions to create a missing translations handler here: https://github.com/ngx-translate/core#how-to-handle-missing-translations
但是我的版本允许像这样将默认值传递给管道
But my version allows passing a default value to the pipe like this
<span>{{"MyTranslateKey" | translate: {Default: "Default Translation"} }}</span>
默认值可以是上面指定的字符串或变量.
The default can be a specific string as above, or a variable.
这是我的处理程序:
import {MissingTranslationHandler, MissingTranslationHandlerParams} from '@ngx-translate/core';
export class MissingTranslationHelper implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
if (params.interpolateParams) {
return params.interpolateParams["Default"] || params.key;
}
return params.key;
}
}
这篇关于如果缺少密钥或正在加载翻译文件,请执行ngx-translate默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!