Developing with Eclipse  |  Developing with JBuilder  |  Example using Resource Bundles

Internationalization

Internationalization is a feature for creating Mobile Infrastructure Client Application that display messages and other text strings in the selected language. The language can be selected in the country settings of the client and in the Web browser. Language support for Mobile Infrastructure Client Applications uses the Java locale technology with resource bundles. The resource bundle contains the text strings that are displayed by the Mobile Infrastructure application according to the language (locale) settings.

Language Settings in the Browser

You can set up the language code in your browser. The servlet checks the language code and adjusts the language (if no default language was defined for the user). In the Microsoft Internet Explorer you can select the language code by choosing:
Tools -> Internet Options -> Languages.
The language code you want to use in your browser has to be located at the top of your list.

Example: Selecting German as browser language in the Microsoft Internet Explorer

 

Naming Conventions and Location of the Resource Bundles

To define the name of the resource bundle to be used we use the com.sap.ip.me.api.services.MEResourceBundle package. It is an extension of the java.util.ResourceBundle.

private static final ResourceBundle ME_RESOURCE_BUNDLE = MEResourceBundle.getBundle("myresource");

Note:

The _xx mentioned above stands for the country ISO code of the language you want to support. If you want to support English and German, you have to supply the files:

myresource_de.properties (German)
myresource_en.properties
(English)
myresource.properties (fall back for all other ISO codes)

The language properties must be located in folder /classes  or packed in the .jar file of the Mobile Infrastructure Client Application that is located in folder /lib of the Mobile Infrastructure Client Application. If you work with the MDK plugin for Eclipse you can select where to store the resource bundles with the MDK project properties in Eclipse (MDK Project Properties).

Note:

If the resource file cannot be found, the method getBundle() returns a null value. This causes "null pointer exceptions" when you try to access the resources later on.
If a component does not find the resource bundles, you should restart Tomcat after copying the resource files to the correct folder.

 

Using Resource Bundles

The com.sap.ip.me.api.services.MEResourceBundle package provides the getBundle method for accessing the resource bundles.

Example:

private static final ResourceBundle ME_RESOURCE_BUNDLE = MEResourceBundle.getBundle(BUNDLE_NAME);

The getBundle method returns the resource object. If the value returned is null, the resource bundle could not be found. The resource object provides the method getString(string id), which returns the text string in the language (locale) settings with the given ID.

Example:

String buttonc_text = ME_RESOURCE_BUNDLE.getString("button_cancel")

buttonc_text retrieves the text string stored in the resource bundle with the ID "button_cancel".

The entry in the resource bundle would be:

Example for an entry:

in myresource_de.properties (German)

button_cancel=Abbruch

in myresource_en.properties (English)

button_cancel=Cancel

in myresource.properties (fall back for all other ISO codes)

button_cancel=Cancel

 

Creating Resource Bundles for a Mobile Infrastructure Application

You have two options for creating a resource bundle:

Manually Creating Resource Bundle Files

  1. Create a resource bundle file in the class directory of your application.
    If you are using the default name for resource bundles, name the resource file localization.properties. Otherwise use the name you defined in the component profile (as described above).

  2. Store your text strings in the resource files (see "Example for an entry" above). A text string starts with an ID. The ID should be self-explanatory, such as button_cancel. The ID is directly (no spaces) followed by an equals sign (=) and the text string that appears on the user interface in the language of the resource bundle file:
    Example of an entry in myresource_en.properties (English):

    button_cancel=Cancel


    To get the text that corresponds to the key use:

    resource.getString("button_cancel")


Creating Resource Bundle Files with the Eclipse Wizard

  1. In Java view of Eclipse, select the Java file that includes the text strings and choose Source -> Externalize Strings.
  2. A dialog window lets you select strings that should not be converted to external strings. You can also change the key of the strings to a name more suitable for your application. When finished select Next...
  3. Select a name and the package for the resource bundle. We suggest to store the resource bundle directly under the /java/myPackage source folder. This makes sure, that the resource bundles are always placed in the class path automatically by Eclipse and that the MDK plugin can find the resource bundles.
  4. On Finish, Eclipse will create a Messages.java file which implements a getString method for the resource bundle. Messages.java implements java.util.ResourceBundle.

In the state your application is right now, the Java resource bundles will be used. So the locale setting of your computer (regional settings) controls the language. In order to use the locale setting of the Mobile Infrastructure we have to make changes to Messages.java and our servlet class. In the further description the servlet class is called SystemInfo.

