【发布时间】:2018-02-05 13:15:43
【问题描述】:
所以我的范围是在开发模式下我在页面上显示翻译字符串(例如橙色)..
所以,我们有一个翻译管道
import { Pipe, PipeTransform } from '@angular/core';
import { TranslationService } from 'app/services/translation.service';
const getByKey = require('lodash.get');
@Pipe({
name: 'translate'
})
export class TranslatePipe implements PipeTransform {
constructor(public _translation: TranslationService) {}
transform(value: string, args?: string): any {
console.log('KEY: ', value, 'ENGLISH', args);
return getByKey(this._translation.store, value) || args;
}
}
我们在我们的视图中使用它
{{ 'global.key' | translate 'Translated text' }}
效果很好……
现在,我们想刷新那些用管道翻译的......所以它们在页面上可见......让我们的翻译页面更好地了解正在发生的事情(已翻译的内容)以及还缺少什么)
现在,如果我们要使用
这很简单,就像这样
import { Pipe, PipeTransform } from '@angular/core';
import { TranslationService } from 'app/services/translation.service';
import { DomSanitizer } from '@angular/platform-browser';
const getByKey = require('lodash.get');
@Pipe({
name: 'translate'
})
export class TranslatePipe implements PipeTransform {
constructor(public _translation: TranslationService, private _sanitizer: DomSanitizer) {}
transform(value: string, args?: string): any {
console.log('KEY: ', value, 'ENGLISH', args);
return this._sanitizer.bypassSecurityTrustHtml('<span class="translated">' + (getByKey(this._translation.store, value) || args) + '</span>');
}
}
但我们不是……我们没有使用 innerHTML……并且要跨站点实现它,这将是一项繁重的任务……
那么有没有其他方法可以将 html/css 包装器放入我们的管道翻译文本中?
欢迎任何想法
【问题讨论】:
标签: angular translation angular-pipe