Skip to content

Commit f61d728

Browse files
committed
Fallback Locale other than the system Locale through setDefaultLocale
Closes gh-23977
1 parent f4c847b commit f61d728

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractResourceBasedMessageSource.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.context.support;
1818

1919
import java.util.LinkedHashSet;
20+
import java.util.Locale;
2021
import java.util.Set;
2122

2223
import org.springframework.lang.Nullable;
@@ -43,6 +44,9 @@ public abstract class AbstractResourceBasedMessageSource extends AbstractMessage
4344

4445
private boolean fallbackToSystemLocale = true;
4546

47+
@Nullable
48+
private Locale defaultLocale;
49+
4650
private long cacheMillis = -1;
4751

4852

@@ -143,6 +147,7 @@ protected String getDefaultEncoding() {
143147
* {@code java.util.ResourceBundle}. However, this is often not desirable
144148
* in an application server environment, where the system Locale is not relevant
145149
* to the application at all: set this flag to "false" in such a scenario.
150+
* @see #setDefaultLocale
146151
*/
147152
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
148153
this.fallbackToSystemLocale = fallbackToSystemLocale;
@@ -152,11 +157,46 @@ public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
152157
* Return whether to fall back to the system Locale if no files for a specific
153158
* Locale have been found.
154159
* @since 4.3
160+
* @deprecated as of 5.2.2, in favor of {@link #getDefaultLocale()}
155161
*/
162+
@Deprecated
156163
protected boolean isFallbackToSystemLocale() {
157164
return this.fallbackToSystemLocale;
158165
}
159166

167+
/**
168+
* Specify a default Locale to fall back to, as an alternative to falling back
169+
* to the system Locale.
170+
* <p>Default is to fall back to the system Locale. You may override this with
171+
* a locally specified default Locale here, or enforce no fallback locale at all
172+
* through disabling {@link #setFallbackToSystemLocale "fallbackToSystemLocale"}.
173+
* @since 5.2.2
174+
* @see #setFallbackToSystemLocale
175+
* @see #getDefaultLocale()
176+
*/
177+
public void setDefaultLocale(@Nullable Locale defaultLocale) {
178+
this.defaultLocale = defaultLocale;
179+
}
180+
181+
/**
182+
* Determine a default Locale to fall back to: either a locally specified default
183+
* Locale or the system Locale, or {@code null} for no fallback locale at all.
184+
* @since 5.2.2
185+
* @see #setDefaultLocale
186+
* @see #setFallbackToSystemLocale
187+
* @see Locale#getDefault()
188+
*/
189+
@Nullable
190+
protected Locale getDefaultLocale() {
191+
if (this.defaultLocale != null) {
192+
return this.defaultLocale;
193+
}
194+
if (this.fallbackToSystemLocale) {
195+
return Locale.getDefault();
196+
}
197+
return null;
198+
}
199+
160200
/**
161201
* Set the number of seconds to cache loaded properties files.
162202
* <ul>

spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -237,6 +237,7 @@ protected PropertiesHolder getMergedProperties(Locale locale) {
237237
if (mergedHolder != null) {
238238
return mergedHolder;
239239
}
240+
240241
Properties mergedProps = newProperties();
241242
long latestTimestamp = -1;
242243
String[] basenames = StringUtils.toStringArray(getBasenameSet());
@@ -253,6 +254,7 @@ protected PropertiesHolder getMergedProperties(Locale locale) {
253254
}
254255
}
255256
}
257+
256258
mergedHolder = new PropertiesHolder(mergedProps, latestTimestamp);
257259
PropertiesHolder existing = this.cachedMergedProperties.putIfAbsent(locale, mergedHolder);
258260
if (existing != null) {
@@ -279,18 +281,26 @@ protected List<String> calculateAllFilenames(String basename, Locale locale) {
279281
return filenames;
280282
}
281283
}
284+
285+
// Filenames for given Locale
282286
List<String> filenames = new ArrayList<>(7);
283287
filenames.addAll(calculateFilenamesForLocale(basename, locale));
284-
if (isFallbackToSystemLocale() && !locale.equals(Locale.getDefault())) {
285-
List<String> fallbackFilenames = calculateFilenamesForLocale(basename, Locale.getDefault());
288+
289+
// Filenames for default Locale, if any
290+
Locale defaultLocale = getDefaultLocale();
291+
if (defaultLocale != null && !defaultLocale.equals(locale)) {
292+
List<String> fallbackFilenames = calculateFilenamesForLocale(basename, defaultLocale);
286293
for (String fallbackFilename : fallbackFilenames) {
287294
if (!filenames.contains(fallbackFilename)) {
288295
// Entry for fallback locale that isn't already in filenames list.
289296
filenames.add(fallbackFilename);
290297
}
291298
}
292299
}
300+
301+
// Filename for default bundle file
293302
filenames.add(basename);
303+
294304
if (localeMap == null) {
295305
localeMap = new ConcurrentHashMap<>();
296306
Map<Locale, List<String>> existing = this.cachedFilenames.putIfAbsent(basename, localeMap);

spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ public ResourceBundle newBundle(String baseName, Locale locale, String format, C
461461
@Override
462462
@Nullable
463463
public Locale getFallbackLocale(String baseName, Locale locale) {
464-
return (isFallbackToSystemLocale() ? super.getFallbackLocale(baseName, locale) : null);
464+
Locale defaultLocale = getDefaultLocale();
465+
return (defaultLocale != null && !defaultLocale.equals(locale) ? defaultLocale : null);
465466
}
466467

467468
@Override

0 commit comments

Comments
 (0)