Repository: commons-math
Updated Branches:
  refs/heads/master efb023006 -> 34bd17077


MATH-1467: Avoid raising exception when the locale's language is "English".

The change also avoids raising an exception when a translation is missing.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/34bd1707
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/34bd1707
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/34bd1707

Branch: refs/heads/master
Commit: 34bd1707749c71302207fb2fc0559b6ca301d895
Parents: efb0230
Author: Gilles <er...@apache.org>
Authored: Wed Aug 29 03:46:52 2018 +0200
Committer: Gilles <er...@apache.org>
Committed: Wed Aug 29 03:46:52 2018 +0200

----------------------------------------------------------------------
 .../math4/exception/util/LocalizedFormats.java    | 16 +++++++++-------
 .../exception/util/LocalizedFormats_en.properties | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/34bd1707/src/main/java/org/apache/commons/math4/exception/util/LocalizedFormats.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/math4/exception/util/LocalizedFormats.java 
b/src/main/java/org/apache/commons/math4/exception/util/LocalizedFormats.java
index 54b3efb..f504516 100644
--- 
a/src/main/java/org/apache/commons/math4/exception/util/LocalizedFormats.java
+++ 
b/src/main/java/org/apache/commons/math4/exception/util/LocalizedFormats.java
@@ -395,13 +395,17 @@ public enum LocalizedFormats implements Localizable {
     public String getLocalizedString(final Locale locale) {
         try {
             final String path = 
LocalizedFormats.class.getName().replaceAll("\\.", "/");
-            ResourceBundle bundle =
-                    ResourceBundle.getBundle("assets/" + path, locale);
+            final ResourceBundle bundle = ResourceBundle.getBundle("assets/" + 
path, locale);
             if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) 
{
-                // the value of the resource is the translated format
-                return bundle.getString(toString());
+                final String key = toString();
+                if (bundle.containsKey(key)) {
+                    // the value of the resource is the translated format
+                    return bundle.getString(key);
+                } else {
+                    // Use default.
+                    return sourceFormat;
+                }
             }
-
         } catch (MissingResourceException mre) { // NOPMD
             // do nothing here
         }
@@ -409,7 +413,5 @@ public enum LocalizedFormats implements Localizable {
         // either the locale is not supported or the resource is unknown
         // don't translate and fall back to using the source format
         return sourceFormat;
-
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/34bd1707/src/main/resources/assets/org/apache/commons/math4/exception/util/LocalizedFormats_en.properties
----------------------------------------------------------------------
diff --git 
a/src/main/resources/assets/org/apache/commons/math4/exception/util/LocalizedFormats_en.properties
 
b/src/main/resources/assets/org/apache/commons/math4/exception/util/LocalizedFormats_en.properties
new file mode 100644
index 0000000..0b14f1d
--- /dev/null
+++ 
b/src/main/resources/assets/org/apache/commons/math4/exception/util/LocalizedFormats_en.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+# Empty file: Its purpose is to avoid raising a "MissingResourceException"
+# whenever the "Locale" language is "English" as the default messages are
+# in English (see "LocalizedFormats" in package "o.a.c.m.exception.util").

Reply via email to