I'm checking this in.

This changes getopt to use resource bundles for its messages.

I used Eclipse to externalize these things.  This was amazingly
easy... nice tool.

Tom

2006-05-15  Tom Tromey  <[EMAIL PROTECTED]>

        * tools/gnu/classpath/tools/getopt/Option.java (getDescription):
        Removed old comment.
        * tools/gnu/classpath/tools/getopt/ClasspathToolParser.java:
        Externalized strings.
        (getVersionString): Use MessageFormat.
        * tools/gnu/classpath/tools/getopt/Messages.java: New file.
        * resource/gnu/classpath/tools/getopt/Messages.properties: New file.
        * tools/gnu/classpath/tools/getopt/Parser.java: Externalized strings.
        (getArgument): Use a MessageFormat.
        (handleLongOption): Likewise.
        (parse): Likewise.

Index: tools/gnu/classpath/tools/getopt/ClasspathToolParser.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/ClasspathToolParser.java,v
retrieving revision 1.1
diff -u -r1.1 ClasspathToolParser.java
--- tools/gnu/classpath/tools/getopt/ClasspathToolParser.java   8 May 2006 
18:38:21 -0000       1.1
+++ tools/gnu/classpath/tools/getopt/ClasspathToolParser.java   15 May 2006 
15:46:20 -0000
@@ -38,6 +38,8 @@
 
 package gnu.classpath.tools.getopt;
 
+import java.text.MessageFormat;
+
 import gnu.classpath.Configuration;
 
 /**
@@ -50,13 +52,13 @@
 {
   private static String getVersionString(String programName)
   {
-    return (programName + " (GNU Classpath) "
-            + Configuration.CLASSPATH_VERSION
-            + "\n\n"
-            + "Copyright 2006 Free Software Foundation, Inc.\n"
-            + "This is free software; see the source for copying conditions.  
There is NO\n"
-            + "warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE.");
-
+    String fmt = (Messages.getString("ClasspathToolParser.VersionFormat")); 
//$NON-NLS-1$
+    return MessageFormat.format(fmt, 
+                                new Object[]
+                                  {
+                                    programName,
+                                    Configuration.CLASSPATH_VERSION
+                                  });
   }
 
   public ClasspathToolParser(String programName)
Index: tools/gnu/classpath/tools/getopt/Option.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/Option.java,v
retrieving revision 1.1
diff -u -r1.1 Option.java
--- tools/gnu/classpath/tools/getopt/Option.java        8 May 2006 18:38:21 
-0000       1.1
+++ tools/gnu/classpath/tools/getopt/Option.java        15 May 2006 15:46:20 
-0000
@@ -184,7 +184,6 @@
    */
   public String getDescription()
   {
-    // FIXME: localization.
     return description;
   }
 
