Skip to main content

Getting started with Mobile internationalization API. Part 2

Posted by tbrandalik on February 16, 2007 at 3:46 AM PST

I'm back to finish this tutorial which started here. So let's have a look at internal structure of a resource file and explain how to work with resources in JSR238.

In part1 we talked about directory structure of resource files in the application, but I haven't shown any code for accessing them. Here is it. Main entry point into resource files is ResourceManager class. There's not much to screw up ;-)

	// look for phrases resource file under default locale
rm = ResourceManager.getManager(“phrases“);

// look for phrases resource file under en-US locale
rmUS = ResourceManager.getManager(“phrases“, “en-US“);

// get device resources
rmDev = ResourceManager.getManager(““);

Before we dive into resource file structure I have to mention also metafiles accompanying resource files. Metafiles are fairly simple. They contain only plain text, enumerating supported locales for given resource file. Next example is a metafile of phrases resource file from i18nDemo bundled with WTK2.5.

"" en cs-CZ sk-SK he-IL zh-CN ja-JP de-DE it-IT es-ES
Main purpose of the metafile is to speed up look up of supported locales for a resource file.
If you call

String[] supLocales = ResourceManager.getSupportedLocales("phrases");
the metafile is read first without need to scan thru a /global subdirectories.

However metafile is easily editable plain text file, it's not recommended to update it directly. Better use i18n Resource Manager which provides necessary integrity of metafiles and resource files. If metafile is a plain text file, resource file on the other hand is binary file designed to be efficient in storing and accessing resources. In general there are 2 types of resources in resource file:

  • text strings (UTF-8 encoded)
  • binary data (images, sounds etc. stored as a raw data)

Handling text string resources is very simple. Decoding of text strings from UTF-8 is provided by the JSR238 implementation of ResourceManager. Each string and each binary resource is identified by a number (integer in range 0 .. 0x7fffffff)

	ResourceManager rm = ResourceManager.getManager(“phrases“);
// get string id = 1234
String s = rm.getString(1234);

On the other hand binary resources are application dependent which means that developer is responsible for further processing of raw data retrieved from resource file. He has to turn raw data of for example png encoded file into an image.

	ResourceManager rm = ResourceManager.getManager(“common“);
// get image as binary data id = 5678
byte[] idata = rm.getData(5678);
Image img = Image.createImage(idata, 0, idata.length);

Evenif it's not necessary for application developer to know the format of the resource file, it's interesting what's behind the scene and of course you can write your own tool for resource file creation. Following picture shows that resource file contains header informations and resource data items. Header informations are necessary for fast access, resource file doesn't have to be read sequentially to find particular resource but the index determines a position in the file. Here is what a structure of resource file looks like:

    4 bytes for signature
    4 bytes for header length
    64-bit header entry
    64-bit header entry
    64-bit header entry
    ...
    64-bit header entry
    resource
    resource
    ...
    resource

Each header entry is composed of a resource ID, Type and Offset:

  bits 63...32   resource ID
  bits 31...0    type and offset

where values for the type are:

  • 0x01 = string
  • 0x10 = binary data
  • 0x00 = end of resources

That's all for today. Next time formatting and sorting ...