3

We are trying to internationalize our Spring MVC web application in Hindi language. When we try to copy the Hindi text in the properties file, the properties file shows small boxes in places of Hindi characters.

When we run the application and see the JSP, it shows questions marks (???????) in place of Hindi characters.

Edit:

My properties file has following contents.

login.message=\u0915\u0943\u092a\u092f\u093e \u0905\u092a\u0928\u0947 \u0916\u093e\u0924\u0947 \u092e\u0947\u0902 \u0932\u0949\u0917 \u0911\u0928 \u0915\u0930\u0928\u0947 \u0915\u0947 \u0932\u093f\u090f \u0928\u0940\u091a\u0947 \u0915\u0947 \u092b\u093e\u0930\u094d\u092e \u0915\u093e \u0909\u092a\u092f\u094b\u0917 \u0915\u0930\u0947\u0902

I have used following command to get this encoded string.

native2ascii -encoding utf-8 ApplicationResources_hi.properties gen\ApplicationResources_hi.properties

My JSP page has following line in head section

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Do I need to do anything else? Sorry I may be missing something here.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
ajm
  • 12,863
  • 58
  • 163
  • 234
  • It is because of charset issue. Make UTF8 used – jmj Sep 06 '11 at 05:33
  • @Jigar Joshi Hi Jigar. Thanks for the quick reply. I have tried saving my properties file in UTF-8 format. Still it does not work. UTF-8 encoded properties file show \u0915\u0943\u092A\u092F\u093E. – ajm Sep 06 '11 at 05:36
  • yes so now problem might be in reading/displaying – jmj Sep 06 '11 at 05:41

2 Answers2

5

You actually have to get 2 separate steps right for this to work:

  1. getting the text from your .properties file to your Java code correctly and
  2. getting the text from your Java code to the browser in a way that it understands.

The first one is somewhat strange: .properties files are defined to use the ISO-8859-1 encoding, which doesn't support arbitrary Unicode characters, but luckily .properties file support the same Unicode escapes that Java source code also supports, namely \uxxxx.

Now writing those escapes by hand can become nasty, so there are basically two alternatives:

  • write a .properties file with the encoding of your choice (UTF-8 probably) and use native2ascii to convert it to the propper encoding
  • use a dedicated .properties editor that already does this (and usually a bit more) behind the scenes

Once this works (verify it in the debugger by looking at the character values (String.charAt() of some localized strings), you need to make sure that the browser actually receives the data in the correct way.

The easiest way here is to make sure that you use UTF-8 encoding to get the data to the browser, since UTF-8 can represent all possible Unicode codepoints.

If you're using JSP to produce your output, then you can use something like this to specify that you want UTF-8 output:

<%@ page contentType="text/html; charset=utf-8" language="java" %>

See the Java EE Tutorial for details.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Hi. I did all of this but still it shows ????????. I have updated my question with what I have done so far. – ajm Sep 06 '11 at 06:31
  • 3
    You specified which encoding the page *should* have (i.e. you promised the browser that you're writing UTF-8), but I don't see where you actually make sure that you *actuall write* UTF-8. Your JSP should have something like this near the top: `<%@ page contentType="text/html; charset=utf-8" language="java" %>`. See [this page for details](http://download.oracle.com/javaee/5/tutorial/doc/bnayb.html#bnayg). – Joachim Sauer Sep 06 '11 at 06:38
-1

Make sure the .properties file is saved in UTF-8 encoding, and the charset in the JSP page is also set to UTF-8

In your IDE, (for example Eclipse), go to Window -> Preferences -> Workspace, then make sure Text File Encoding is set to UTF-8 as well

UPDATE
You might need to convert the file to Ascii, Java has that tool already. From the command line navigate to Java SDK folder, then try this:

..\Java\bin>native2ascii -encoding utf-8 Labels.txt Labels_locale.properties

Set the _locale to your Hindi local (two letters only)

mohdajami
  • 9,604
  • 3
  • 32
  • 53