Javascript vue-i18n如何简化货币显示?
有没有办法实现d3在vue-i18n生态系统中的功能 有一个悬而未决的问题 我发现了Javascript vue-i18n如何简化货币显示?,javascript,vue.js,vue-i18n,Javascript,Vue.js,Vue I18n,有没有办法实现d3在vue-i18n生态系统中的功能 有一个悬而未决的问题 我发现了Intl对象的一个老问题 感谢您的帮助。一种方法是用您自己的处理程序替换VueI18n.n 添加自定义属性=缩写为VueI18n.numberFormats,以确定是否应用特殊样式 将原始的VueI18n.n保存到VueI18n.n1(对于其他样式,我们仍将使用VueI18n.n1) 使用您自己的处理程序替换VueI18n.n,然后在处理程序内部,检查numberFormatter.缩写是否为true,如果为ye
Intl
对象的一个老问题
感谢您的帮助。一种方法是用您自己的处理程序替换
VueI18n.n
VueI18n.numberFormats
,以确定是否应用特殊样式VueI18n.n
保存到VueI18n.n1
(对于其他样式,我们仍将使用VueI18n.n1
)VueI18n.n
,然后在处理程序内部,检查numberFormatter.缩写是否为true,如果为yes,应用您的特殊样式(在下面的演示中,只需使用正则表达式来实现)
Vue.use(VueI18n)
常数numberFormats={
“恩我们”{
货币:{
样式:“货币”,货币:“美元”,货币显示:“符号”,使用分组:false
}
},
‘ja JP’:{
货币:{
样式:“货币”,货币:“日元”,货币显示:“名称”,
缩写:true,//用于确定是否应用特殊样式的自定义属性
最大有效位数:4,使用分组:真
}
}
}
常数i18n=新VueI18n({
数字格式
})
i18n.n1=i18n.n//将默认i18n.n保存到i18n.n1
i18n.n=函数(nValue,nStyle){
让numberFormatter=this.getNumberFormat(this.locale)//基于locale获取NumberFormat
if(numberFormatter[nStyle].缩写){//if自定义属性为true
让newValue=Math.round(nValue/1000000)//将10^6除以百万,将10^3除以K,依此类推
返回此.n1(newValue,nStyle)。替换(/(\d[\d|,]*)(\。\d+)/,“$1$2M”)
}
返回此.n1(nValue,nStyle)//对于其他,使用默认的i18n.n进行处理。
}
Vue.config.productionTip=false
app=新Vue({
el:“应用程序”,
i18n,
数据:{
测试:1234567890,
语言:“en US”
},
计算:{
computedTest:函数(){
返回此.$n(this.test,'currency')
}
},
方法:{
toggleLanguage:函数(){
this.language=this.language=='en-US'?'ja-JP':'en-US'
this.$i18n.locale=this.language
}
}
})
语言
语言:[{{Language}}]
组织:{{test}}
本地化:{{computedTest}}
经过一段时间的研究,我决定编写自己的助手方法,因为我的网站上显示的价格相当高,所以缩写是必须的@斯芬克斯的解决方案似乎是太多的努力,只是为了使数字更短
我创建了一个utils文件并将i18n导入其中。这个想法来自
在您的组件中
<template>
<div class="col-auto">
{{ calculateCurrency(item.price) }}
</div>
</template>
<script>
import { CURRENCY_CALCULATOR } from '../constants/Utils'
export default {
name: '<<component name here>>',
props: ['<<your props here>>']
methods: {
calculateCurrency (price) {
return CURRENCY_CALCULATOR(price)
}
}
}
</script>
{{calculateCurrency(item.price)}
从“../constants/Utils”导入{CURRENCY_CALCULATOR}
导出默认值{
名称:“”,
道具:['']
方法:{
计算频率(价格){
返回货币\u计算器(价格)
}
}
}
感谢您的建议。你默默地说,没有经典的方法可以做到这一点。。。我将把你的附加费方法和d3格式结合起来。我必须将我的数字翻译成7种语言,有很多不同的数字,我不能从头开始。@GeorgeAbitbol,因为i18n使用,但这个API不能实现您的要求。i18n>1作者的回答是,与正则表达式不同,另一种方法是使用一个第三方库来实现目标,比如@GeorgeAbitbol,我以前没有使用D3,但在google之后,这个库似乎更适合您的需求
<template>
<div class="col-auto">
{{ calculateCurrency(item.price) }}
</div>
</template>
<script>
import { CURRENCY_CALCULATOR } from '../constants/Utils'
export default {
name: '<<component name here>>',
props: ['<<your props here>>']
methods: {
calculateCurrency (price) {
return CURRENCY_CALCULATOR(price)
}
}
}
</script>