How to use i18n Internationalization

From OpenJUMP Wiki
Jump to navigation Jump to search

Introduction

There exists 2 ways for internationalizing the user interface.

  • a) using the i18n classes
  • b) using the resource bundle classes

both are described below

Internationalization with i18n

  • For internationalization an additional class I18NPlug.java is needed.
  • Further the name and the path of a language properties file, containing the translation, has to be defined.
  • To allow international plugins to work also with JUMP (not only OpenJump) a case selection should be made in the #initialize() method. Therefore use the expression:
if(I18NPlug.jumpi18n == true){
  //do something
}
else{
  //do something else
}

Since OpenJUMP nightly built Nov. 2007

The updates done in November 2007 for the I18N class supports different resource files for plug-ins.

To use it in your plug-ins you will need to do the following, assuming your plug-in is in the package com.foo.jump.bar

  • 1 - Create a language package in the resource directory of you source for your resource bundles. Using MAVEN the directory would be src/main/resources/com/foo/jump/bar/language
  • 2 - Add the jump.properties file (and other language files) into the directory created in 1
  • 3 - Use the method I18N.getText("com.foo.jump.bar","com.foo.jump.bar.MyPlugin.some-text-key") in your plugin to get the I18Nized text
  • 4 - Make sure when you build your jar file the whole directory tree under src/main/resources is copied to the jar file. With MAVEN this is done for you.

The new code uses the plug-in classloader so it should be able to find the resource bundle as long as you don't use your own classloader.


Using Resource Bundle Class

Holger suggests to use the ResourceBundle- Class (Java Standard). This works with all versions of JUMP. Here the is the Hello World Example:

package example;

import com.vividsolutions.jump.workbench.plugin.*;
import java.util.*;

public class MyInternational
    extends AbstractPlugIn
{
  // this is the path to the propertie- files
  //    myinternational.properties       = language not suported
  //    myinternational_de.properties    = german
  //    myinternational_en.properties    = english
  //    etc.
   
  String basename = "example.resources.myinternational";
  ResourceBundle res;

  public MyInternational()
  {
  }

  public void initialize(PlugInContext context) throws Exception
  {
    
    res = ResourceBundle.getBundle(basename);
    
    // example 
    System.out.println(getString("Text1"));

    context.getFeatureInstaller().addMainMenuItem(this,
                                                  new String[]
                                                  {getString("Tools"),
                                                  getString("MyPlugins")},
                                                  getName(), false, null, null);
  }

  public boolean execute(PlugInContext context) throws Exception
  {
    context.getWorkbenchFrame().getOutputFrame().createNewDocument();
    context.getWorkbenchFrame().getOutputFrame().addText(getString("Hallo"));
    context.getWorkbenchFrame().getOutputFrame().surface();
    return true;
  }

  public String getName()
  {
    return getString("Name");
  }

  private String getString(String s)
  {
    try
    {
      return res.getString(s);
    }
    catch (MissingResourceException ex)
    { // no entry 
      ex.printStackTrace();
      return "";
    }
    catch (Exception ex)
    { // no resource file
      ex.printStackTrace();
      return "";
    }

  }
}