【问题标题】:How to change the currency symbol for any one specific locale?如何更改任何特定语言环境的货币符号?
【发布时间】:2017-10-16 20:13:14
【问题描述】:

我正在使用 jstl 来显示像这样的货币值...

<fmt:formatNumber value="${sellingPrice}" type="currency"/>

99% 的时间都有效。现在我有一个要求,对于给定的语言环境,客户根本不想显示任何货币符号。

我试过这个:

<fmt:formatNumber currencySymbol="" value="${sellingPrice}" type="currency"/>

...无论语言环境如何,都会删除货币符号。

我想过像这样使用spring消息:

<fmt:formatNumber currencySymbol="<spring:message code='myCurrency.symbol' />" value="${sellingPrice}" type="currency"/>

但问题是我需要返回并为我支持的所有其他语言环境创建正确的货币符号,并为可能出现的任何新语言环境添加春季消息......

我真正需要的是能够说这样的话:

if(locale == 'EN_XX') { currencySymbol=' '; } else { // otherwise use whatever the normal currency symbol would be for that locale }

有没有办法在低于 JSP 的级别覆盖特定语言环境的默认货币符号?我的意思是价值必须来自某个地方,对吗?

【问题讨论】:

  • 语言环境属性文件baeldung.com/spring-boot-internationalization 可以为您工作吗?
  • @varren 我正在使用:&lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt; 并且我已经定义了一个 localeResolver ...虽然没有看到如何更改特定语言环境的货币符号...

标签: java spring jsp jstl el


【解决方案1】:

我最终创建了自己的扩展 FormatNumberTag 的自定义标签。它找出正确的语言环境,然后调用 FormatNumberTag.setCurrencySymbol("...");

public class MyFormatNumberTag extends FormatNumberTag {

   public MyFormatNumberTag() {
       super();
   }

   //...snip...

   @Override
   public int doStartTag() throws JspException {
       Locale locale = getMyRequestLocale();

       if(locale == Locale.EN_GB) setCurrencySymbol("");

       return super.doStartTag();
   }
}

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2012-09-23
    • 2014-12-28
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多