×
Namespaces

Variants
Actions
(Difference between revisions)

Localising app attributes in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:Code Snippet]][[Category:Localization]][[Category:Code Snippet]][[Category:S60 3rd Edition FP1]][[Category:Symbian]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]][[Category:Series 40 Developer Platform 2.0]][[Category:Series 40]][[Category:Nokia Belle]][[Category:Java Runtime 2.3 for Symbian]][[Category:Code Examples]]
__NOEDITSECTION__
+
{{Abstract|This code snippet is one of the series of snippets that demonstrate how to implement multi-language applications in Java ME. }}
{{KBCS}}
+
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
|-
+
|'''ID''' || CS001094
+
|'''Creation date''' || September 16, 2008
+
|-
+
|'''Platform''' || S60 3rd Edition, FP1
+
|'''Tested on devices''' || Nokia N95 8GB
+
|-
+
|'''Category''' || Java ME
+
|'''Subcategory''' || Localization
+
|}
+
  
 
+
{{ArticleMetaData <!-- v1.2 -->
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
|sourcecode= [[Media:LocalisingAppAttributes.zip]]
|-
+
|installfile= [[Media:LocalisingAppAttributes binaries.zip]]
|'''Keywords (APIs, classes, methods, functions):''' java.lang.System, javax.microedition.midlet.MIDlet, java.lang.System.getProperty(), javax.microedition.midlet.MIDlet.getAppProperty()
+
|devices= Nokia N95 8GB, C3-01, Asha 306, Nokia E7-00
|}
+
|sdk= [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.1 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java (beta)], [http://www.developer.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html/ Nokia Symbian SDKs]
 +
|platform= Series 40, S60, Nokia Belle
 +
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= java.lang.System, javax.microedition.midlet.MIDlet, java.lang.System.getProperty(), javax.microedition.midlet.MIDlet.getAppProperty()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20080827
 +
|author= [[User:Tapla]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001094
 +
}}
  
 
==Overview==
 
==Overview==
  
This code snippet is one of the series of snippets that demonstrate how to implement multi-language applications in Java ME. There are several techniques for localizing a MIDlet:
+
There are several techniques for localizing a MIDlet:
 
+
 
* Using application attributes
 
* Using application attributes
 
* Using text files
 
* Using text files
Line 30: Line 36:
 
This snippet describes the first technique; using application attributes.
 
This snippet describes the first technique; using application attributes.
  
Generally speaking, application attributes (or MIDlet attributes, or system properties) are key-value pairs that represent information about the MIDP application. Attributes can be specified in the application descriptor file (<tt>.jad</tt>), in the manifest file (<tt>manifest.mf</tt>), or both. There are specific rules about how the attributes are located and how possible overlappings are resolved, but they are outside of the scope of this code snippet. See the "See also" section for more information.
+
Generally speaking, application attributes (or MIDlet attributes, or system properties) are key-value pairs that represent information about the MIDP application. Attributes can be specified in the application descriptor file ({{Icode|.jad}}), in the manifest file ({{Icode|manifest.mf}}), or both. There are specific rules about how the attributes are located and how possible overlappings are resolved, but they are outside of the scope of this code snippet. See the "See also" section for more information.
  
 
In the technique presented in this snippet, the attributes related to localization are specified in the JAD file. This allows adding languages easily since no coding is required. On top of that, it is possible to create separate JAD files for different locales, in which case the application provider can allow the user to download only the file the user is interested in. On the other hand, this approach may not be useful for a large amount of resources because reading large JAD files may cause performance issues. Also note that JAD files address only string resources.
 
In the technique presented in this snippet, the attributes related to localization are specified in the JAD file. This allows adding languages easily since no coding is required. On top of that, it is possible to create separate JAD files for different locales, in which case the application provider can allow the user to download only the file the user is interested in. On the other hand, this approach may not be useful for a large amount of resources because reading large JAD files may cause performance issues. Also note that JAD files address only string resources.
Line 48: Line 54:
 
</code>
 
</code>
  
'''Note''': In the JAD file above, the French title (<tt>title-fr</tt>) is omitted to demonstrate the usage of default values when retrieving the attributes. See the <tt>localize()</tt> method for implementation details.
+
'''Note''': In the JAD file above, the French title ({{Icode|title-fr}}) is omitted to demonstrate the usage of default values when retrieving the attributes. See the {{Icode|localize()}} method for implementation details.
  
 
==Source==
 
==Source==
Line 166: Line 172:
  
 
* [http://developers.sun.com/mobility/midp/ttips/getAppProperty/index.html Retrieving MIDlet attributes]
 
* [http://developers.sun.com/mobility/midp/ttips/getAppProperty/index.html Retrieving MIDlet attributes]
* [[CS001095 - L10n: Text files]]
+
* [[Localising text files in Java ME]]
* [[CS001096 - L10n: Resource bundles]]
+
* [[Localising resource bundles in Java ME]]
 
+
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:Code Examples]][[Category:Localization]]
+

Revision as of 08:07, 2 October 2012

This code snippet is one of the series of snippets that demonstrate how to implement multi-language applications in Java ME.

Article Metadata
Code ExampleTested with
Devices(s): Nokia N95 8GB, C3-01, Asha 306, Nokia E7-00
CompatibilityArticle
Keywords: java.lang.System, javax.microedition.midlet.MIDlet, java.lang.System.getProperty(), javax.microedition.midlet.MIDlet.getAppProperty()
Created: tapla (27 Aug 2008)
Last edited: hamishwillee (02 Oct 2012)

Contents

Overview

There are several techniques for localizing a MIDlet:

  • Using application attributes
  • Using text files
  • Using resource bundles

This snippet describes the first technique; using application attributes.

Generally speaking, application attributes (or MIDlet attributes, or system properties) are key-value pairs that represent information about the MIDP application. Attributes can be specified in the application descriptor file (.jad), in the manifest file (manifest.mf), or both. There are specific rules about how the attributes are located and how possible overlappings are resolved, but they are outside of the scope of this code snippet. See the "See also" section for more information.

In the technique presented in this snippet, the attributes related to localization are specified in the JAD file. This allows adding languages easily since no coding is required. On top of that, it is possible to create separate JAD files for different locales, in which case the application provider can allow the user to download only the file the user is interested in. On the other hand, this approach may not be useful for a large amount of resources because reading large JAD files may cause performance issues. Also note that JAD files address only string resources.

The JAD file

exit-en: Exit
exit-fr: Quitter
localeLbl-en: Locale
localeLbl-fr: Locale
text-en: Here's some text.
text-fr: Voici du texte.
textLbl-en: Text
textLbl-fr: Texte
title-en: Localization example

Note: In the JAD file above, the French title (title-fr) is omitted to demonstrate the usage of default values when retrieving the attributes. See the localize() method for implementation details.

Source

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class L10nMIDlet extends MIDlet implements CommandListener {
private Form mainForm;
private Command exitCommand;
private String locale;
 
/**
* Constructor. Constructs the object and initializes displayables.
*/

public L10nMIDlet() {
// Obtain the system locale
locale = System.getProperty("microedition.locale");
 
initUI();
}
 
public void initUI() {
// Initialize the form
mainForm = new Form(localize("title", "Localization example"));
 
// Display the locale
StringItem localeItem = new StringItem(localize("localeLbl", "Locale"),
locale);
mainForm.append(localeItem);
 
// Display a localized text string
StringItem textItem = new StringItem(localize("textLbl", "Text"),
localize("text", "Here's some text."));
mainForm.append(textItem);
 
// Create the exit command
exitCommand = new Command(localize("exit", "Exit"), Command.EXIT, 1);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
}
 
/**
* Localizes a string.
* @param key the key used for identifying a specific value in the
* application descriptor file
* @param def the default value in case "key" is not found from the
* application descriptor file
* @return the message localized; "def" if the key is not found
*/

private String localize(String key, String def) {
// Append the current locale to the specified key and use the produced
// string to obtain the property from the application descriptor file
String value = getAppProperty(key + "-" + locale);
if (value == null) {
value = def;
}
return value;
}
 
/**
* From MIDlet.
* Called when the MIDlet is started.
*/

public void startApp() {
// The initial display is the first form
Display.getDisplay(this).setCurrent(mainForm);
}
 
/**
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.
*/

public void pauseApp() {
// No implementation required
}
 
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated
*/

public void destroyApp(boolean unconditional) {
// No implementation required
}
 
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
// Exit the MIDlet
notifyDestroyed();
}
}
}

Postconditions

A MIDlet is localized using application attributes.

See also

194 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.

×