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.

  1. import java.awt.event.*;
  2. import java.awt.*;
  3.  
  4. import java.util.MissingResourceException;
  5. import java.util.ResourceBundle;
  6.  
  7. public class SimpleNLS {
  8.     private static final ResourceBundle STRINGS = ResourceBundle.getBundle("simple");
  9.  
  10.     public static String getString(String key) {
  11.         try {
  12.             return STRINGS.getString(key);
  13.         }
  14.         catch (MissingResourceException e) {
  15.             return '!' + key + '!';
  16.         }
  17.     }
  18.    
  19.     public static void main(String[] args) {
  20.         ApplicationWindow appwin = new ApplicationWindow(getString("Title"));
  21.         appwin.setVisible(true);
  22.     }
  23.  
  24.     static class ApplicationWindow extends Frame {
  25.         ApplicationWindow(String name) {
  26.             super(name);
  27.  
  28.             addWindowListener(new WindowAdapter() {
  29.                 public void windowClosing(WindowEvent e) {
  30.                     System.exit(0);
  31.                 }
  32.             });
  33.  
  34.             Menu filemenu = new Menu(getString("File"), true);
  35.             MenuItem quit = new MenuItem(getString("Quit"));
  36.             quit.setShortcut(new MenuShortcut(quit.getLabel().charAt(0)));
  37.             quit.addActionListener(new ActionListener() {
  38.                 public void actionPerformed(ActionEvent e) {
  39.                     System.exit(0);
  40.                 }
  41.             });
  42.             filemenu.add(quit);
  43.  
  44.             MenuBar menubar = new MenuBar();
  45.             menubar.add(filemenu);
  46.  
  47.             setMenuBar(menubar);
  48.        
  49.             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  50.            
  51.             int width = screenSize.width/3;
  52.             int height = screenSize.height/4;
  53.             int x = (screenSize.width - width) / 2;
  54.             int y = (screenSize.height - height) / 3;  
  55.  
  56.             setBounds(width, height, x, y);
  57.             setResizable(false);
  58.         }
  59.     }
  60. }
$ 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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].