From b760196a723b240742daee5d8f0250b8e587028d Mon Sep 17 00:00:00 2001 From: Radek Felcman Date: Wed, 12 Jun 2024 10:56:57 +0200 Subject: [PATCH] LoggingLocalization performance improvement Signed-off-by: Radek Felcman --- .../junit/localization/LocalizationTest.java | 5 ++++- .../localization/EclipseLinkLocalization.java | 20 +++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/foundation/eclipselink.core.test/src/test/java/org/eclipse/persistence/testing/tests/junit/localization/LocalizationTest.java b/foundation/eclipselink.core.test/src/test/java/org/eclipse/persistence/testing/tests/junit/localization/LocalizationTest.java index 7d9a4e61a9d..a3041d0dcb6 100644 --- a/foundation/eclipselink.core.test/src/test/java/org/eclipse/persistence/testing/tests/junit/localization/LocalizationTest.java +++ b/foundation/eclipselink.core.test/src/test/java/org/eclipse/persistence/testing/tests/junit/localization/LocalizationTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020 IBM Corporation. All rights reserved. * * This program and the accompanying materials are made available under the @@ -17,6 +17,7 @@ import org.junit.Assert; import org.junit.Test; +import org.eclipse.persistence.internal.localization.EclipseLinkLocalization; import org.eclipse.persistence.internal.localization.LoggingLocalization; public class LocalizationTest { @@ -27,5 +28,7 @@ public void test() { "EclipseLink, version: EXAMPLE", LoggingLocalization.buildMessage("topLink_version", new Object[] { "EXAMPLE" })); Assert.assertEquals("LoggingLocalization.buildMessage could not find the correct translation.", "message_not_exist (There is no English translation for this message.)", LoggingLocalization.buildMessage("message_not_exist")); + Assert.assertEquals("EclipseLinkLocalization.buildMessage could not find the correct translation.", + "somekey1 (There is no English translation for this message.)", EclipseLinkLocalization.buildMessage("AAAAUnknownClass", "somekey1", null, true)); } } diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/localization/EclipseLinkLocalization.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/localization/EclipseLinkLocalization.java index 5d1a7b5f570..468cc89f80c 100644 --- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/localization/EclipseLinkLocalization.java +++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/localization/EclipseLinkLocalization.java @@ -29,6 +29,9 @@ */ public abstract class EclipseLinkLocalization { + // Get the current language's NoTranslationForThisLocale message. + private static final String NO_TRANSLATION_MESSAGE = ResourceBundle.getBundle("org.eclipse.persistence.internal.localization.i18n.EclipseLinkLocalizationResource", Locale.getDefault()).getString("NoTranslationForThisLocale"); + /** * Return the message for the given exception class and error number. */ @@ -60,13 +63,18 @@ public static String buildMessage(String localizationClassName, String key, Obje } catch (java.util.MissingResourceException mre) { if (translate) { // Found bundle, but couldn't find translation. - // Get the current language's NoTranslationForThisLocale message. - bundle = ResourceBundle.getBundle("org.eclipse.persistence.internal.localization.i18n.EclipseLinkLocalizationResource", Locale.getDefault()); - String noTranslationMessage = bundle.getString("NoTranslationForThisLocale"); - return MessageFormat.format(message, arguments) + noTranslationMessage; - } + // Use the current language's NoTranslationForThisLocale message. + if (arguments == null) { + return message + NO_TRANSLATION_MESSAGE; + } else { + return MessageFormat.format(message, arguments) + NO_TRANSLATION_MESSAGE; + } } + } + if (arguments == null) { + return message; + } else { + return MessageFormat.format(message, arguments); } - return MessageFormat.format(message, arguments); } }