Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to implement Internationalization and Localization in Java

Java was originally designed with the internationalization in mind and includes a number of the classes to make that effort painless. See in this article how to implement Internationalization and Localization in Java

The primary packages/classes used for internationalization represents a generic environment. The Instances of these classes are used by other classes to format the dates and numbers, including the localized strings and other objects in a standard way. There are also classes for dealing with different characters of the encoding.

There are two different types of definition for Internationalization and localization processes in the Java Development system. Localization refers to the adaptation of an application or document content to meet the culture, language and other requirements of a specific region known as 'locale'.

The definition of internationalization may vary. This is a high-level working definition for use with the W3C (World Wide Consortium) Internationalization Activity model. Some people use other terms, such as 'globalization' to refer to the same idea. Internationalization means to organize the application by identifying different geographical regions and providing means to use different varieties which are alternative to the hardcoded values.

The Illustrations are labels, messages, online help texts, graphics, format of the dates, times, numbers, currencies, measurements, and sometimes even the page layouts and the colors.

When an application has been internationalized, it can also be localized for different regions. It provides internationalized application messages, texts, graphics as well as the rules for formatting the date/times and numbers for one or more regions. The localization sometimes abbreviated as L10N, following the same logic as the I18N abbreviation. The support for the new regions can be added without changing the application by simply installing new localized resources.

Note: Internationalization is the process of designing an application so that it can be adapted to various languages and regions without any programming changes. Sometimes the term internationalization is abbreviated as I18N, as there are 18 letters between the first "I" and the last "N".

Process to support Internationalization and Localization:

Java was designed with the goals of making internationalization as a part of its framework including a number of classes. Basically the primary class that is used for internationalization represents a specific geographical region. The instances of these classes are used by other classes to format dates and numbers along with localized strings and objects in an application. Different other classes are also available for dealing with different character encodings as per requirement.

Working with the Locale Class:

All Java classes supporting localization uses a class named java.util.Locale. An instance of this class represents a particular geographical, political, or cultural region and it is specified by a combination of language code and a country code. The Java class that performs the task differs on a user's language and local system so it is also called locale-sensitive operations. Locale-sensitive operations interpret date strings and format the numeric values. We can also create a Locale instance using a constructor (Constructor name and class name are same) that takes the country code and language code as arguments. The following code describes the syntax.

java.util.Locale usLocale = new Locale("en", "US");  /*Setting up US English language*/

In the above code a Locale for U.S. (United State) English is created. Both a language code and a country code are needed to describe locales completely. Actually the language code, combination of two lowercase letters, is defined by the ISO (International Standard Organization) 639 standard available at http://www.ics.uci.edu/pub/ietf/ , http/related/iso639.txt. Also the country code, two uppercase letters, is defined by the (International Standard Organization) ISO 3166 standard, which is available at http://www.chemie.fuberlin. de/diverse/doc/ISO_3166.html.

Formatting Numbers and Dates:

Now we will discuss formatting of numbers and dates. We look at the process a locale can be used. The order of the date generally follows as month, day, and the year so the format is MM-DD-YYYY [04-13-1993]. Java also provides two classes to deal with the formatting of the numbers and dates for an unambiguous locale; they are named as java.text.NumberFormat and java.text.DateFormat. In the normal way the Number Format of the class, formats numbers based on the locale of the underlying operating system. If we use it on a server that is configured to use a U.S. (United State) English locale, it can format them according to American customs process; on a server configured with an Italian locale, it also formats them according to Italian customs, and so on…. Nevertheless we can also explicitly specify the locale to format statistics rendering to the rules for other locales than the one used by the operating system. We will soon see how to tell the Java Standard Tag Library formatting actions to use a specific locale or figure out which one to use based on the Accept-Language header. The DateFormat class works basically in the same way, because the day and month names are sometimes spelled out in the local language.

Generating Localized Output:

Now we will discuss about generating localized output process. Instead of using the internationalization classes directly in the pages; we will use the Java Standard Tag Library I18N actions based on these classes. The text, numbers, and dates are available in different languages. I18N will provide support for all localized output.

Using Localized Text:

