Internationalization in Composer sample

Article ID: 2178
Last updated: 28 May, 2018
Article ID: 2178
Last updated: 28 May, 2018
Revision: 3
Views: 821
Posted: 02 Nov, 2007
by Dean J.
Updated: 28 May, 2018
by Gargani A.

Question

Does Composer support internationalization?

Answer

Since version 5.0, JViews Composer includes internationalization capabilities that can serve as inspiration for your own internationalization mechanism. The Composer implementation is built upon the java.util.ResourceBundle facilities; see the JDK reference manual here for further details.

Apart from this, note that i18n is part of the Java language. So, you can use the Java i18n API in your application, and integrate it within your JViews objects to benefit from this.

For example, suppose you want to provide a multilingual JViews application, so that the labels displayed through JViews objects (for example IlvLabel) depend on the current system locale. To do this, you define the language-specific messages in a property file (one for each locale), and store them in a java.util.ResourceBundle Java object. Then, when you create your IlvLabel object, you need to get the localized message from the resource bundle, and set it as the label of your IlvLabel instance.

Here is a simple example. It expects the locale and encoding to be passed as a command line parameter (for example: 'java Test18n en US') so that testing the multilingual feature is made easy. For a real application, the default locale used is of course the system locale.

import ilog.views.*;
import ilog.views.graphic.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;  

public class Test18n extends JFrame {
  private IlvManager mgr;
  private IlvManagerView mgrview;
  private String bundleName = "TestDbm";
  private ResourceBundle res;  

  public Test18n() {
    super("JViews and internationalization");  
    res = ResourceBundle.getBundle(bundleName);   
    mgr = new IlvManager();  mgrview = new IlvManagerView(mgr);  
    Container pane = getContentPane();  
    pane.setLayout(new BorderLayout());  
    pane.add(mgrview, BorderLayout.CENTER);  
    String helloString = getLocalizedString("hello");  
    lvLabel label = new IlvLabel(new IlvPoint(50,50), helloString);  
    mgr.addObject(label, false);  setSize(800,600);
  }  

  private String getLocalizedString(String key) {
    try {   
        return res.getString(bundleName + "." + key);  
    } catch (MissingResourceException e) {   
        return key;  
    }
  }  

  public static void main(String[] args) {  
      if (args.length ==2)
         Locale.setDefault(new Locale(args[0], args[1]));   
      (new Test18n()).setVisible(true);
  }  
}

The content of the messages database files is simply the translation of the word "hello" in the corresponding language.
Each file is named TestDbm_ll_CC.properties, where TestDbm is the name passed to ResourceBundle.getBundle(), ll is the language, and CC the country. For example:

the file TestDbm_fr_FR.properties contains the string:
TestDbm.hello = Bonjour

whereas the file TestDbm_en_US.properties contains:
TestDbm.hello = Hello

Since JViews 8.1, you can use facilities of IlvResourceUtil provided by the JViews Framework to retrieve resource bundles. These facilities allow you to optimize the performance of resource loading for applets.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2178
Last updated: 28 May, 2018
Revision: 3
Views: 821
Posted: 02 Nov, 2007 by Dean J.
Updated: 28 May, 2018 by Gargani A.
Also listed in


Others in this category