Changes in Messages.java

Before changes. (Message.java created by the Eclipse wizard. The String variable BUNDLE_NAME contains the name of the resource bundle you entered while creating the resource bundle. You need the name of the resource bundle for the changes in SystemInfo.java).

package example1;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Messages {

private static final String BUNDLE_NAME = "example1.configExampleResBundle";
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

private Messages() {
}

public static String getString(String key) {
  try {
      return RESOURCE_BUNDLE.getString(key);
   } catch (MissingResourceException e) {
    return '!' + key + '!';
   }
}

}

After changes (SystemInfo is the servlet class)

package example1;

import java.util.MissingResourceException;

public class Messages {

private Messages() {
}

public static String getString(String key) {
  try {
      return SystemInfo.RESOURCE_BUNDLE.getString(key);
  } catch (MissingResourceException e) {
    return '!' + key + '!';
  }
}

}

 

Changes in SystemInfo.java

Add the declaration of RESOURCE_BUNDLE ("public static MEResourceBundle RESOURCE_BUNDLE;") at the beginning of the SystemInfo.java.
Example (line to add is in bold characters):

public class SystemInfo extends AbstractMEHttpServlet implements Constants {
public static MEResourceBundle RESOURCE_BUNDLE;

Changes in the doInitialize method:

Before changes:

public void doInitialize() throws ServletException {
    setResourceBundle("/NoResourceBundleUsed");
}

After changes:

public void doInitialize() throws ServletException {
     setResourceBundle("example1.configExampleResBundle");
     RESOURCE_BUNDLE = getResourceBundle();
}


The parameter for setResourceBundle, "example1.configExampleResBundle", is the string we removed in the Messages.java before.

If you do not want to to use the Message.java file any more, you can also move the getString method into the servlet class and delete Message.java.

 

The resource bundles must have a unique name (package + filename) in order to avoid conflicts with the Mobile Infrastructure class loader. See, Import a .war file into a new project for more details.

 

Creating Resource Bundle Files with the JBuilder Wizard

  1. In JBuilder, select the Java file that includes the text strings and choose Wizard -> Resource Strings.
  2. In the dialog box, choose New...
  3. In the Name field, enter the value localization or the name you defined in the default.properties file.
  4. In the Type field, choose PropertyResourceBundle
  5. Confirm with Ok
  6. Choose Next to proceed to the next step in the wizard
  7. Choose the texts that you actually want to have replaced in your code and choose Finish
  8. The wizard adds to your code an import statement for importing the package java.util.ResourceBundle:

    import java.util.ResourceBundle;

  9. It also adds the following line to your Java code:

    static ResourceBundle res = ResourceBundle.getBundle("localization");


    Replace this line with the following line at the beginning of your doContent method:

    ResourceBundle resource = myRequest.getResourceBundle();

     

    Note:
    If you use text strings that start or end with blanks or you use a colon (:) or other special characters, the JBuilder Wizard enters backslashes to avoid left and right trimming of the text string. To avoid exceptions in the MI, remove the backslashes.

    Example:

    setString(" a blank before");


    The JBuilder Wizard would create following entry

    ablank_before=\ a blank before

    The \ has to be removed. Since the leading blank is trimmed when you receive the string with the getString("ablank_before") method, you have to adjust your coding accordingly.

     

Using Resource Bundles in JSP

To use the MI Resource bundles we recommend to place the RESOURCE_BUNDLE object into the session context, like a bean.

Example:

Defining the RESOURCE_BUNDLE object (see also the steps mentioned in "Creating Resource Bundle Files with the Eclipse Wizard" above):

public void doInitialize() throws ServletException {
     setResourceBundle("example1.configExampleResBundle");
     MEResourceBundle RESOURCE_BUNDLE = getResourceBundle();
}

Placing the RESOURCE_BUNDLE object in the session context:

request.getSession().setAttribute("resBundle", RESOURCE_BUNDLE);

Getting the resource bundle object out of the session context in the JSP:

<jsp:useBean id="resBundle" scope="session" class="com.sap.ip.me.api.services.MEResourceBundle" />

The name used in the id parameter of the useBean command, has to match the first parameter used in the request.getSession().setAttribute call shown above.

Using the resource bundle object to place a text in the JSP:

<%=resBundle.getString("resource_Key") %>

 

Examples

Example using Resource Bundles