gpt4 book ai didi

javascript - 使用 ngx-translate 以 10 Angular 的形式翻译动态字符串

转载 作者:行者123 更新时间:2023-12-04 08:38:06 25 4
gpt4 key购买 nike

我正在使用一些动态值来创建一个字符串并将其发送到 sweetalert 配置。
我想知道有什么方法可以在发送到配置之前翻译字符串?
我见过几个解决方案,我们可以将变量传递给 translate 方法,但是还有其他解决方案可以使用吗?
我应该在 en.json 中添加什么?

let status = '';
switch (action?.innerText) {
case 'Accept': {
status = 'Accepted';
break;
}
case 'Approve': {
status = 'Approved';
break;
}
case 'Delete': {
status = 'Deleted';
break;
}
case 'Reject': {
status = 'Rejected';
break;
}
}

//You have ${status} a Request for $${this.data?.details?.amount}
let transalteTitle;
this.translate.get('You have', {
value: status
}, ' a Request for', {
value: this.data?.details?.amount
}).subscribe(res => {
transalteTitle = res;
});

const configval = {
title: transalteTitle,
text: 'Do you want to continue?',
showCancelButton: true,
icon: 'warning',
confirmButtonText: 'YES',
cancelButtonText: 'NO'
};


//en.json

{
"title": "You have ${status} a Request for ${val}"
"status_accepted": "accepeted",
"status_rejected": "rejected"
}

最佳答案

let status = '';
switch (action?.innerText) {
case 'Accept': {
status = 'status_accepted';
break;
}
case 'Approve': {
status = 'status_approve';
break;
}
case 'Delete': {
status = 'status_delete';
break;
}
case 'Reject': {
status = 'status_rejected';
break;
}
}

//You have ${status} a Request for $${this.data?.details?.amount}
let transalteTitle;
this.translate.get(status).pipe(switchMap(translatedStatus =>
this.translate.get('title', {
status: translatedStatus, val:
this.data?.details?.amount
}))).subcribe(translatedTilte => {
const configval = {
title: transalteTitle,
text: 'Do you want to continue?',
showCancelButton: true,
icon: 'warning',
confirmButtonText: 'YES',
cancelButtonText: 'NO'
};
})


//en.json

{
"title": "You have ${status} a Request for ${val}"
"status_accepted": "accepeted",
"status_rejected": "rejected"
}`enter code here`

关于javascript - 使用 ngx-translate 以 10 Angular 的形式翻译动态字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64699747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com