Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

References:


Internationalization is the process of designing an application so that it can be adapted to various languages and regions without engineering changes. Sometimes the term internationalization is abbreviated as i18n, because there are 18 letters between the first "i" and the last "n."
An internationalized program has the following characteristics:
  • With the addition of localized data, the same executable can run worldwide.
  • Textual elements, such as status messages and the GUI component labels, are not hardcoded in the program. Instead they are stored outside the source code and retrieved dynamically.
  • Support for new languages does not require recompilation.
  • Culturally-dependent data, such as dates and currencies, appear in formats that conform to the end user's region and language.
  • It can be localized quickly.

Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text. The term localization is often abbreviated as l10n, because there are 10 letters between the "l" and the "n."
The primary task of localization is translating the user interface elements and documentation. Localization involves not only changing the language interaction, but also other relevant changes such as display of numbers, dates, currency, and so on. Other types of data, such as sounds and images, may require localization if they are culturally sensitive. The better internationalized an application is, the easier it is to localize it for a particular language and character encoding scheme.

Steps for Internationalizing a Program (JavaSE)
Check this little source code example

  1. Create the Properties Files
    This is just a text file with extension ".properties" that has key-value String pairs.
    The name of the properties file is important. For example, the name of the MyMessagesBundle_pt_PT.properties file contains the pt language code and the PT country code. These codes are also used when creating a Locale object.
    Ex. of the the contents of the file:
    greetings = Olá.
    farewell = Adeus.
    inquiry = Como está?
  2. Define the Locale
    Ex.:
    Locale currentLocale = new Locale("pt", "PT");
  3. Create a ResourceBundle
    Ex.:
    ResourceBundle messages = ResourceBundle.getBundle("myproperties.MinhaBundle", currentLocale);
    Where "myproperties" is the package and "MinhaBlundle" the properties file.
  4. Fetch the Text from the ResourceBundle
    Ex.:
    System.out.println(messages.getString("greetings"));
    System.out.println(messages.getString("inquiry"));
Simple sample here How do I use locales and resource bundles to internationalize my application?

Internationalization on Java EE

A good example application for this is: The NetBeans E-commerce Tutorial - Adding Language Support (the tutorial also has other valuable reference links at the bottom).

Some notes:

There are three basic steps that you need to follow to incorporate multilingual support into your web pages.

  1. Create a resource bundle for each language you plan to support:
    A resource bundle is a text file (.properties) suitable for internationalizing applications by separating out all human-visible text strings from your code. This files has key-value pairs for the text used in you app. You'll have one file of this for each supported language.
    Ex file "messages.properties":
    # welcome page
    greeting=Welcome to the online home of the Affable Bean Green Grocer.
    introText=Our unique home delivery service brings you fresh organic produce, dairy, meats, breads and other delicious and healthy items direct to your doorstep.
    
    # categories
    dairy=dairy
    meats=meats
    bakery=bakery
    fruit\ &\ veg=fruit & veg
    
  2. Register the resource bundle with the application by setting a context parameter in the web.xml deployment descriptor:
    The purpose of this step is to inform JSTL's format (i.e., fmt) tag library where it can locate any resource bundles existing in the application. You accomplish this by instructing the application to create a LocalizationContext using the existing resource bundles. This can be done by setting a context parameter in the application's web.xml deployment descriptor.

    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.messages</param-value>
    </context-param>

    Notes:
    - the <param-name> is always "javax.servlet.jsp.jstl.fmt" (the LocalizationContext class belongs to the javax.servlet.jsp.jstl.fmt package).
    - the <param-value> is the "package_name"."resource_bundle_default_name" of your app
  3. In page views, replace 'hard-coded' text with <fmt:message> tags that reference keys in the resource bundles:
    .Replace Hard-Coded Text with <fmt:message> Tags

    In order to apply the localized text of resource bundles to your web pages, you reference the keys from the key-value pairs you created.
    Example:
    <p style="font-size: larger"><fmt:message key='greeting'/></p>

Working with browser preferred locales/languages
When a request is made, the browser passes a list of preferred locales in the Accept-Language HTTP header (you can use Netbeans's HTTP Monitor - Window > Debugging > HTTP Server Monitor - to examine HTTP headers for client requests). The Java runtime environment on the server reads the list and determines the best match based on the locales defined by the application's resource bundles. This match is then recorded in the ServletRequest object, and can be accessed using the getLocale method. For example, you could access the preferred locale from a servlet with the following statement:
request.getLocale();

To determine the language of the preferred locale:
request.getLocale().getLanguage();

This can also be done through EL acessing the same methods through the pageContext.request implicit object to access the ServletRequest for the given client request:
${pageContext.request.locale.language}

Working with user selection language (the user selects the language through the app interface)

With the <fmt:setLocale> tag we can manually switch the language used in the page display:
<fmt:setLocale value="${language}" scope="session" />

Check if a value has been set by the <fmt:setLocale> tag:
You can determine this value using the expression:
${sessionScope['javax.servlet.jsp.jstl.fmt.locale.session']}

Note: javax.servlet.jsp.jstl.fmt.locale.session is the string literal key for the Locale set by the <fmt:setLocale> tag.

This can also be accessed in a servlet:
Locale locale = (Locale) session.getAttribute("javax.servlet.jsp.jstl.fmt.locale.session");
String language = "";

if (locale != null) {
     language = (String) locale.getLanguage();
}

if (!language.isEmpty()) {
     request.setAttribute("language", language);
}



The following code is very useful for tests, outputting:
  1. the client request's preferred language;
  2. the value (if any) set by the <fmt:setLocale> tag;
<code>\${pageContext.request.locale.language}</code>: ${pageContext.request.locale.language}
<br>
<code>\${sessionScope['javax.servlet.jsp.jstl.fmt.locale.session']}</code>: ${sessionScope['javax.servlet.jsp.jstl.fmt.locale.session']}

Sem comentários:

Enviar um comentário