Index: tools/gnu/classpath/tools/getopt/Parser.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/Parser.java,v
retrieving revision 1.6
diff -u -r1.6 Parser.java
--- tools/gnu/classpath/tools/getopt/Parser.java        15 May 2006 01:57:29 
-0000      1.6
+++ tools/gnu/classpath/tools/getopt/Parser.java        15 May 2006 15:46:20 
-0000
@@ -40,6 +40,7 @@
 
 import java.io.PrintStream;
 import java.text.BreakIterator;
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.Locale;
@@ -127,7 +128,7 @@
   protected static void formatText(PrintStream out, String text, Locale 
aLocale)
   {
     BreakIterator bit = BreakIterator.getLineInstance(aLocale);
-    String[] lines = text.split("\n");
+    String[] lines = text.split("\n"); //$NON-NLS-1$
     for (int i = 0; i < lines.length; i++)
       {
         text = lines[i];
@@ -165,8 +166,8 @@
     this.longOnly = longOnly;
 
     // Put standard options in their own section near the end.
-    OptionGroup finalGroup = new OptionGroup("Standard options");
-    finalGroup.add(new Option("help", "print this help, then exit")
+    OptionGroup finalGroup = new 
OptionGroup(Messages.getString("Parser.StdOptions")); //$NON-NLS-1$
+    finalGroup.add(new Option("help", Messages.getString("Parser.PrintHelp")) 
//$NON-NLS-1$ //$NON-NLS-2$
     {
       public void parsed(String argument) throws OptionException
       {
@@ -174,7 +175,7 @@
         System.exit(0);
       }
     });
-    finalGroup.add(new Option("version", "print version number, then exit")
+    finalGroup.add(new Option("version", 
Messages.getString("Parser.PrintVersion")) //$NON-NLS-1$ //$NON-NLS-2$
     {
       public void parsed(String argument) throws OptionException
       {
@@ -182,7 +183,7 @@
         System.exit(0);
       }
     });
-    finalGroup.add(new Option('J', "pass argument to the Java runtime", 
"OPTION")
+    finalGroup.add(new Option('J', Messages.getString("Parser.JArgument"), 
Messages.getString("Parser.JName")) //$NON-NLS-1$ //$NON-NLS-2$
     {
       public void parsed(String argument) throws OptionException
       {
@@ -299,7 +300,12 @@
   {
     ++currentIndex;
     if (currentIndex >= args.length)
-      throw new OptionException("option '" + request + "' requires an 
argument");
+      {
+        String message
+          = MessageFormat.format(Messages.getString("Parser.ArgReqd"), 
//$NON-NLS-1$
+                                 new Object[] { request });
+        throw new OptionException(request);
+      }
     return args[currentIndex];
   }
 
@@ -321,7 +327,11 @@
           }
       }
     if (found == null)
-      throw new OptionException("unrecognized option '" + real + "'");
+      {
+        String msg = 
MessageFormat.format(Messages.getString("Parser.Unrecognized"), //$NON-NLS-1$
+                                          new Object[] { real });
+        throw new OptionException(msg);
+      }
     String argument = null;
     if (found.getTakesArgument())
       {
@@ -332,8 +342,10 @@
       }
     else if (eq != - 1)
       {
-        throw new OptionException("option '" + real.substring(0, eq + index)
-                                  + "' doesn't allow an argument");
+        String msg
+          = MessageFormat.format(Messages.getString("Parser.NoArg"), 
//$NON-NLS-1$
+                                 new Object[] { real.substring(0, eq + index) 
});
+        throw new OptionException(msg);
       }
     found.parsed(argument);
   }
@@ -351,10 +363,14 @@
           }
       }
     if (found == null)
-      throw new OptionException("unrecognized option '-" + option + "'");
+      {
+        String msg = 
MessageFormat.format(Messages.getString("Parser.UnrecDash"), //$NON-NLS-1$
+                                          new Object[] { "" + option }); 
//$NON-NLS-1$
+        throw new OptionException(msg);
+      }
     String argument = null;
     if (found.getTakesArgument())
-      argument = getArgument("-" + option);
+      argument = getArgument("-" + option); //$NON-NLS-1$
     found.parsed(argument);
   }
 
@@ -383,12 +399,12 @@
           {
             if (args[currentIndex].length() == 0
                 || args[currentIndex].charAt(0) != '-'
-                || "-".equals(args[currentIndex]))
+                || "-".equals(args[currentIndex])) //$NON-NLS-1$
               {
                 files.notifyFile(args[currentIndex]);
                 continue;
               }
-            if ("--".equals(args[currentIndex]))
+            if ("--".equals(args[currentIndex])) //$NON-NLS-1$
               break;
             if (args[currentIndex].charAt(1) == '-')
               handleLongOption(args[currentIndex], 2);
@@ -405,10 +421,14 @@
       }
     catch (OptionException err)
       {
-        System.err.println(programName + ": " + err.getMessage());
-        System.err.println(programName + ": Try '" + programName
-                           + " " + (longOnly ? "-help" : "--help") 
-                           + "' for more information.");
+        System.err.println(programName + ": " + err.getMessage()); 
//$NON-NLS-1$
+        String fmt;
+        if (longOnly)
+          fmt = Messages.getString("Parser.TryHelpShort"); //$NON-NLS-1$
+        else
+          fmt = Messages.getString("Parser.TryHelpLong"); //$NON-NLS-1$
+        String msg = MessageFormat.format(fmt, new Object[] { programName });
+        System.err.println(programName + ": " + msg); //$NON-NLS-1$
         System.exit(1);
       }
   }
Index: resource/gnu/classpath/tools/getopt/Messages.properties
===================================================================
RCS file: resource/gnu/classpath/tools/getopt/Messages.properties
diff -N resource/gnu/classpath/tools/getopt/Messages.properties
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ resource/gnu/classpath/tools/getopt/Messages.properties     1 Jan 1970 
00:00:00 -0000
@@ -0,0 +1,49 @@
+# MessagesBundle.properties -- English language messages
+# Copyright (C) 2006  Free Software Foundation, Inc.
+#
+# This file is part of GNU Classpath.
+#
+# GNU Classpath is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# GNU Classpath is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Classpath; see the file COPYING.  If not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA.
+#
+# Linking this library statically or dynamically with other modules is
+# making a combined work based on this library.  Thus, the terms and
+# conditions of the GNU General Public License cover the whole
+# combination.
+#
+# As a special exception, the copyright holders of this library give you
+# permission to link this library with independent modules to produce an
+# executable, regardless of the license terms of these independent
+# modules, and to copy and distribute the resulting executable under
+# terms of your choice, provided that you also meet, for each linked
+# independent module, the terms and conditions of the license of that
+# module.  An independent module is a module which is not derived from
+# or based on this library.  If you modify this library, you may extend
+# this exception to your version of the library, but you are not
+# obligated to do so.  If you do not wish to do so, delete this
+# exception statement from your version.
+
+Parser.StdOptions=Standard options
+Parser.PrintHelp=print this help, then exit
+Parser.PrintVersion=print version number, then exit
+Parser.JArgument=pass argument to the Java runtime
+Parser.JName=OPTION
+Parser.ArgReqd=option ''{1}'' requires an argument
+Parser.Unrecognized=unrecognized option ''{1}''
+Parser.NoArg=option ''{1}'' doesn''t allow an argument
+Parser.UnrecDash=unrecognized option ''-{1}''
+Parser.TryHelpShort=Try ''{1} -help'' for more information
+Parser.TryHelpLong=Try ''{1} --help'' for more information
+ClasspathToolParser.VersionFormat={1} (GNU Classpath) {2}\n\nCopyright 2006 
Free Software Foundation, Inc.\nThis is free software; see the source for 
copying conditions.  There is NO\nwarranty; not even for MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.
Index: tools/gnu/classpath/tools/getopt/Messages.java
===================================================================
RCS file: tools/gnu/classpath/tools/getopt/Messages.java
diff -N tools/gnu/classpath/tools/getopt/Messages.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tools/gnu/classpath/tools/getopt/Messages.java      1 Jan 1970 00:00:00 
-0000
@@ -0,0 +1,67 @@
+/* Messages.java -- i18n support for getopt
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+
+package gnu.classpath.tools.getopt;
+
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+public class Messages
+{
+  private static final String BUNDLE_NAME
+    = "gnu.classpath.tools.getopt.Messages"; //$NON-NLS-1$
+
+  private static final ResourceBundle RESOURCE_BUNDLE
+    = ResourceBundle.getBundle(BUNDLE_NAME);
+
+  private Messages()
+  {
+  }
+
+  public static String getString(String key)
+  {
+    try
+      {
+        return RESOURCE_BUNDLE.getString(key);
+      }
+    catch (MissingResourceException e)
+      {
+        return '!' + key + '!';
+      }
+  }
+}

Reply via email to