February 1999

Internationalize with Localization

by Jay Cross

When you're creating an app that will be used by various groups who communicate differently, it's nice to be able to keep the back-end processing the same, and make only minimal, easily edited changes to the user interface. This is true whether you're using different national tongues, for example, English, French, Japanese, or Colloquial Tibetan, or simply different target audiences, such as qualified Network Administrators versus Cyber-Laymen loading that MSN CD-ROM for the first time. Visual J++ 6.0's Localization feature gives you some easy-to-use tools and classes to facilitate using this desirable design pattern, making your applications easily ported to other countries and cultures (presuming you have a linguist handy). In this article, the first of two, we'll look at these new tools for exporting your code across the world.

Note: Examples shown here were done using a Technology Preview version of Visual J++ 6.0, which is freely available to Microsoft SBN (guest and better) members, by navigating down from msdn.microsoft.com/visualj

Getting the idea

Applications, as depicted in Figure A, are a collection of program logic, data storage, access to services, and forms, including some constant text, image, sound, and perhaps other data. Localization allows us to have several sets of that constant part, each associated with a specific locale. Your user's system environment, or some other input, can tell your application which locale to use, and, from that, which set of constants should be used.

Figure A: Cultural and linguistic constant data can be selectively swapped based on locale.

[ Figure A ]

In Visual J++, we generally create forms using the Visual Editor, dropping in Windows controls, just as we might in Visual Basic. Every element of these forms has some properties that are localizable, including (obviously) the text property.

Using the following steps, we create a separate resource file for every form in our application and for every language in use. So, if we have seven forms and four languages, we'll have 28 resource files. If you use the required naming convention, your user's computer will select the right resource file at execution time, based on user preferences.

Building an example

The following example is extremely simplistic, but it shows the salient details. We'll see more theory a bit later. When you first start Visual J++, you should create a new project. When that's ready, click on Form1 to get into the Form Designer. For the purposes of this lab, we'll need to write a small amount of code, but most of the work is filling out property sheets. Create a form using two labels and one button (from the toolbox) so that it looks something like Figure B.

Figure B: If you don't get too fancy, your form could look like this.

[ Figure B]

I altered the font and color properties over in the Properties Window, to make the text bigger and bolder. Other than that, nothing special was done here--yet.

However, before you save your form, click on the overall form and go over to the Properties window. Find the localizable property and set it to true.

It's now time to add some code. (What? Can't I just set some property? No, sorry, code is required here.) You need to supply some action in the button1_click() method. Try this:


private void button1_click(Object source, =>Event e) {
	label1.setVisible(label2.getVisible());
	label2.setVisible(!(label2.getVisible()));
}

You may also need to add a line to your main method in the Class1.java file. If it's empty, add:


public static void main(String[] args) {
	Form1 f = new Form1();
}

Build and test your application (It doesn't do much, but it created the resource file.)

At this point, you should make two copies of your Form1.resources file. The first will be renamed to something that won't interfere, so you have a good copy some place. The second, you'll rename to Form1_enu_enu.resources. Next, go into the Form editor and change the text fields on our components so they use French approximations to our English.

Build and test the application again. When you're done, look in the project space with Windows (or NT) Explorer, and change the name of the current Form1.resources file to Form1_fra_fra.resources. Then you'll probably want to copy the old master resources file as Form1.resources. Your directory should look something like Figure C. And that's it. You can now run the program to verify that it works.

Figure C: Your directory should contain these three files--it's important to get the names exactly right.

[ Figure C ]

Running your application

Looking in your project space, you'll see one file with the .exe extension. Click on this and you should see something like Figure B. Next, go to Regional Settings from the Settings | Control Panel in Windows. Change to Standard French, as in Figure D. Run the application again and you'll see something like Figure E. That's all there's to it.

Figure D: Find the Globe and select French (Standard).

[ Figure D ]

Figure E: Our form now displays its text in French.

[ Figure E]

Using additional features

Yes, that last sentence was actually a very narrow interpretation of the truth. In fact, for this application that's all there is. If you've ever written code that needed to work in multiple countries, you know that there are many other issues. These include date and money formats, changing sound clips, and images. Also a factor is merging various automatic data into text. The order in which text must appear can vary, especially in languages that proceed right to left, such as Arabic or Hebrew, or from the top down, such as Chinese.

Visual J++ includes classes that help manage all of these, and more. We'll take a closer look at these next month, in part 2 of this article.

Conclusion

Although we haven't completely solved all the problems facing mankind, localization is one major part of the solution. Specifically, it's a pattern and tool to reduce the cost in man-hours required for making one application available and comprehensible to all the peoples of the world. We ancients of the software industry will happily tell you that this is no small feat. In Microsoft Visual J++ 6.0, you have a tool well-tuned for this job, so that a few short minutes when creating a project could save months of redesign time when your product reaches global prominence.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.