Resources are used to isolate text and user interface elements (such as menus and dialog boxes) from the executable part of an application. Although the advantages of using resources apply to all applications, resources are particularly useful in applications requiring internationalization. Resource files can be localized and tested without having to rebuild an entire application. Using resources also decreases size and shortens load time for .class files, as they are not implemented with large amounts of static data.
The Microsoft SDK for Java supports a compact resource format. The com.ms.ui.resource.ResourceDecoder class is an extensible class designed to load resources from a binary file. The com.ms.ui.resource.Win32ResourceDecoder class reads resources in the Win32 resource format. This class also provides methods for accessing the individual resource elements within a resource file. This enables you to use existing resource files with Java applications and to use familiar tools and processes to create and localize new resources.
One problem with current development tools is that they are not necessarily capable of modifying resources in multiple languages within the same file. For example, a common problem is merging Far Eastern language resources with Western ones due to operating system limitations. The Microsoft SDK for Java includes the resmerge utility that enables you to develop resources separately, and then combine resources for different languages into one .res file. When you load Win32 resources from these combined resource files, you can specify the combined resource file as a source, and the language of the resource (the LCID) that you want to load.
There are four steps used in creating a Java application that uses localized resources:
For example, if you wanted to be able to load both a Japanese and an English string resource, you would first develop these two resources, and then combine them using resmerge.
C:\..\>resmerge -a resources.res MyProjResUS.res MyProjResJP.res
Using Win32 resources in an application is easy. You link to the file and extract the resource using the Win32ResourceDecoder. For example, to populate a panel with localizable controls, you create a resource file that contains dialog resources with the controls laid out as you want them. Execute the following code at runtime:
public void populate(UIPanel p, int id) { Win32ResourceDecoder res=new Win32ResourceDecoder("resources.res"); try { res.populateDialog(p, id); } catch (Exception e) { System.out.println("Problem "+e); } }
Note that a locale identifier (LCID) is not used because you typically load the resources in the language the user needs. However, you can specify a language for any resource by adding the LCID to the method call. For example, an English/Greek dictionary would specify that the list of translated words be loaded in those languages, even if the application is currently being used by someone in Germany and the rest of the user interface is in German.
Note Using Win32 resources allows you to localize your Java applications without giving your Java source code to localization translators. As a result, there will be no confusion between literal text strings and text that should be localized.