IBM'er IBM'er - 1 year ago 114
Java Question

How to internationalize a Java web application?

I learnt from Google that Internationalization is the process by which I can make my
web application to use all languages. I want to understand Unicode for the process of internationalization, so I learnt about Unicode from here and there.

I am able to understand about Unicode that how a charset set in encoded to bytes and again bytes decoded to charset. But I don't know how to move forward further. I want to learn how to compare strings and I need to know how to implement internationalization in my web application. Any Suggestions Please? Please guide me.

My Objective:

My main objective is to develop a Web Application for Translation (English to Arabic & vice versa). I want to follow Internationalization. I wish to run my web Application for translation in all the three browsers namely FF, Chrome, IE. How do I achieve this?

Answer Source

In case of a basic JSP/Servlet webapplication, the basic approach would be using JSTL fmt taglib in combination with resource bundles. Resource bundles contain key-value pairs where the key is a constant which is the same for all languages and the value differs per language. Resource bundles are usually properties files which are loaded by ResourceBundle API. This can however be customized so that you can load the key-value pairs from for example a database.

Here's an example how to internationalize the login form of your webapplication with properties file based resource bundles.

  1. Create the following files and put them in some package, e.g. com.example.i18n (in case of Maven, put them in the package structure inside src/main/resources). (contains key-value pairs in the default language, usually English)

    login.label.username = Username
    login.label.password = Password
    login.button.submit = Sign in
 (contains Dutch (nl) key-value pairs)

    login.label.username = Gebruikersnaam
    login.label.password = Wachtwoord
    login.button.submit = Inloggen
 (contains Spanish (es) key-value pairs)

    login.label.username = Nombre de usuario
    login.label.password = ContraseƱa
    login.button.submit = Acceder

    The resource bundle filename should adhere the following pattern The _ll part should be the lowercase ISO 693-1 language code. It is optional and only required whenever the _CC part is present. The _CC part should be the uppercase ISO 3166-1 Alpha-2 country code. It is optional and often only used to distinguish between country-specific language dialects, like American English (_en_US) and British English (_en_GB).

  2. If not done yet, install JSTL. If you're running on a Servlet 2.5 container or newer (Tomcat 6.0 and so on) and your web.xml is declared conform the Servlet 2.5 specification, then just put jstl-1.2.jar in webapp's /WEB-INF/lib folder.

  3. Create the following example JSP file and put it in web content folder.


    <%@ page pageEncoding="UTF-8" %>
    <%@ taglib prefix="c" uri="" %>
    <%@ taglib prefix="fmt" uri="" %>
    <c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
    <fmt:setLocale value="${language}" />
    <fmt:setBundle basename="com.example.i18n.text" />
    <!DOCTYPE html>
    <html lang="${language}">
            <title>JSP/JSTL i18n demo</title>
                <select id="language" name="language" onchange="submit()">
                    <option value="en" ${language == 'en' ? 'selected' : ''}>English</option>
                    <option value="nl" ${language == 'nl' ? 'selected' : ''}>Nederlands</option>
                    <option value="es" ${language == 'es' ? 'selected' : ''}>EspaƱol</option>
            <form method="post">
                <label for="username"><fmt:message key="login.label.username" />:</label>
                <input type="text" id="username" name="username">
                <label for="password"><fmt:message key="login.label.password" />:</label>
                <input type="password" id="password" name="password">
                <fmt:message key="login.button.submit" var="buttonValue" />
                <input type="submit" name="submit" value="${buttonValue}">

    The <c:set var="language"> manages the current language. If the language was supplied as request parameter (by language dropdown), then it will be set. Else if the language was already previously set in the session, then stick to it instead. Else use the user supplied locale in the request header.

    The <fmt:setLocale> sets the locale for resource bundle. It's important that this line is before the <fmt:setBundle>.

    The <fmt:setBundle> initializes the resource bundle by its base name (that is, the full qualified package name until with the sole name without the _ll_CC specifier).

    The <fmt:message> retrieves the message value by the specified bundle key.

    The <html lang="${language}"> informs the searchbots what language the page is in so that it won't be marked as duplicate content (thus, good for SEO).

    The language dropdown will immediately submit by JavaScript when another language is chosen and the page will be refreshed with the newly chosen language.

You however need to keep in mind that properties files are by default read using ISO-8859-1 character encoding. You would need to escape them by unicode escapes. This can be done using the JDK-supplied native2ascii.exe tool. See also this article section for more detail.

A theoretical alternative would be to supply a bundle with a custom Control to load those files as UTF-8, but that's unfortunately not supported by the basic JSTL fmt taglib. You would need to manage it all yourself with help of a Filter. There are (MVC) frameworks which can handle this in a more transparent manner, like JSF, see also this article.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download