advertisement

Search Tips
Articles  |   Tutorials  |   Reviews  |   Dev Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source Code  |  

Browse DevX
DevX Updates - Sign Up Here


Partners & Affiliates












advertisement

Articles : Advanced Topics in Java :
Internationalization :

Contents
Introduction
Naming Resource Bundles
Creating Resource Bundles by Direct Subclassing
Using ListResourceBundle
Using PropertyResourceBundle
Compiling Resource Bundles

Using PropertyResourceBundle

A PropertyResourceBundle is definitely the easiest bundle to implement. If your application contains String objects that need to be translated into different languages, you should store these String objects in a ResourceBundle that is backed up by a set of properties files. Since the properties files are simple text files, they can be created and maintained by the translators.

A typical properties file would look something like:

key1=value1
key2=value2
key3=value3
key4=value4

Windows programmers can draw an analogy between an .ini file and a properties file (quite crudely actually ). Think of each locale having its own ini file that contains all the strings in that locale's encoding.

You can create additional properties files containing the translated values as needed, to support different locales. No changes to your source code are required, because your program references the keys, not the values.

The running Java program can identify the resource bundle and corresponding properties file directly as before using getBundle only if you use the ".properties" extension to the files.

There is another way to force loading of a particular property file using the constructor for the PropertyResourceBundle class:
PropertyResourceBundle(InputStream) and the getResourceAsStream(String) method in the java.lang.Class class.

A typical usage would look something like:

PropertyResourceBundle labels = new PropertyResourceBundle(
   new ProgramName().getClass().getResourceAsStream("MyProgramResource.txt"));

A word of caution with the PropertyResourceBundle!
How do you save your properties file that contains non-ASCII characters of say Asian languages?
Well you simply can't!
The Java 1.1 java.util.propertyResourcebundle class can read only ISO 8859_1 characters. i.e. : 8 bit characters sometimes called the ANSI code. Some sources may seem to imply that if you use Unicode escape codes to represent Unicode characters, or run native2ascii on the properties file to translate it into Unicode, everything will be fine. The fact actually remains that Property files are read byte by byte, with no character code conversion and if the characters in the file are specified in the \uXXXX format then it will only work as long as the higher order bytes are zeroes i.e. ISO 8859_1 .

This makes PropertyResourceBundle unsuitable for translated messages. The flaw will eventually be remedied perhaps when the Properties class is made locale aware and can handle non-Western European languages.

NEXT


Sameer Tyagi is a Software Engineer with several years of programming experience in iNet application development and has conducted multiple training workshops in Java. Besides holding an Engineering degree in Electronics he is a Sun Certified Java 1.1 Programmer.
Email: sameertyagi@usa.net

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

internet.com logo


JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.
Blackmail Applet
This applet prints out text that looks like letters cut out of a newspaper. You can specify the text, the width and height of the applet and the delay after each letter. Future versions will have more parameters.
JB User Poll
What is your primary tool for learning Java?
The JavaBoutique Top 15:
1. articles.rdf
2. applets.rdf
3. anfyjava
4. quickserv
5. javamp3
6. Viewer
7. ZTBanner
8. RushHour
9. Autobahner
10. Lovemeter
11. PulseText
12. PingPong
13. AScroll2
14.
15. Pool
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Google IM Not Talking to Other Jabbers
Newly-launched Google Talk IM service doesn't connect to other open source Jabber servers and users -- at least not yet.

Printer Sets Good Example for Small Business Security
While surveys suggest that small businesses aren't prepared for ever-present security dangers, this Boston-area printer has maintained a pristine operation since a scare six years ago.

VoIP Gizmo Comes to Universities
As student gear up for back to school, a SIP-based initiative aims to VoIP-enable universities around the world for free calling.



Copyright 2002 INT Media Group, Incorporated. All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/