As we know, automatic translation of numbers and dates into local language is a great help for software developers. Until programmed translation software is a lot smarter than it is today, we had to translate all the text used in the application. The set of Java classes can help us pick the right version for a specific locale. Actually the main class for dealing with the localized resources like text, images, video and sounds is named as java.util.ResourceBundle. This class is actually the abstract superclass for the two subclasses that prepare the real work, ListResourceBundle and PropertyResourceBundle. It provides the methods to get an appropriate subclass instance, hiding the details about which subclass actually provides the resources. Details about the difference between these two subclasses are beyond the scope of this article. It is enough to say that the Java Standard Tag Library actions can use the resources provided through either of them. Generally in most of the web applications an instance of the PropertResourceBundle is used. A PropertyResourceBundle instance is associated with the named set of the localized text resources; a key identifies each of the resource. The keys and their corresponding text values are stored in a regular text file format, known as a resource bundle file.

Listing 1: Showing storing mechanism

site_name=Welcome to SGC Campus….
company_logo_photo=/images/logo_cei.gif
welcome_msg=Hello SGC..!

In the code Listing 1, it shows the text ‘Welcome to SGC Campus….’ in the site map as well as through the variable company_logo_photo. It will be stored as logo_cei.gif and welcome_msg variable will show “Hello SGC..!” message here.

There are three keys present; site_name, company_logo_photo, and welcome_msg. All have been assigned some values. Actually the key is a string, without the space or supplementary singular characters, and the value is any text. If the value spans more than one line, the line break must be escaped with a backslash

Listing 2: Sowing properties file entries

character (\):
multi_line_msg=This text value\	   /*Here this option is used to separate the lines...*/
continues on the next line.

Generally the backsplash character is used for the escape sequence but here this character is used for separating the line. Here we write down “\” to continue the line after next line.

The file must use the .properties extension, for instance sitetext.properties, and can be located in the specific classpath that is used by the Java Virtual Machine (JVM). In web applications, we can store the file in the web application's WEB-INF/classes directory; this point is used for the directory that is always included in the classpath.

To localize an application, we create separate resource bundle files for each locale, all with the same main name (called the base name), with the unique suffixes to identify the locale. As per the illustration, a file named as sitetext_es_MX.properties, where es is the language code for Spanish, as well as the MX is the country code for Mexico, can contain the text for the Mexican Spanish locale. The Java Standard Tag Library action deals with the localized text and finds the resource bundle that most closely matches the selected locale or the default bundle if there is no match.

Process to use One Page for Multiple Locales scenario:

Basically here we will discuss with a specimen of the polling.jsp page. The locale selection is then used to produce the text in the corresponding language throughout the pages.

In this section we will try with the simple Illustration of language selection and vote page. The name of the page is polling.jsp

Listing 3: Sample JSP file to show locale value settings

<fmt:setBundle> Pick the best one based on the Accept-Language header. --%>
<c:if test="${param.language == 'en'}"> /*This  code is for the English Langauge*/
<fmt:setLocale value="en" scope="session" />
</c:if>
<c:if test="${param.language == 'sv'}">
<fmt:setLocale value="sv" scope="session" />
</c:if>
<c:if test="${param.language == 'de'}">
<fmt:setLocale value="de" scope="session" />
</c:if>
<fmt:setBundle basename="pages" var="pagesBundle" />
<fmt:setBundle basename="labels" scope="session" />

In this scenario we are working with the field to pass the data through the parameter. Here, we also generate a session whichis the server side variable. The variable it stored at server and control server side process. In the Listing 3 we are focusing on the language information passing through the parameter. Here we have set three locale values with session scope. The ‘param.language’ is used to assign the language values. Then ‘setLocale’ is used to set values and ‘scope’ variable is used to set ‘session’ scope.

Listing 4: Sample form with locale setting

<fmt:message key="select_language" />:
<form action="polling.jsp">
<p>
<c:set var="currLang" value="${pagesBundle.locale.language}" />
<input type="radio" name="language" value="en" <c:if test="${currLang == 'en'}">checked</c:if>>
<fmt:message key="english" /><br>
<input type="radio" name="language" value="sv"
<c:if test="${currLang == 'sv'}">checked</c:if>>
<fmt:message key="swedish" /><br>
<input type="radio" name="language" value="de"
<c:if test="${currLang == 'de'}">checked</c:if>>
<fmt:message key="german" /><br>
<p>
<input type="submit" value="<fmt:message key="new_language" />">
</form>
<a href="<fmt:message key="details_page" bundle="${pagesBundle}" />">
<fmt:message key="question"/>
</a>

