Solved

Struts Internationalization (i18n)

Posted on 2008-10-12
10
1,827 Views
Last Modified: 2013-11-24
Hi Experts,

I am internationalizing a web site - something I have successfully done before but am having problems with the way the languages are being displayed in the browser.

On a JSP page if I paste French text directly into the JSP page, it is displayed correctly in the browser.  If I pull the same string from a UTF-8 property file with struts tag:

<struts-bean:message bundle="content" key="content.landing.captain.01" />

The text is not formatted correctly.  I suspect it is some struts configuration issue, but cannot track it down.  Any suggestions?

Any help greatly appreciated!

Regards,

John
0
Comment
Question by:mygreenisland
  • 6
  • 2
10 Comments
 
LVL 4

Expert Comment

by:Mr_It
ID: 22795511
And how is the text exactly displayed?

I don't think it's a struts problem. It might be a problem with your property files itself. I guess you have problems with special characters like é, à, etc... If so, it's because you are editing your propery files using the native character encoding of your OS and running the application elsewhere?

For resource bundles, it's always a good practice to convert all characters to unicode-encoded (\udddd notation) characters before using them in the application: è would become \u00e8, etc...

There's a standard java tool in the JAVA_HOME/bin folder that takes care of this automatically: http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/native2ascii.html. It's very easy to use. You can even run it from your build script. Just read about it to understand the problem.

There are also several plugins for Eclipse and other IDE's for editing resource bundles. Most of them will do the transformation from native to ascii for you (see the plugin preferences for settings).

Anyway, before you take a look at all this, do a manual test first by putting both the è and \u00e8 in your properties file, and see if this is really the problem you are experiencing...


0
 
LVL 4

Expert Comment

by:Mr_It
ID: 22795541
I just saw that you state that your properties file is UTF-8 encoded (and not OS native), so I think this might be indeed the cause of your problems.
0
 
LVL 4

Expert Comment

by:Mr_It
ID: 22795611
I meant, then it might not be the cause of your problems, but maybe you are using native encoding while you think you're not. Hey, it's worth a try!

Like I said, converting to ascii is never bad ;-)
0
 

Author Comment

by:mygreenisland
ID: 22795824
Hi Mr_It,

Thanks for your recommendations- I thought my poor question had been forgotten about ;-)   I am going to research the tools you mentioned- I would not be at all surprised if it is indeed the prop files that are the problem.  One thing though- it seems like you should not have to encode every character?  Do people in France encode their special characters?

I would be interested to learn more about this topic in general.

Thanks,

John
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:Mr_It
ID: 22799408
The tool only converts the 'special' characters. Problème would become Probl\u00e8me. The tool also works the other way around.

I have had project where é, à characters etc were displayed correctly. In another everything we had to convert them with the native2ascii tool. Just try to type \u00e8 in your properties file as a quick test and see what it does...
0
 

Author Comment

by:mygreenisland
ID: 22804547
Hi Mr_It,

Thanks for the suggestions.  I think I am getting closer to the solution, although not necessarily a better  understanding of all the issues at play.

First off- I agree with you- this is not a struts issue but something to do with the format of the file.

I tried the native2ascii utility and it worked without problem.  For example, it would convert the è to \u00e8, etc.  But the strange thing is that the browser would not display the characters correctly- également was replaced with également.

Then I tried something based on another of your suggestions- using a resource bundle plugin tool for Eclipse.  I figured I would create the property file with their tool and assumed that they would set up the file with the correct format, and this actually worked- almost!  Almost all the characters are displaying correctly, but some characters (e.g. presquîle) are being replaced in both Eclipse editor and in browser with a question mark.  Seems strange that it would be OK with some chars but not with all.

I hope that these latest symptoms give you a clue as to what might be happening, because I am running out of things to try ;0)

Thanks,

John
0
 
LVL 4

Accepted Solution

by:
Mr_It earned 500 total points
ID: 22810879
Properties files in java are loaded using the ISO 8859-1 character encoding. So your accents (é, à, ...) should work without problems (without converting to \u....). But there are characters, like the Euro-sign (¬) that are not in ISO 8859-1 (it is supported ISO 8859-15 however). So for characters, like the Euro-sign, a conversion to \u.... is necessary. That's why it's never bad to run some native2ascii tool.

Anyway, it looks like your browser applies the default character encoding, that is, us-ascii, and loses any international character. Did try to specify a unicode (like UTF-8) content type for your JSP pages? Using unicode is always recommended to prevent problems like you have right now, because unicode is universal character set that includes all the languages character sets. I'm sure it's also supported by your browser.

You can try to add this to your web.xml (ONLY if you use JSP 2.0 and up):

<jsp-config>
      <jsp-property-group>
            <description>Property group for common configuration for all the JSP's</description>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
      </jsp-property-group>
</jsp-config>

OR write something like this in all your JSP's:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page pageEncoding="UTF-8" %>

If that does not work, make sure your JSP pages itself are saved as UTF-8, because that is what you specify in you pageEncoding ;-)
0
 
LVL 4

Expert Comment

by:Mr_It
ID: 22810892
This is funny! To illustrate that character encoding is something that is often overlooked: The EURO sign in and also others in my previous post do not display correctly in my browser  :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 25 72
canBalance challenge 34 65
factorial example challenge 10 62
java 8 lambda expresssions exception handling 3 38
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now