我正在使用一个项目,要求是将整个网站英文翻译成泰文我做了95%但是面临一个问题如何在.ts文件中的HTML代码中使用json密钥。
.ts代码
{
element: '#step_two_element_id',
intro: `
<div class="mobile-verification-dialog">
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption font-green-sharp">
<i class="icon-users font-green-sharp"></i>
<span class="caption-subject bold uppercase">Mobile Phone Verification</span>
<span class="caption-helper hide"></span>
</div>
</div>
<div class="portlet-body">
<h5 class="pull-left">Enter your phone number to GET a free trial</h5>
<input type="text" class="form-control" placeholder="+15256458521" id="trialPhone">
<br/>
<input type="button" class="btn btn-primary pull-right" id="trialPhoneBtn" value="Send Code">
<br/><br/>
<small class="block text-right">You will receive verification code shortly.</small>
</div>
</div>
</div>`,
position: 'bottom'
},
HTML(我在HTML中使用了这样的翻译)
<div class="page-bar">
<ul class="page-breadcrumb">
<li>
<a routerLink="/dashboard">{{ 'HOME' | translate }}</a>
<i class="fa fa-circle"></i>
</li>
<li>
<span>{{ 'CONFIGURE_PAGES' | translate }} </span>
</li>
</ul>
</div>
答案 0 :(得分:0)
您可以使用DOMParser解析该字符串HTML,然后获取如下所示的键:
var parser = new DOMParser();
var doc = parser.parseFromString(jsonData.intro, "text/html");
//declare a global variable htmlKey
this.htmlKey = doc.getElementsByClassName("caption-subject")[0].innerHTML;
然后在您的HTML中,您可以像这样使用您的管道:
{{ htmlKey | translate }}
答案 1 :(得分:0)
最后我在几天前解决了。下面是代码。
initTour(lang) {
if(lang === 'en'){
var WELLCOME= 'Wellcome'
var SEND_CODE = "Send code"
var VERIFY = 'Verify'
var CODE = 'Code'
}
else if(lang === 'th'){
var WELLCOME= '惠康'
var SEND_CODE = "发送代码"
var VERIFY = '校验'
var CODE = '码'
}
var tourStep1Content = `
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption font-green-sharp">
<i class="icon-users font-green-sharp"></i>
<span class="caption-subject bold uppercase">`+WELLCOME+`</span>
<span class="caption-helper hide"></span>
</div>
</div>
<div class="portlet-body">
<h5>`+SEND_CODE+`</h5>
</div>
</div>`;
var tourStep2Content = `
<div class="mobile-verification-dialog">
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption font-green-sharp">
<i class="icon-users font-green-sharp"></i>
<span class="caption-subject bold uppercase">`+VERIFY+`</span>
<span class="caption-helper hide"></span>
</div>
</div>
<div class="portlet-body">
<h5 class="pull-left">`+CODE+`</h5>
<input type="text" class="form-control" placeholder="5256458521" id="trialPhone">
<br/>
</div>
</div>`;
intro.setOptions({
steps: [
{
element: '#tour_step_1',
intro: tourStep1Content,
position: 'left'
},
{
element: '#step_two_element_id',
intro: tourStep2Content,
position: 'bottom'
},
],
exitOnEsc: true,
exitOnOverlayClick: false,
showStepNumbers: false,
showBullets: true,
showButtons: false
});
}