Monday, July 10, 2006

Internationalization (i18n) and Multi bundling in Java web tier

I am going to describe basic concepts for i18n (internationalization), how to write and configure an internationalized software, and how to modify and internationalize a software. In addtion, I will write my experieces about this concept.

firstly, I stronglly belive that you must know something about resource bundles, because i18n doesnt have any meaning without resource bundle. So, after you get come knowlage about it come back and continues your reading :D I was kidding with you, I will describe it brieflly. in old version of programing, you have a jsp or html page like this:

user-name : < input type="text"... />

and if you have some other pages which need to show user-name, you have to write it in each page. Consequentlly, if you want to change for example, user-name to UserName you must change them in all of your pages. It is a very heigh risk action. So, for preventing this we write our caption in resource bundle file (.properties) instead of write them in jsp pages. For this, you just create a property file with an optional name, I recommende ApplicationResources.properties and place it in WEB-INF below, for example, /WEB-INF/ApplicationResources.properties. In addition, if you use struts web framework you should change your struts-config.xml as below:

< message-resources parameter="ApplicationResources"/>

and declare your captions in this file. Each cption has 2 parts, key and description. for above example, I will write userName = user-name in resource bundle file. userName is key and user-name is description of it. Now, your jsp page will be like this: < input type="text".../> well, if you want to change user-name to UserName you just change your property file then deploy your project and enjoy it ;)

Now back to main subject i18n, for this one you can make some other property files with the same name(like this, Application ApplicationResources_xx.properties, xx will be changed for each language) beneath the main resource file(for above example ApplicationResources.properties) for each language. for example if you want to make an application with 2 languages Farsi and English, make Application ApplicationResources_en.properties fo English and Application ApplicationResources_fa.properties for Farsi. all of the keys have to the same, but, the description defined for each language. look the following sample:

# ApplicationResources_en.properties
userName = user-name

# ApplicationResources_fa.properties
userName = كاربر

Now, if you change your locale the struts controller change user-name to كاربر.

after that, your projst is like this:

WEB-INF
|
-----ApplicationResources // the main propery file
|
ApplicationResources_en.properties
ApplicationResources_fa.properties

Moreover, I have to add this note that you dont have to write anything in the main property file, but there is this question, what is the default file? for answer to this question you must change your web.xml like this:

< context-param>
< param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale
< param-value>en


In this one, the default file is ApplicationResources_en.properties and if your locale is for example, es, your application shown in English.

JSTL

If you use JSFT tags instead of struts tags, you must declare it in your web.xml as described it:

< context-param>
< param-name>javax.servlet.jsp.jstl.fmt.localizationContext
< param-value>ApplicationResources


and use < fmt:message key="userName"/> in your jsp page.

Multi Resource Bundle

you can have 2 or more property file, for this, you have to declare 2 or more files and write them in struts-config.xml as I mentioned and use bean tag of struts in your jsp page.

for example you have 2 files named, ApplicationResources.properties and orod.properties was located under WEB-INF, so your struts-config.xml will be like this:

< message-resources parameter="ApplicationResources"/>
< message-resources parameter="orod"/>

and write hello = hello in ApplicationResources_en.properties and write bye = bye in orod.properties. when you use will be printed hello in jsp and when you use printed bye in the jsp.

But, if use JSTL instead of struts tags, you have to change web.xml as above and set bundle to introduce specific bundle before < fmt:message .../> by < fmt:setBundle basename="orod"/> or < fmt:bundle basename="orod">

have a good time and enjoy i18n

1 Comments:

Anonymous Anonymous said...

He'd give him a little http://startso11.info/Dar%C5%82%C3%B3wko+zlot+motocyklowy.html punch behind, and the next minute you'd see that frog whirling in the air like a doughnut--see him turn one summerset, or may be a couple, if he got a good start, and come down flat-footed and all right, like a cat.. Homer on his guard http://startso11.info/THURGAU.html for the future; and she was unfeignedly surprised to discover that her best efforts were of no avail.. You come to my house and you will never so much http://rebestal.info/ca%C5%82kiem_jadalna.html as hear of it.. I sold dem mules http://startso11.info/wse.amu.edu.pl.html for three hundred dollars.. But the analysis and--still more distinctly--the synthesis of dreams which http://rebestal.info/do+wynaj%C4%99cia.html lack regression toward pictures, e.. , some dreams in which we dream that we dream. http://startso11.info/ikp.pl.html. Any discussion here http://startso11.info/brudny+gumolit.html is irregular and premature--and must be reserved for the jury-room--after they have retired.. He made me and himself a great many good friends, some of whom I did not afterwards recognize as quickly as Dennis did http://startso11.info/teenwithes.com.html my parishioners.. For the http://startso11.info/wypadek+samochodowy.html pulling off, which serves to represent commercial dishonesty, the dreamer himself gives a second explanation--namely, onanism.. In his essay on The Origin of Language (Linguistic Essays, p. http://startso11.info/ABITURIENT.html. I could easily spare the three http://startso11.info/biura+podr%C3%B3%C5%BCy+w+krakowie.html hundred.. He stopped dancing, http://startso11.info/kleje.html and grinned at them.. There, with http://startso11.info/gra+nba+live+07+w+canada.html all the sudden glory of crystallization, the vague idea took definite form and became the great inspiration of Stuhk's career.. But now don't you want to buy http://startso11.info/www.comex.pl.html a bonnet or a cloak to carry home to your wife? Well, you're whistlin' now, birdie; that's my intention; set 'em all out.. In a few minutes the blaze broke forth with violence, and in an incredibly brief period the entire building http://storyah44.info/pmb+S%C5%82upsk.html was wrapped in flames.. In the psychoses these modes of operation of the psychic apparatus, which are normally suppressed in the waking state, reassert themselves, and then betray their inability to satisfy our wants in the outer http://startso11.info/szyby+samochodowe+gdansk.html world.. The negroes fled--they dared not trust http://startso11.info/o%C5%9Bwiadczenie.html their feelings; even Alicia sniggered unobtrusively; Grandfather Claiborne chuckled, and Aunt Missouri frankly collapsed into her rocking-chair, bubbling with mirth, crying out: Good for you, Ross! Seems you did know how to call on the girls, after all.. In the yard, after the train pulled out, http://startso11.info/ryczyw%C3%B3%C5%82+basen.html he saw his private car under a glaring arc light, and grinned to see it left behind.. Podington gave a howl http://startso11.info/podglondanie.html of horror, and the horse, with high, uplifted head, was obliged to swim.. Podington, who thought they were going to drown. http://rebestal.info/security+center.html..

7:02 AM  

Post a Comment

<< Home