From d77409e168c13680e2fc00e52352fd0cac413bcf Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 30 Nov 2023 08:48:41 -0400 Subject: [PATCH] When getting locale message, default locale to LocaleContextHolder when locale is null (#7516) --- .../main/java/org/fao/geonet/languages/LocaleMessages.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/fao/geonet/languages/LocaleMessages.java b/core/src/main/java/org/fao/geonet/languages/LocaleMessages.java index 56260990034..69c31cc4919 100644 --- a/core/src/main/java/org/fao/geonet/languages/LocaleMessages.java +++ b/core/src/main/java/org/fao/geonet/languages/LocaleMessages.java @@ -33,6 +33,7 @@ import org.fao.geonet.utils.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils; +import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.ResourceBundleMessageSource; import com.google.common.collect.BiMap; @@ -57,16 +58,16 @@ public static String getMessageForLocale(String messageKey, Locale locale, Strin * * @param messageKey message key to use when retrieving the value from the properties file. * @param args Argument that may be supplied to the messagekey string - * @param locale locale to use when getting the message key + * @param locale locale to use when getting the message key. If null then it will default to locale context holder. * @param resourceBundleBeanQualifier resource bundle qualifier to use when getting ResourceBundleMessageSource bean * @return message */ public static String getMessageForLocale(String messageKey, Object[] args, Locale locale, String resourceBundleBeanQualifier) { - if (!StringUtils.isEmpty(messageKey) && locale !=null) { + if (!StringUtils.isEmpty(messageKey)) { ResourceBundleMessageSource resourceBundleMessageSource = getResourceBundleMessageSource(resourceBundleBeanQualifier); if (resourceBundleMessageSource != null) { - return resourceBundleMessageSource.getMessage(messageKey, args, locale); + return resourceBundleMessageSource.getMessage(messageKey, args, locale == null ? LocaleContextHolder.getLocale() : locale); } } // If we could not find the ResourceBundleMessageSource or the messageKey was in an invalid format then lets return the original key as the message.