问题
I have a dropdown menu in my JSP page which is implemented with <s:select>
tag it is as
<s:select name="priorToApplyingInfo.userProfile.phoneNumbers[0].type"
listKey="key" listValue="value" list="phoneTypes" headerKey="0" headerValue=""/>
Now the values in the dropdown menu are from the list phonetypes
which is implemented as a HashMap
in .java
file.
phoneTypes = new LinkedHashMap<Integer, String>();
phoneTypes.put(new Integer(1), getText("HOME"));
// Phone ContactBook category for the business phone
phoneTypes.put(new Integer(DAOHelperFactory.OWNER_PROFILE_PHONE_CATEGORY), getText("WORK"));
phoneTypes.put(new Integer(3), getText("MOBILE"));
phoneTypes.put(new Integer(DAOHelperFactory.OWNER_PROFILE_FAX_CATEGORY), getText("FAX"));
phoneTypes.put(new Integer(5), getText("OTHER"));
preferredContact = new ArrayList<String>();
preferredContact.add(getText("HOME"));
preferredContact.add(getText("WORK"));
preferredContact.add(getText("MOBILE"));
preferredContact.add(getText("FAX"));
preferredContact.add(getText("EMAIL"));
preferredContact.add(getText("OTHER"));
bestContactTime = new ArrayList<String>();
bestContactTime.add(getText("AFTERNOON"));
bestContactTime.add(getText("EVENING"));
bestContactTime.add(getText("MORNING"));
The keys like home=home
, work=work
, etc., are in a .properties
file I am working on internationalizing this page and I'm unable to find a way to get the translations for the
values in the dropdown menu.
回答1:
To change locale in Struts2 application you need to include requst_locale
parameter to some link or form.
<s:url var="urlen" includeParams="all" value="">
<s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{#urlen}">English</s:a>
If you want to change locale from action class use ActionContext
to set it and also put it in HTTP session.
ActionContext.getContext().setLocale(locale);
session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale);
You can call getText
method also in JSP in listValue
attribute of <s:select>
tag.
<s:select name="priorToApplyingInfo.userProfile.phoneNumbers[0].type"
list="phoneTypes" headerKey="0" headerValue=""
listKey="key" listValue="%{getText(value)}"/>
回答2:
For me worked (like Aleksandr M wrote on last paragraph):
<s:select listValue="%{getText(value)}" listKey="key" list="phoneTypes></s:select>
only I created
phoneTypes = new HashMap<String, String>()
phoneTypes.put("HOME", "HOME")
phoneTypes.put("WORK", "WORK")
etc..
In this case keys are left out (you can write anything here) and on page displays (translated) values. This solution does not work with list, only map.
回答3:
It seams you didn't switch the locale in Struts2 before you are retrieved the messages from the resources.
getText()
is localized method and if it using the default text provider as a default behavior then it searches for locale specific keys. You could get the current locale used by Struts2 from action context or direct from your ActionSupport
action (haven't seen that you have an action and it extends it).
Usually switching locale is done via the i18n
interceptor where you put a parameter into request request_locale
. But you could switch it by changing locale in the action context (make sure you are running the same thread as current).
ActionContext.getContext().setLocale(new Locale("es"));
You should run this code before any getText()
is executed to get localized messages.
来源:https://stackoverflow.com/questions/18407063/i18n-on-dropdown-menu-using-sselect-tag