changeset 6565:ffd0a74b30d6

8016127: NLS: logging.properties translatability recommendation 8024131: Issues with cached localizedLevelName in java.util.logging.Level Summary: This fix updates logging.properties resource bundles to follow internationalization guidelines. It also fixes a caching issue with localizedLevelName. The regression test that was added needs both fixes to pass. Reviewed-by: mchung
author dfuchs
date Wed, 04 Sep 2013 20:50:32 +0200
parents 5a9b87abdbee
children 41c6bdfe4bb4
files src/share/classes/java/util/logging/Level.java src/share/classes/sun/util/logging/resources/logging.properties test/java/util/logging/LevelResourceBundle.java test/java/util/logging/LocalizedLevelName.java
diffstat 4 files changed, 185 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/logging/Level.java	Tue Sep 03 23:47:23 2013 +0100
+++ b/src/share/classes/java/util/logging/Level.java	Wed Sep 04 20:50:32 2013 +0200
@@ -27,6 +27,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.ResourceBundle;
 
@@ -63,7 +64,7 @@
  */
 
 public class Level implements java.io.Serializable {
-    private static String defaultBundle = "sun.util.logging.resources.logging";
+    private static final String defaultBundle = "sun.util.logging.resources.logging";
 
     /**
      * @serial  The non-localized name of the level.
@@ -81,7 +82,8 @@
     private final String resourceBundleName;
 
     // localized level name
-    private String localizedLevelName;
+    private transient String localizedLevelName;
+    private transient Locale cachedLocale;
 
     /**
      * OFF is a special level that can be used to turn off logging.
@@ -209,6 +211,7 @@
         this.value = value;
         this.resourceBundleName = resourceBundleName;
         this.localizedLevelName = resourceBundleName == null ? name : null;
+        this.cachedLocale = null;
         KnownLevel.add(this);
     }
 
@@ -250,17 +253,71 @@
         return this.name;
     }
 
-    final synchronized String getLocalizedLevelName() {
+    private String computeLocalizedLevelName(Locale newLocale) {
+        ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
+        final String localizedName = rb.getString(name);
+
+        final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
+        if (!isDefaultBundle) return localizedName;
+
+        // This is a trick to determine whether the name has been translated
+        // or not. If it has not been translated, we need to use Locale.ROOT
+        // when calling toUpperCase().
+        final Locale rbLocale = rb.getLocale();
+        final Locale locale =
+                Locale.ROOT.equals(rbLocale)
+                || name.equals(localizedName.toUpperCase(Locale.ROOT))
+                ? Locale.ROOT : rbLocale;
+
+        // ALL CAPS in a resource bundle's message indicates no translation
+        // needed per Oracle translation guideline.  To workaround this
+        // in Oracle JDK implementation, convert the localized level name
+        // to uppercase for compatibility reason.
+        return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
+    }
+
+    // Avoid looking up the localizedLevelName twice if we already
+    // have it.
+    final String getCachedLocalizedLevelName() {
+
         if (localizedLevelName != null) {
-            return localizedLevelName;
+            if (cachedLocale != null) {
+                if (cachedLocale.equals(Locale.getDefault())) {
+                    // OK: our cached value was looked up with the same
+                    //     locale. We can use it.
+                    return localizedLevelName;
+                }
+            }
         }
 
+        if (resourceBundleName == null) {
+            // No resource bundle: just use the name.
+            return name;
+        }
+
+        // We need to compute the localized name.
+        // Either because it's the first time, or because our cached
+        // value is for a different locale. Just return null.
+        return null;
+    }
+
+    final synchronized String getLocalizedLevelName() {
+
+        // See if we have a cached localized name
+        final String cachedLocalizedName = getCachedLocalizedLevelName();
+        if (cachedLocalizedName != null) {
+            return cachedLocalizedName;
+        }
+
+        // No cached localized name or cache invalid.
+        // Need to compute the localized name.
+        final Locale newLocale = Locale.getDefault();
         try {
-            ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName);
-            localizedLevelName = rb.getString(name);
+            localizedLevelName = computeLocalizedLevelName(newLocale);
         } catch (Exception ex) {
             localizedLevelName = name;
         }
+        cachedLocale = newLocale;
         return localizedLevelName;
     }
 
@@ -318,6 +375,7 @@
      *
      * @return the non-localized name of the Level, for example "INFO".
      */
+    @Override
     public final String toString() {
         return name;
     }
@@ -420,6 +478,7 @@
      * Compare two objects for value equality.
      * @return true if and only if the two objects have the same level value.
      */
+    @Override
     public boolean equals(Object ox) {
         try {
             Level lx = (Level)ox;
@@ -433,6 +492,7 @@
      * Generate a hashcode.
      * @return a hashcode based on the level value
      */
+    @Override
     public int hashCode() {
         return this.value;
     }
--- a/src/share/classes/sun/util/logging/resources/logging.properties	Tue Sep 03 23:47:23 2013 +0100
+++ b/src/share/classes/sun/util/logging/resources/logging.properties	Wed Sep 04 20:50:32 2013 +0200
@@ -27,20 +27,20 @@
 # these are the same as the non-localized level name.
 
 # The following ALL CAPS words should be translated.
-ALL=ALL
+ALL=All
 # The following ALL CAPS words should be translated.
-SEVERE=SEVERE
+SEVERE=Severe
 # The following ALL CAPS words should be translated.
-WARNING=WARNING
+WARNING=Warning
 # The following ALL CAPS words should be translated.
-INFO=INFO
+INFO=Info
 # The following ALL CAPS words should be translated.
-CONFIG= CONFIG
+CONFIG= Config
 # The following ALL CAPS words should be translated.
-FINE=FINE
+FINE=Fine
 # The following ALL CAPS words should be translated.
-FINER=FINER
+FINER=Finer
 # The following ALL CAPS words should be translated.
-FINEST=FINEST
+FINEST=Finest
 # The following ALL CAPS words should be translated.
-OFF=OFF
+OFF=Off
--- a/test/java/util/logging/LevelResourceBundle.java	Tue Sep 03 23:47:23 2013 +0100
+++ b/test/java/util/logging/LevelResourceBundle.java	Wed Sep 04 20:50:32 2013 +0200
@@ -33,15 +33,19 @@
 
 public class LevelResourceBundle {
     public static void main(String args[]) throws Exception {
-        final String name = "SEVERE";
-        String en = getLocalizedMessage(Locale.getDefault(), name);
-        String fr = getLocalizedMessage(Locale.FRANCE, name);
+        final String key = "SEVERE";
+        final String name = "Severe";
+        String en = getLocalizedMessage(Locale.getDefault(), key);
+        String fr = getLocalizedMessage(Locale.FRANCE, key);
         if (!name.equals(en)) {
              throw new RuntimeException("Expect " + name + " equals " + en);
         }
         if (name.equals(fr)) {
              throw new RuntimeException("Expect " + name + " not equals " + fr);
         }
+        if (key.equals(fr)) {
+             throw new RuntimeException("Expect " + key + " not equals " + fr);
+        }
     }
 
     private static final String RBNAME = "sun.util.logging.resources.logging";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/logging/LocalizedLevelName.java	Wed Sep 04 20:50:32 2013 +0200
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.util.*;
+import java.util.logging.*;
+
+/*
+ * @test
+ * @bug 8016127 8024131
+ * @summary test logging.properties localized
+ * @run main/othervm LocalizedLevelName
+ */
+
+public class LocalizedLevelName {
+    private static Object[] namesMap = {
+        "SEVERE",  Locale.ENGLISH, "Severe",        Level.SEVERE,
+        "WARNING", Locale.FRENCH,  "Avertissement", Level.WARNING,
+        "INFO",    Locale.ITALIAN, "Informazioni",  Level.INFO,
+        "SEVERE",  Locale.FRENCH,  "Grave",         Level.SEVERE,
+        "CONFIG",  Locale.GERMAN,  "Konfiguration", Level.CONFIG,
+        "ALL",     Locale.ROOT,    "All",           Level.ALL,
+        "SEVERE",  Locale.ROOT,    "Severe",        Level.SEVERE,
+        "WARNING", Locale.ROOT,    "Warning",       Level.WARNING,
+        "CONFIG",  Locale.ROOT,    "Config",        Level.CONFIG,
+        "INFO",    Locale.ROOT,    "Info",          Level.INFO,
+        "FINE",    Locale.ROOT,    "Fine",          Level.FINE,
+        "FINER",   Locale.ROOT,    "Finer",         Level.FINER,
+        "FINEST",  Locale.ROOT,    "Finest",        Level.FINEST
+    };
+
+    public static void main(String args[]) throws Exception {
+        Locale defaultLocale = Locale.getDefault();
+        for (int i=0; i<namesMap.length; i += 4) {
+            final String key = (String) namesMap[i];
+            final Locale locale = (Locale) namesMap[i+1];
+            final String expectedTranslation = (String) namesMap[i+2];
+            final Level level = (Level) namesMap[i+3];
+
+            final String en = getLocalizedMessage(Locale.ENGLISH, key);
+            final String other = getLocalizedMessage(locale, key);
+
+            System.out.println(locale + ": " + key + "=" + expectedTranslation
+                    + ", (Level." + level.getName() + ")");
+            System.out.println("     => localized(" + Locale.ENGLISH + ", "
+                    + key + ")=" + en);
+            System.out.println("     => localized(" + locale + ", " + key
+                    + ")=" + other);
+            if (!key.equals(en.toUpperCase(Locale.ROOT))) {
+                throw new RuntimeException("Expect " + key
+                        + " equals upperCase(" + en + ")");
+            }
+            if (!Locale.ENGLISH.equals(locale) && !Locale.ROOT.equals(locale)
+                    && key.equals(other.toUpperCase(Locale.ROOT))) {
+                throw new RuntimeException("Expect " + key
+                        + " not equals upperCase(" + other +")");
+            }
+            if ((Locale.ENGLISH.equals(locale) || Locale.ROOT.equals(locale))
+                    && !key.equals(other.toUpperCase(Locale.ROOT))) {
+                throw new RuntimeException("Expect " + key
+                        + " equals upperCase(" + other +")");
+            }
+            if (!other.equals(expectedTranslation)) {
+                throw new RuntimeException("Expected \"" + expectedTranslation
+                        + "\" for '" + locale + "' but got \"" + other + "\"");
+            }
+            Locale.setDefault(locale);
+            final String levelName = level.getLocalizedName();
+            System.out.println("Level.getLocalizedName() is: " + levelName);
+            if (!levelName.equals(other.toUpperCase(locale))) {
+                throw new RuntimeException("Expected \""
+                        + other.toUpperCase(locale) + "\" for '"
+                        + locale + "' but got \"" + levelName + "\"");
+            }
+            Locale.setDefault(defaultLocale);
+       }
+    }
+
+    private static final String RBNAME = "sun.util.logging.resources.logging";
+    private static String getLocalizedMessage(Locale locale, String key) {
+        ResourceBundle rb = ResourceBundle.getBundle(RBNAME, locale);
+        return rb.getString(key);
+    }
+}