I18n

From OpenI Wiki

Table of contents

1 Best practices to i18n'ize
2 Resource files
3 Naming Convention
4 Locale precedence
5 Sample Usage
6 User Profile
7 Usefull Links

i18n

Internationalization is based on the java Locale objet and resource bundles. A Locale object represents a geographical, political, or cultural region, and is used with the various Java classes that perform locale-specific operations. A ResourceBundle is a standart text file that a program or web-page can use to retrieve a String, either a text or a formatting pattern, for a particular Locale. So, each locale must have its own resource file.

Best practices to i18n'ize

A short list of recommendations should be taken into account in order to make OpenI work properly:

  1. Everytime you have to show a user text, either in a web page or in a java class, do no hard code it. Use a ResourceBundle. I'm unifing all properties files in just one "org.openi.labels.properties".
  2. When a Controller can perform several activities, use do not use a user label to know which action to perform. It will not work if localized!! The name of the object should be used instead of the label. A clear example of such code is DatasourceFormController, where the jsp button's name is "action", so the servlet requests the parameter "action" and compares to its label value "Save".
In example, in the function "validateJdbcParam" you have the code:
   if ("Save".equalsIgnoreCase(request.getParameter("action"))) {  ... }
A localizable code would be: (provided the jsp buttons name is "save" instead of "action", no matter what the button value is)
   if (request.getParameter("save") != null) { ... }
Take also into account that Mondrian and MSAS can be localized. Therefore, "Measures" is not allways called this way and any code relying on this could not work properly in other languages.
A good idea is to mark, into the resource file, all the labels with and XX, so you see it while testing and you clearly identify what can be localizable and what cannot. I mean: instead of declaring, in the resouce file, a label as error.message="Can't connect", you can put error.message="Can't connectXX", so you see it on the screen while testing. For the release, it's as easy as find & replace all XX for empty, with any editor (even the notepad)
  1. I still haven't found with this, but when designing a user page, keep in mind that many languages are longer than english. In an example, spanish is a 30% longer than english in average. Look at this english label and its spanish translation:
Datasource Name
Nombre de la Fuente de Datos

Resource files

The name of a ResourceBundle file will be in the order labels_<language>.properties. , which is located at org.openi. In tomcat, its location is [TOMCAT-HOME]/openi/WEB-INF/classes/org/openi. Ex: labels_es.properties, labels_es.properties, text files for spanish and english.
If the resource file for a certain locale is not found, the application will look for the default resource file. Default resource file name is labels.properties and it must be located at the same place of the rest of the resource files.

The resource file is a standard properties file with name=value pairs, one per line. Examples of pairs:

project_list.selectproject=Please select a project to continue
project_list.currentproject=Current project
project_list.availableprojectlist=Available Project List

Naming Convention

The Name part of the pair is named using the following convention:

location format sample
If label of a web page [name of the page w/o extension].short_description project_list.currentproject=Current project
If text in a java class java_[name of the Class].short_description java_Project.adminmenu.Configure_Datasource=Configure Datasource
If a label is used across the application common.short_description common.datefullformat=EEEE, dd MMMM yyyy


Locale precedence

The precedence in the selection of a language is the following:

  1. User's profile definition
  2. Company's settings
  3. Browser's default settings

Sample Usage

Retrieving text is jsp pages

file property name/value retrieve in jsp
web/WEB-INF/classes/org/openi/labels_en.properties project_list.selectproject=Please select a project to continue <fmt:message key="project_list.selectproject"/>
web/WEB-INF/classes/org/openi/labels_en.properties project_list.currentproject=Current project <fmt:message key="project_list.currentproject"/>
web/WEB-INF/classes/org/openi/labels_en.properties project_list.availableprojectlist=Available Project List <fmt:message key="project_list.availableprojectlist"/>


Retrieving text in java classes

ProjectContext prjCtx = (ProjectContext)session.getAttribute("projectContext");

String text = prjCtx.getResourceString("java_AuthorizationFilter.invalidProject");


i18n in java classes should be used only to retrieve user's text. Throwing errors should be still in English, since the average user will not understand the stack trace and most IT professionals can read English more or less fluently.

User Profile

See User Profile

Usefull Links

Core Java Internationalization page (http://java.sun.com/j2se/corejava/intl/index.jsp)
Standart supported locales (http://java.sun.com/j2se/1.5.0/docs/guide/intl/locale.doc.html)