Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

JSP with non-English characters

In DB table , I have  I have 3 column
project_id, project_name_english , project_name_japanese
I have records in these columns.

In JSP, I display  records from project_name_english column by default.

Now, I want if my user clicks on "japanese" link on my JSP , I want to display japanese column value.
 I also want if my logged user's language is  "japanese" , then my page should display in japanese.

Question:
What is the best approach I can think here ?

I'm worried about conversion of language.

I'm using JSP, Servlet  and Struts 1.3  mix.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Make sure you use UTF-8 encoding in the db and as the page encoding. Then you should be fine
Avatar of cofactor
cofactor

ASKER

My VO  has 3 fields

String projectID
String projectNameEnglish
String projectNameJapanese

I load data from DB when user logs into the system.  ...and  want to display English by default.

>>> if my user clicks on "japanese" link on my JSP

How do I switch English to Japanese now in JSP ?
The field values are stored in ENGLISH or JAPANEES in database?
>>>The field values are stored in ENGLISH or JAPANEES in database

Database has two separate columns for this.

DBA has loaded  both data manually.
Well it's a pretty crude way of arranging things (the project name field should be a key into a language table for example) but check the locale of the client and show the Japanese if it's ja_JP
When user click the language link, store the selected lang_key into session. (example: en, jp)
On jsp, check the session value for lang_key and as per that call the respective getter for project name

See my example in jsp:

<%
    String langKey = (String)session.getAttribute("lang");
    ProjectVO proj = (ProjectVO)request.getAttribute("projVo");
%>

Project Name: <%=langKey.equals("en")?proj.getProjectNameEnglish():proj.getProjectNameJapanese()%>

Open in new window

Encode your page properly with UTF-8, so that the characters are properly displayed.

Then I would put the english label in a span with a unique ID, then when the user clicks the "Japanese" link or button, run a bit of javascript to rewrite the value in the span with the Japanese value. That way the name is rewritten without the page having to be reloaded.

Something like this:
<span id="projectName">English Title</span>
<input type="button" value="Japanese" onclick="swapProjectName('Japanese title')"/>

<script type="text\javascript">
    function swapProjectName(jTitle) {
        var projectNameSpan = document.getElementById("projectName");
        projectNameSpan.innerHtml = jTitle;
    }
</script>

Open in new window

@pramodkrjsr,

concerns / doubts in your comments.

(1)
If I have to display multiple fields , that becomes a messy code.  Is not there any other simplified way ?

(2) Do I need  to call  javascript reload to reload the page as soon as user clicks on "japanese" link.
There are standard, best practices for i18n, which you should follow. No need to reinvent the wheel:

http://www.roseindia.net/struts/strutsinternationalization.shtml
>>>There are standard, best practices for i18n, which you should follow. No need to reinvent the wheel:

they are using multiple properties file ( each property file for each language) whereas  I'll be using database(each column value for each language).

how do you make it dB drivern /?



Normally a user will not frequent change the language page to page from english to japaness and vis-versa.

I would suggest to introduce another getter method for project name which will return the value as per language. By this, you need to use only one method in your jsp.

Project Name: <%=proj.getProjectName(langKey)%>
String projectID
String projectNameEnglish
String projectNameJapanese
String projectName;

public String getProjectName(String lagKey)
{
	if(langKey.equals("en")
	{
		return projectNameEnglish;
	}
	else if(langKey.equals("jp")
	{
		return projectNameJapanese;
	}
	return null;
}

Open in new window

Problem with this approach is,  this involves lots of JSP scriplets....code is going to become messy.

I'm looking for a solution with JSTL or Sturts tags.  Can this be made with using JSTL or Struts tags ? Need a small sample to start with.
>>how do you make it dB drivern /?

I don't know the answer to that offhand - but there probably is one ;)
I don't see lots of JSP scriptlets and just one line for one field:
<%=proj.getProjectName(langKey)%>
It would seem the 'proper' way to do it would be to use the new ResourceBundle.Control class to allow the bundle to be loaded from a db. The Javadoc gives clues on this:

http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.Control.html
>>>><%=proj.getProjectName(langKey)%>

what is the equivalent code in JSTL ?


>>>>It would seem the 'proper' way to do it would be to use the new ResourceBundle.Control class to allow

Thanks for the info. Will look out for examples.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

<jsp:useBean id="proj" class="bean.ProjectVO"/>

Project Name: <jsp:getProperty name="proj" property="projectName"/>

Open in new window

:)