1 // Example for use of GNU gettext. 2 // This file is in the public domain. 3 // 4 // Source code of the Java/AWT program. 5 6 import java.util.*; 7 import java.io.*; 8 import java.text.*; 9 import java.awt.*; 10 import java.awt.event.*; 11 import gnu.gettext.*; 12 13 public class Hello { main(String[] args)14 public static void main (String[] args) { 15 ResourceBundle catalog = ResourceBundle.getBundle("hello-java-awt"); 16 Frame frame = new Frame("Hello example"); 17 frame.addWindowListener( 18 new WindowAdapter() { 19 public void windowClosing (WindowEvent event) { 20 System.exit(0); 21 } 22 }); 23 Label label1 = new Label(GettextResource.gettext(catalog,"Hello, world!")); 24 Label label2 = 25 new Label( 26 MessageFormat.format( 27 GettextResource.gettext(catalog, 28 "This program is running as process number {0}."), 29 new Object[] { getPid() })); 30 Button button = new Button("OK"); 31 button.addActionListener( 32 new ActionListener() { 33 public void actionPerformed (ActionEvent event) { 34 System.exit(0); 35 } 36 }); 37 Container labels = new Container(); 38 labels.setLayout(new GridLayout(2, 1)); 39 labels.add(label1); 40 labels.add(label2); 41 Container buttons = new Container(); 42 buttons.setLayout(new FlowLayout(FlowLayout.RIGHT)); 43 buttons.add(button); 44 frame.setLayout(new BorderLayout()); 45 frame.add(labels, BorderLayout.CENTER); 46 frame.add(buttons, BorderLayout.SOUTH); 47 frame.pack(); 48 frame.setVisible(true); 49 } 50 51 /* Return the process ID of the current process. */ getPid()52 private static String getPid () { 53 try { 54 String[] args = new String[] { "/bin/sh", "-c", "echo $PPID" }; 55 Process p = Runtime.getRuntime().exec(args); 56 InputStream p_out = p.getInputStream(); 57 String s = (new BufferedReader(new InputStreamReader(p_out))).readLine(); 58 p.destroy(); 59 if (s != null) 60 return s; 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } 64 return "???"; 65 } 66 } 67