如何在 JSP 中仅使用语言代码使 fmt:setLocale 在 fmt:formatNumber 上工作?
我正在尝试在 JSP Web 应用程序上本地化货币,问题是当我询问区域设置时,我只获得语言代码(“en”)而不是完整的语言和国家/地区代码(“en_US”)。 问题是,当 setLocale 的值不包含语言和国家/地区代码时,formatNumber 不起作用。
我可以通过在 jsp 页面开头检查区域设置语言并为几种语言设置默认国家/地区代码然后设置 setLocale 的值来解决此问题,但这种方法对我来说看起来相当难看。有更好的方法吗?
我现在就是这样做的:
<c:choose>
<c:when test="${pageContext.response.locale == 'cs'}">
<f:setLocale value="cs_CZ" />
</c:when>
<c:otherwise>
<f:setLocale value="en_US" />
</c:otherwise>
</c:choose>
<f:formatNumber type="currency" value="${product.price}" currencyCode="CZK"/>
I'm trying to localize currency on my JSP web application, problem is when I ask for locale, I only get language code ("en") instead of full language and country code ("en_US").
Problem with this is, formatNumber doesnt work when setLocale's value doesn't contain language and country code.
I can solve it by checking for locale language at the beginning of the jsp page and setting default country code for few languages and then setting value of setLocale, but this method looks pretty ugly to me. Is there a better way of doing this?
This is how I do it now:
<c:choose>
<c:when test="${pageContext.response.locale == 'cs'}">
<f:setLocale value="cs_CZ" />
</c:when>
<c:otherwise>
<f:setLocale value="en_US" />
</c:otherwise>
</c:choose>
<f:formatNumber type="currency" value="${product.price}" currencyCode="CZK"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
货币取决于国家/地区,而不取决于语言。您确实还需要设置它。更通用的方法是使用
Filter
来实现此目的,这样您就不需要在每个 JSP 中复制粘贴检查。更新:我现在看到您正在使用
HttpServletResponse#getLocale()
返回以编程方式设置的区域设置或容器的默认区域设置。正常的做法是使用HttpServletRequest#getLocale()
获取客户端的区域设置,因此:看看是否有帮助。然而,您仍然需要检查该国家/地区是否确实存在。
Filter
是最好的地方。The currency is dependent on the country, not on the language. You really need to set it as well. A more generic way is to use a
Filter
for this so that you don't need to copypaste the check in every JSP.Update: I now see that you're using
HttpServletResponse#getLocale()
which returns the programmatically set locale or otherwise the container's default locale. The normal practice is to useHttpServletRequest#getLocale()
to get the client's locale, thus so:See if that helps. You however still need to check if the country is actually present. A
Filter
is the best place for that.你用的是条纹!! Stripes 将为您处理区域设置,您应该从 ActionBeanContext 获取它:
不要绕过 Stripes 的背!这就是通往痛苦和不幸的道路!条纹是你的朋友!
一般来说,您不需要使用
,因为 Stripes 已经在 Stripes 过滤器中设置了区域设置。再次强调,Stripes 是您的朋友!! 在 Stripes wiki 中阅读相关内容:http://www.stripesframework。 org/display/stripes/Localization
You're using Stripes!! Stripes will handle the locale for you, and you should be getting it from the ActionBeanContext:
Don't go around Stripes' back! That is the path to distress and unhapiness! Stripes is your friend!
In general, you should not need to use
<fmt:setLocale>
because Stripes already sets up the locale in the Stripes filter. Again, Stripes is your friend!! Read about this in the Stripes wiki:http://www.stripesframework.org/display/stripes/Localization