5
SimpleNLS
Create a file called simple_en_US.properties for the English version:
Title=English File=File Quit=Quit
Create a file called simple_fr_FR.properties for the French version:
Title=Français File=Fichier Quit=Quitter
IMPORTANT: Save the files in ISO-8859-1.
- import java.awt.event.*;
- import java.awt.*;
- import java.util.MissingResourceException;
- import java.util.ResourceBundle;
- public class SimpleNLS {
- private static final ResourceBundle STRINGS = ResourceBundle.getBundle("simple");
- public static String getString(String key) {
- try {
- return STRINGS.getString(key);
- }
- catch (MissingResourceException e) {
- return '!' + key + '!';
- }
- }
- public static void main(String[] args) {
- ApplicationWindow appwin = new ApplicationWindow(getString("Title"));
- appwin.setVisible(true);
- }
- static class ApplicationWindow extends Frame {
- ApplicationWindow(String name) {
- super(name);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- Menu filemenu = new Menu(getString("File"), true);
- MenuItem quit = new MenuItem(getString("Quit"));
- quit.setShortcut(new MenuShortcut(quit.getLabel().charAt(0)));
- quit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- filemenu.add(quit);
- MenuBar menubar = new MenuBar();
- menubar.add(filemenu);
- setMenuBar(menubar);
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int width = screenSize.width/3;
- int height = screenSize.height/4;
- int x = (screenSize.width - width) / 2;
- int y = (screenSize.height - height) / 3;
- setBounds(width, height, x, y);
- setResizable(false);
- }
- }
- }
$ javac SimpleNLS.java
$ java SimpleNLS
The run the program in another language, change the system properties user.language
and user.country
from the command-line:
$ java -Duser.language=fr -Duser.country=FR SimpleNLS
Comments