|
|
|||||||||||||||
IntroductionThe global connectivity provided by the Internet will increase the demand for internationalized software products and place additional requirements on internationalized applications. For example, applications that are delivered and run over the Internet may need to adapt to the user's language at run time. Other applications will need to be multilingual and support a combination of different languages. Java was designed to support internationalizationits primitive character type is based on Unicode, an international standard for encoding written language elements. The Microsoft SDK for Java enhances the internationalization features of Java with support for local character set encoding, Microsoft® Win32® resources, locales, vertical writing, and Java-based input method editors (IMEs). Enhanced internationalization support is provided by the following Microsoft packages:
In addition to the enhanced support provided by the packages in the preceding list, the Microsoft Win32 VM for Java (Microsoft VM) includes fully functional versions of the java.lang.Runtime methods getLocalizedInputStream and getLocalizedOutputStream. Creating Multilingual Java Applications using Win32 ResourcesResources 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 to address this problem. It 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 Chinese 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 Multilingual Java Applications with Win32 ResourcesUsing 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 run time: 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 will 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 if all 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. This way, there will be no confusion between literal text strings and text that should be localized. Enhanced Locale SupportThe Microsoft VM includes the following enhancements to the Locale class to support internationalization.
Vertical Writing SupportTraditional Chinese and Japanese texts are written verticallycharacters appear in a vertical line from the top to the bottom of the page, with lines progressing from the right to the left side of the page. The Microsoft VM supports vertical writing with the FxFormattedText class. There are two ways to specify that the text for an FxFormattedText buffer be written vertically:
The following sample code implements a panel that contains vertical writing. class VerticalWritingSample extends UIPanel { FxFormattedText fxft; public VerticalWritingSample () { fxft = new FxFormattedText ("Vertical Text!"); fxft.setLocale (Locale.JAPANESE_VERTICAL); } public void paint (FxGraphics g) { fxft.paint (g); } } Note When written vertically, characters are rotated 90 degrees (clockwise). This rotation is in accordance with the conventions of vertical writing for traditional Chinese and Japanese texts. Input Method EditorsAn input method editor (IME) is a program that allows computer users to enter complex characters and symbols, such as Japanese Kanji characters, by using a standard keyboard. Internationalized versions of Microsoft® Windows® provide native system IMEs for locales with languages using non-Latin alphabets (for example, Japanese, Korean, Traditional Chinese, and Simplified Chinese). The Microsoft VM provides IME support for Java applications with an Input Method Manager (IMM) that hosts Java-based IMEs, as well as native system IMEs. Native System IMEsThe Microsoft VM supports existing native system IMEs interchangeably with Java-based IMEs. However, there are some limitations associated with using system IMEs. You can use a Java-based IME on any system, regardless of what language the system is localized for as long as the Microsoft VM is installed. With native system IMEs, however, you are limited to IMEs supported by the host system. Java-Based IMEsThe Microsoft VM for Java fully supports IMEs written in the Java language. Java-based IMEs provide more flexibility and control for applications than native system IMEs. The following is a summary of the features of Java-based IMEs.
Writing a Java-Based Input Method EditorThe com.ms.util.InputMethod package contains the following classes for creating Java-based IMEs.
Note The Microsoft SDK for Java includes a sample application, SampleIME, that illustrates how to write a simple, Java-based IME. Handling KeystrokesKeystrokes are passed from the Microsoft VM to the IMM, which routes them to the IME. To receive keystrokes, the IME must override the InputMethodListener.handledKey method. The imeCallback parameter in InputMethodListener.handledKey specifies an InputMethodCallback object that allows the IME to send messages back to the Microsoft VM. When composition is complete, the IME should use the InputMethodCallback.handleIMEChar method to return the composed Unicode character to the Microsoft VM. Note Keystrokes are represented by virtual key codes defined by the KeyEvent class in the java.awt.event package. Keyboard state codes (ALT, CTRL, SHIFT, and so on) are defined in the InputManagerListener class. Using IMEs with Java ApplicationsYou don't have to do anything special with your Java application to use native system IMEs. If a native system IME is present on a system running the Microsoft VM, it will correctly process and route IME-composed strings to the application. If you want to use a Java-based IME with your application, you'll need to use the IMM to activate the IME that you want to use. The following list shows the classes you use to work with the IMM.
You must set the current IMM before activating an IME. The following code shows how to set the default IMM and install an IME. InputManagerListener imm; MyIME ime; // Set default IMM imm = com.ms.lang.SystemX.getDefaultInputManager (); com.ms.lang.SystemX.setInputManager (imm, true); if (imm != null) { // Instantiate IME. ime = new MyIME () ; if (ime != null) { // Set IME to be current IME and activate it. imm.setInputMethod (ime); ime.activate (); } } Unicode SupportAlthough the Java language includes support for Unicode strings, many text editors use local character set encoding, and many text files contain local character set strings, not Unicode strings. This causes two different problems:
The Microsoft Compiler for Java (jvc) translates local character set encoded string literals in Java source code to Unicode when it compiles the source into class files. When the Microsoft VM runs these class files, the string literals will appear correctly. The java.lang.Runtime methods getLocalizedInputStream and getLocalizedOutputStream will convert between local character set encoding and Unicode. Note You should only use getLocalizedInputStream on input streams that are known to be encoded in a local character set. Do not use this method on input streams that contain Unicode characters. Converting Local Encoding to UnicodeThe getLocalizedInputStream method converts local character set encoding to Unicode. To use it, you must first call Runtime.getRuntime to get a Runtime object. Then, call the Runtime.getLocalizedInputStream method, passing the input stream object that you want to have converted. The getLocalizedInputStream method returns a DataInputStream object that performs local-character-set-to-Unicode conversion on the input stream. The following example demonstrates this technique. Runtime rt = Runtime.getRuntime(); FileInputStream fisLoc; // local DataInputStream disUni; // Unicode String sInput; fisLoc = new FileInputStream("LOCAL.TXT"); disUni = (DataInputStream)rt.getLocalizedInputStream(fisLoc); sInput = disUni.readLine(); Converting Unicode to Local EncodingThe getLocalizedOutputStream method converts Unicode to local character set encoding. To use it, you must first call Runtime.getRuntime to get a Runtime object. Then, call the Runtime.getLocalizedOutputStream method, passing the output stream object that you want to have converted. The getLocalizedOutputStream method returns a DataOutputStream object that performs Unicode-to-local-character-set conversion on the output stream. The following example demonstrates this technique. Runtime rt = Runtime.getRuntime(); FileOutputStream fosUni; // Unicode DataOutputStream dosLoc; // local String sOutput = "This is a Unicode string"; fosUni = new FileOutputStream("OUTPUT.TXT"); dosLoc = (DataOutputStream)rt.getLocalizedOutputStream(fosUni); dosLoc.writeChars(sOutput);
|
© 1998 Microsoft Corporation. All rights reserved. Terms of use. |