Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Translation of Java Programs under Eclipse
References:
http://java.sun.com/docs/books/tutorial/i18n/intro/quick.html
File:
TranslationExample.jar
We consider a small Java program: SimpleExample.java.
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class SimpleExample {
public static void main(String[] args) {
JOptionPane.showMessageDialog(new JFrame(), "Hello");
}
}
Execution of the program in Eclipse:
Developer - To translate the program:
Go to: Source / Externalize Strings
The key SimpleExample.0 has the value Hello (“Hello” is a string in
SimpleExample.java that is externalized).
The file SimpleExample.java is automatically changed to:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class SimpleExample {
public static void main(String[] args) {
JOptionPane.showMessageDialog(new
Messages.getString("SimpleExample.0")); //$NON-NLS-1$
}
}
JFrame(),
A file Message.java is created automatically.
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Messages {
private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(String key) {
// TODO Auto-generated method stub
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
A file Messages.properties is also generated automatically; it associates values to keys.
This file is used when no translation is available. (Use Message.propertiess and not
messages.properties)
SimpleExample.0=Hello\!
To translate the program in American English, create a file Messages_en_US.properties:
SimpleExample.0=Bonjour\!
To translate the program in French, create a file Messages_fr.properties:
SimpleExample.0=Bonjour\!
To translate the program in German, create a file Messages_de.properties:
SimpleExample.0=Guten Tag\!
Translator - To translate the program:
Translators only need to edit the Messages files.
Developer - Create a JAR file:
Go to: File/Export and select JAR file. A JAR file is created. Let us call it
TranslationExample.jar.
Content of the JAR file:
You may have to edit the MANIFEST.MF file and incorporate it again in the JAR file to
make the program run properly.
The content of MANIFEST.MF should be:
Manifest-Version: 1.0
Main-Class: SimpleExample
Class-Path: TranslationExample.jar
Note: The files of the JAR file can be edited and re-injected in the JAR file.
User - Run the JAR file:
You may also have to associate JAR files to Java.
Double-click on the JAR file to run the program (or right-click, select Open With and
Java).
The program will run in the language of your OS locale.