JSTL fmt:message and resource bundle

此生再无相见时 提交于 2019-12-21 12:35:59

问题


I want to set the "dir" property of my table from resource bundle based on the locale.

Here is snippet:

        <fmt:setBundle basename="class.path.to.resource.bundle"/>
        <table align=center class="" dir=<fmt:message key="registration.direction"/>>

When the page renders I get this:

   <table align=center dir=???registration.direction???>

I have two resource bundles for english and arabic.

registration.direction = ltr -> English

registration.direction = rtl -> Arabic

Please tell what I am doing wrong? The dir should have "ltr" or "rtl" depending on the locale.

Thanks

BR SC


回答1:


two things

1) I would add a variable to store the message result in

<fmt:message key="registration.direction" var="direction" />

then

2) I would do the following with your code

  <fmt:setBundle basename="class.path.to.resource.bundle"/>
  <fmt:message key="registration.direction" var="direction" />
  <table align=center class="" dir="${direction}">

Now as far as your resource bundles, typically You should have the following structure for your resource bundles

/foo/bar/MyResourceBundle.properties
/foo/bar/MyResourceBundle_en.properties
/foo/bar/MyResourceBundle_en_US.properties
/foo/bar/MyResourceBundle_<lang>[_COUNTRY[_VAR]].properties

If your bundle is not structured in this fashion that might be some of your problem.

Make sure that all keys that are expected to be available are defined in MyResourceBundle with reasonable defaults.

I'm amending this answer as I'm not sure if my comment got lost in a hide function.

With the fact that you are using Struts 2, I'm under the impression that you're using the i18n interceptor. The interceptor will store the current locale in the sesion variable named WW_TRANS_I18N_LOCALE. As such you should be able to get to it and set the locale for the JSTL tags by using the following:

<fmt:setLocale scope="session" value="${sessionScope.WW_TRANS_I18N_LOCALE}" />

Hope that works for you.



来源:https://stackoverflow.com/questions/4667724/jstl-fmtmessage-and-resource-bundle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!