问题
Is there a way to load a UTF-8 encoded .properties file in Struts2?
I know that its posible to load a UTF-8 file in a ResourceBundle via implementing your Control that loads a UTF8 inputstream but how to use it in Struts2? (I found howto do it in JSF here:
http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/
but cannot figure how to do it in struts2)
PD: Also I know that I can use the native2ascii tool, but its... ugly...
EDIT:
I've seen that I can implement TextProviderSupport and set it default like says here and override all its constructors setting a custom createrd resourcebundle with UTF8 inputReader and letting some logs. I've save it and when starting tomcat it says:
Información: Choosing bean (myTextProvider) for (com.opensymphony.xwork2.TextProvider)
with this in struts.config:
<bean class="com.utils.i18n.UTF8TextProvider" name="myTextProvider"
type="com.opensymphony.xwork2.TextProvider" scope="default"/>
<constant name="struts.xworkTextProvider" value="myTextProvider" />
<constant name="system" value="myTextProvider" />
So, its seems ok. But my class is never instanciated (I only run a sample action that implements action support and write a text with 'getText' and a jsp that have a <s:text name="">
)
So problem remains...
回答1:
Struts2 uses the concept of ResourceBundle to load properties files and achive the goals of internalization (i18n) as well as localization (l10n).
The resource bundle is used to keep the key and value pairs for respective languages such as English Resource Bundle can have value as English Text and France Resource Bundle can have value as France Text but the keys are same across.
Resource Bundle can be place in the following places
- ActionClass.properties
- Interface.properties
- BaseClass.properties
- ModelDriven’s Model
- Package.properties
- I18n message key
- Global resource properties
The resource properties are being searched in the above order. At first it looks the resource properties with action class name.properties, if does not found then it looks for interface.properties else goes on till Global resource properties.
There are many tutorials which explain in detail about how to use Resource Bundles in Struts 2. Have a look at the following links:
- http://www.journaldev.com/2304/struts2-resource-bundles-and-localization-example
- http://javatechig.com/java/struts/struts2-localization-example
- http://www.javabeat.net/struts-2-resource-bundle-example/
===============
Update
Regarding loading UTF-8 encoded properties file in Struts 2, you can refer http://javatechig.com/java/struts/struts2-localization-example and see how the Japanese text is being used after encoding it to UTF-8. Since, you mentioned about native2ascii, I am assuming that you know about how to convert a given text to UTF-8 encoding.
来源:https://stackoverflow.com/questions/22779037/struts2-utf-properties