In the above code we are setting up a simple form with a submit button. The button also executes the calling of the page polling.jsp. By using a radio button, user can easily choose the language. Through the message key format we are displaying the language. After that, the radio button accepts the exact option which is set by the user.

Listing 5:
<form action="calculate.jsp" method="post">
<input type="radio" name="answerId" value="1" checked>
<fmt:message key="answer1" />
<br>
<input type="radio" name="answerId" value="2">
<fmt:message key="answer2" />
<br>
<input type="radio" name="answerId" value="3">
<fmt:message key="answer3" />
<p>
<input type="submit"
value="<fmt:message key="submit" />">
</form>

In the above program we are setting up a simple form with a submit button. The button is also executed for calling the page calculate.jsp. From the above scenario we measure the answer of the polling via clicking the radio button. This radio button is used for the executing the answer. This answer is generally differentiated via different variables like answer1, answer2, answer3. Here a messagekey is also present for different types of languages. The values for corresponding message keys are 1, 2 and 3.

Using a Separate Page per Locale:

The Java Standard Tag Library I18N actions make it easy to use the same page for all locales. As described earlier, sometimes it is better to use a separate page per locale. The poll illustration uses this approach for the detailed description of the question. As exposed in the illustration the polling.jsp page uses a resource bundle with the base name pages for holding the name of the details page for each locale.

Now we will try to discuss the pages_sv.properties

details_page=details_sv.jsp

This process is possible for using the and the action to dynamically generate a link to a separate page for each locale option:

<a href="<fmt:message key="details_of_page" bundle="${pagesBundle}" />">
<fmt:message key="question" />
</a>

Here the message key is generated for the question setting process. Through this system the message key is measured for the polling question.

In this bundle, we have used the action explicitly since the pages bundles are not the default bundles.

All remaining is to create a page per supported locale. The following example shows the Swedish page.

Swedish details page (details_sv.jsp)

<fmt:message key="title" />
</title>
</head>
<body bgcolor="yellow">
<h1>
<font color="blue">
<fmt:message key="question" />
</font>
</h1>
<font color="blue">
<p>
</font>
 

We generally found that most of the page consists of Swedish text. The color of the Swedish flag is yellow and blue which is also used as the background, header, and text color. Actually the elements of the pages for other locales follow the same configuration. When the amount of text is very large, and other details of the page are differing, like the colors in this illustration, it is generally convenient to the use a separate page for each locale instead of the one-page approach described earlier.

Handling Localized Input:

Input and output are very serious part of this article. So far we have discussed the process, how we can render pages in different languages. It is very important that most of the applications, which also need to deal with localized input, Internationalization are very urgent requirement. As long as we are only associated with Western European languages (not a computer languge), the only thing that we have to select carefully is by what means to interpret the dates and numbers. The Java Standard Tag Library I18N actions can help us with this as per our requirement.

Internationalization vs. Localization:

Basically the whole world has their preferred classifications for Localization and Inter nationalization. We provide some general way, as well as high-level descriptions of how we tend to use these terms.

Localization:

The Localization is a process that references to the adaptation of the product, and the application or the document content to meet the language, and the cultural and other requirements of a specific target market (a locale).

Internationalization:

A definition of the internationalization is not fixed. This is a high-level working definition for use with W3C Internationalization Activity material.

Basically the Internationalization and localization is one type of very advanced concept in the Java programming language. It is also a theoretical concept as well as practical concept for the programmers. In the above article we have tried to deliver all types of possible conversion that is coming from the working of Internationalization and localization. So here we discussed points like working with the local class, formatting time and date, handling localize output, handling localize input, separate page per locate, localize page or text etc. Some relevant example is also given for understanding the process. Using these article programmers or the developers must try to understand what type’s of systematic procedure is used for Internationalization and localization.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login