Developing with Eclipse | Developing with JBuilder | Example using Resource Bundles
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.
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
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.
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
You have two options for creating a resource bundle:
button_cancel=Cancel
To get the text that corresponds to the key use:
resource.getString("button_cancel")
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
|
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:
|
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.
import java.util.ResourceBundle;
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.
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") %>