Struts2: UTF .properties - java

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...

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.

Related

How to specify, which resource file to be used while running?

I wants my application to be in both Hindi and English language, so I have created a ResourceBundle named as Resources. In this directory I have create label.properties,label_en_US.properties and label_hi_IN.properties and these file have putted some values like ab=xy_default, ab=xy_in_en and ab=xy_in_hindi respectively.
And Now I planned to use them as label text, so in property of that label i mantioned the default one, like this
Here code is key and In label_defualt is code=Code : , in label_en_US code=Code : and in label_hi_IN code=कोड :. How can I specify which of label.properties to be useout of label, label_en_US or label_hi_in. I have stored user preference in my database like which language to use. I want to know how I can force or tell it use that particular file out of label, label_en_US, label_hi_IN in main function or somewhere else. As now it's taking values from label.properties file only, if user want it to be in hindi then how internally we force to use that label_hi_IN.properties file.
If you've got your files sorted out OK, the right internationalization should occur naturally depending on your locale. If you want to test other supported locales in your software, set the appropriate system properties:
java -Duser.country=IN -Duser.language=hi …
or
java -Duser.country=US -Duser.language=en …
ResourceBundle uses a “fallback” strategy. If the user’s current locale is hi-IN:
ResourceBundle.getBundle will look in label_hi_IN.properties (if that resource exists)
If that file is not found, ResourceBundle.getBundle will look for label_hi.properties (if that resource exists)
If that file is not found, ResourceBundle.getBundle will look for label.properties (if that resource exists)
This means you should do the following:
If label.properties contains English entries, remove label_en_US.properties from your project
If label.properties contains Hindi entries, remove label_hi_IN.properties from your project
Rename label_hi_IN.properties, if it is still present, to label_hi.properties, so all Hindi locales will use that file
Rename label_en_US.properties, if it is still present, to label_en.properties, so all English locales will use that file
Finally I got it, We have to just set Default Locale.
Locale locale = new Locale("hi","IN");
Locale.setDefault(locale);

Read Japanese Characters from a properties file using Resource Bundle

I am working on developing a project and when I attempt to read in a Japanese character string from a properties file using ResourceBundle in Java, all I get are a bunch of question marks (??????????) displayed in my application.
I know that I need to encode the string somehow, but I've tried several different ways with no luck. I've also tried to encode the properties file in a text editor but this doesn't seem to work how I'd like either. My code for retrieving the string is below. I know you can do this with stream readers and other methods but I would like to try to stay with the current code structure using ResourceBundle if possible. If anyone has any questions or needs clarification please feel free to let me know.
private final ResourceBundle bundle = ResourceBundle.getBundle("propertiesFile");
titleText = bundle.getString("title.text");
Just to give you an idea, title.text=会議参加者事前登録用ページ in my property file.
Any suggestions are appreciated. This has turned into a real pain.
Thanks again,
-Dave F.
ResourceBundle can only read ISO 8859-1 file. See this thread. How to use UTF-8 in resource properties with ResourceBundle
I had the same problem here, and I've converted my ResourceBundle to a Properties object like in the answer of this thread: read resourcebundle as UTF-8. getString() Method seems to change encoding to ISO-8859
Like this:
InputStream utf8in = getClass().getClassLoader().getResourceAsStream("/path/to/utf8.properties");
Reader reader = new InputStreamReader(utf8in, "UTF-8");
Properties props = new Properties();
props.load(reader);
After some research I was actually still able to make use of the ResourceBundle method.
In my properties file that ResourceBundle pulls from, I listed the following in unicode:
title.text=\u4f1a\u8b70\u53c2\u52a0\u8005\u4e8b\u524d\u767b\u9332\u7528\u30da\u30fc\u30b8
When pulled in by ResourceBundle, it translates as:
会議参加者事前登録用ページ
I'm sure this probably isn't the best practice, but it works without having to change how the entire project pulls in its resource properties, so I just thought I would share with you all for helping me out.
when you run your app you can try this:
java -Dfile.encoding=UTF-8 -jar YourApp.jar
this basically sets the default charset

How to access XML files through the URL

I am a beginner in Struts 2 and I am stuck in an issue, I would like to access an .xml file in the project doc, when the user goes to the link www.sitename.com/sitemap.xml. But right now I can only access it by going to the URL www.sitename.com/sitemap, i.e. without the extension. When I specify the action name as sitemap.xml it does not work. How could I make the user access the URL www.sitename.com/sitemap.xml. Sorry If the question does not make any sense.
The standard extension in Struts 2 for action name is .action. But it's configurable via using a setting struts.action.extension. It's available in the default.properties, which is used by the default action mapper.
You might use a comma separated list, e.g. struts.action.extension=action,xml,whatsoever
The blank extension used to map directories treated as action names. Static content is mapped via specifying a blank extension, e.g. struts.action.extension=, or struts.action.extension=a,b,c,,, or struts.action.extension=a,,b,c.

Translate the Properties File from one language to another using ResourceBundle

I have message.properties file. by default it will load as English when I use Locale and Resource Bundle in Java file. Now, I need to specify the Country Code or Type in Locale, Then the message.properties file key values should be load as the specified Country in Locale. How can I do this.?
Oracle has some example code of this here.
Basically you create bundles with the correct name for the language of the Localeand then load it with:
ResourceBundle.getBundle("LabelsBundle", currentLocale);
More information about internationalization..
Also, before asking a question try googling for yourself....

Force resolution of xsl:include, xsl:import in Java

I'm using Saxon 9.3 HE and Java 1.6. I can resolve xsl:include and xsl:import statements in the xsl by supplying a resolver to setURIResolver on the TransformerFactory instance.
However the Source resolve(String includee, String includer) method doesn't get called if the file was resolved previously. This is a problem for me because I want to resolve differently based on the includer file. For example <xsl:include href="foo.xsl"/> in file1.xsl would be a different file from <xsl:include href="foo.xsl"/> in file2.xsl, and file1.xsl and file2.xsl would be included by file3.xsl. I have some "base" code and "customer-specific" code that can override the template file and I need to resolve them differently for a framework I'm building.
The XSLT specification is clear that resolving a relative URI in the href attribute against the base URI of the containing element must be done according to the standard rules for handling relative URIs, while dereferencing the resulting absolute URI can be done any way the implementation likes. I'd suggest rethinking your design to take account of this.
I would have expected that because the two XSLs that have that includes in them have different base URIs that the URIResolver would need to be called for each one (what if they are in different directories?).
When creating sources for file1.xsl and file2.xsl, what are their system IDs? If they are null, exist in the same directory or don't have any path information (i.e. systemId is just file1.xsl and file2.xsl) maybe Saxon is trying to do an optimization by assuming they are in the same directory and therefore assuming foo.xsl referred to by each one is the same file.
Maybe try explicitly setting the systemId of the source of the base files and make them have different directories?

Categories

Resources