OpenJUMP
How to use i18n Internationalization
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
a) Internationalization with i18n
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
- 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
- Add the jump.properties file (and other language files) into the directory created in 1
- 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
- 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.
b) 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 "";
}
}
}
Showing changes from revision #1 to #2:
Added | Removed
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
a) Internationalization with i18n
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
- 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
- Add the jump.properties file (and other language files) into the directory created in 1
- Use the method
I18N.get(“com.foo.jump.bar”, “com.foo.jump.bar.MyPlugin.some-text-key”)
I18N.getText(“com.foo.jump.bar”,”com.foo.jump.bar.MyPlugin.some-text-key”)
in your plugin to get the I18Nized text (I believe you need to use the I18N.getText method, not the I18N.get method. [The Sunburned Surveyor – 2007-11-06])text
- 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.
b) 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 "";
}
}
}
Revised on November 6, 2007 23:28
by
sstein
(84.75.118.53)