我正在设置一个新的 Angular 7 应用程序。我想为翻译设置默认文本。所以在翻译中 {{ 'wait' |翻译}},如果有任何回退,我想将文本“Waiting Now”设置为默认文本。意味着如果正在加载数据或缺少 key ,则应显示传递的值(在本例中为“立即等待”)。
我正在尝试做类似{{ 'Intro' |翻译:'localizedText' }}
没用
{{ 'Intro' | translate:'localizedText' }}
我希望结果应该是这样
{{ '介绍' |翻译:'localizedText' }} => 简介(如果正在加载或缺少 key )
{{ '介绍' |翻译:'localizedText' }} => 翻译后的文字
最佳答案
我按照说明在此处创建了一个缺失的翻译处理程序:https://github.com/ngx-translate/core#how-to-handle-missing-translations
但我的版本允许像这样将默认值传递给管道
<span>{{"MyTranslateKey" | translate: {Default: "Default Translation"} }}</span>
默认可以是上面的特定字符串,也可以是变量。
这是我的处理程序:
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;
}
}
关于angular - 如果缺少 key 或正在加载翻译文件,则 ngx-translate 默认文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55662448/