From 34bd1707749c71302207fb2fc0559b6ca301d895 Mon Sep 17 00:00:00 2001 From: Gilles Date: Wed, 29 Aug 2018 03:46:52 +0200 Subject: [PATCH] MATH-1467: Avoid raising exception when the locale's language is "English". The change also avoids raising an exception when a translation is missing. --- .../math4/exception/util/LocalizedFormats.java | 16 +++++++++------- .../util/LocalizedFormats_en.properties | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 src/main/resources/assets/org/apache/commons/math4/exception/util/LocalizedFormats_en.properties 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 54b3efbb0..f504516ee 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; - } - } 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 000000000..0b14f1d52 --- /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").