Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Using localised images in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia 5800 XpressMusic, Nokia 6021, Nokia 6131, C3-01, C5-03, Asha 306, Nokia E7-00
CompatibilityArticle
Created: dekudin (26 Feb 2009)
Last edited: hamishwillee (29 Jul 2013)

Contents

Overview

This code snippet demonstrates how to implement the localisation of images in a Java ME application.

One way to implement this is to have variations of an image for each locale in the application, and have some resource to determine which image to use with the specified locale. These settings may be in the manifest and/or the JAD file of the application.

To do this, add the following lines in the manifest of the application:

  1. img-en: Hello-en.png
  2. img-fr: Hello-fr.png
  3. title-en: English
  4. title-fr: French


The first two lines contain the file names of localised images for English (en) and French (fr). During runtime, the application gets these lines with the MIDlet.getAppProperty() method, and loads the appropriate image. By getting the current locale using the System.getProperty() method, and then loading the required image, localisation of images can be achieved.

Source file: manifest.mf

MIDlet-1: UsingLocalizedImagesMidlet, , UsingLocalizedImagesMidlet
MIDlet-Vendor: Vendor
title-en: English
img-en: Hello-en.png
MIDlet-Name: UsingLocalizedImages
title-fr: French
img-fr: Hello-fr.png
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0


Source file: UsingLocalizedImagesMidlet.java

    /**
* Outputs localized images to the specified form
* @param form - form where fields with localized timestamps will be placed
* @throws java.lang.Exception if error occurs while localizing images.
*/

private void showLocalizedImages(Form form) throws Exception {
// Get current locale
String currentLocale = System.getProperty("microedition.locale");
TextField locField = new TextField("CurrentLocale", currentLocale,
128, TextField.UNEDITABLE);
form.append(locField);
 
// Outputs localized images to the form
for(int n = 0; n < locales.length; n++) {
addLocalizedImage(form, locales[n]);
}
}
 
/**
* Adds localized image to form.
* @param form - the form for placing image item with localized image
* @param locale - locale for localizing image
*/

private void addLocalizedImage(Form form, String locale)
throws Exception {
String title = getLocaleTitle(locale);
String imgName = getLocaleImageName(locale);
if(title == null || imgName == null) {
throw new Exception("Unable to get applicaton settings.");
}
 
StringBuffer buffer = new StringBuffer();
buffer.append(title);
buffer.append(" (");
buffer.append(imgName);
buffer.append(")");
 
title = buffer.toString();
 
InputStream ios = this.getClass().getResourceAsStream("/" + imgName);
 
Image image = Image.createImage(ios);
 
ImageItem imgItem = new ImageItem(title, image, 0, null);
form.append(imgItem);
}
 
/**
* Gets locale title from application settings
* @param locale - the id determining returning title for locale
* @return title for specified locale
*/

private String getLocaleTitle(String locale) {
return getAppProperty("title-" + locale);
}
 
/**
* Gets locale date format from application settings
* @param locale - the id determining returning name of localized image
* @return localized image for specified locale
*/

private String getLocaleImageName(String locale) {
return getAppProperty("img-" + locale);
}

Postconditions

When running the snippet, image fields with variations of an image containing the word 'Hello' in French and English are displayed.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.

This page was last modified on 29 July 2013, at 04:59.
115 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×