Skip to content

Commit a397afe

Browse files
authored
fix: i18n default translation from thread (#19136)
Fixes getting translation bundle when executing from thread. Fixes #18977
1 parent 8855b21 commit a397afe

9 files changed

Lines changed: 204 additions & 184 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/di/DefaultInstantiator.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ private I18NProvider getI18NProviderInstance() {
100100
// If no i18n provider provided check if the default location has
101101
// translation files (lang coded or just the default)
102102
List<Locale> defaultTranslationLocales = I18NUtil
103-
.getDefaultTranslationLocales();
103+
.getDefaultTranslationLocales(getClassLoader());
104104
if (!defaultTranslationLocales.isEmpty()
105-
|| I18NUtil.containsDefaultTranslation()) {
105+
|| I18NUtil.containsDefaultTranslation(getClassLoader())) {
106106
// Some lang files were found in default location initialize
107107
// default I18N provider.
108-
return new DefaultI18NProvider(defaultTranslationLocales);
108+
return new DefaultI18NProvider(defaultTranslationLocales,
109+
getClassLoader());
109110
}
110111
return null;
111112
}
@@ -128,6 +129,10 @@ private I18NProvider getI18NProviderInstance() {
128129
return null;
129130
}
130131

132+
protected ClassLoader getClassLoader() {
133+
return getClass().getClassLoader();
134+
}
135+
131136
/**
132137
* Get the I18NProvider property from the session configurator or try to
133138
* load it from application.properties property file.

‎flow-server/src/main/java/com/vaadin/flow/i18n/DefaultI18NProvider.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
public class DefaultI18NProvider implements I18NProvider {
3434

3535
final List<Locale> providedLocales;
36+
private final ClassLoader classLoader;
3637

3738
public static final String BUNDLE_FOLDER = "vaadin-i18n";
3839
public static final String BUNDLE_FILENAME = "translations";
@@ -49,7 +50,23 @@ public class DefaultI18NProvider implements I18NProvider {
4950
* locale.
5051
*/
5152
public DefaultI18NProvider(List<Locale> providedLocales) {
53+
this(providedLocales, DefaultI18NProvider.class.getClassLoader());
54+
}
55+
56+
/**
57+
* Construct {@link DefaultI18NProvider} for a list of locales that we have
58+
* translations for. Enables giving a specific classloader if needed.
59+
*
60+
* @param providedLocales
61+
* List of locales. The first locale should be the default
62+
* locale.
63+
* @param classLoader
64+
* ClassLoader to use for loading translation bundles.
65+
*/
66+
public DefaultI18NProvider(List<Locale> providedLocales,
67+
ClassLoader classLoader) {
5268
this.providedLocales = Collections.unmodifiableList(providedLocales);
69+
this.classLoader = classLoader;
5370
}
5471

5572
@Override
@@ -94,11 +111,10 @@ private ResourceBundle getBundle(Locale locale) {
94111

95112
ResourceBundle getBundle(Locale locale, ResourceBundle.Control control) {
96113
if (control == null) {
97-
return ResourceBundle.getBundle(BUNDLE_PREFIX, locale,
98-
I18NUtil.getClassLoader());
114+
return ResourceBundle.getBundle(BUNDLE_PREFIX, locale, classLoader);
99115
}
100-
return ResourceBundle.getBundle(BUNDLE_PREFIX, locale,
101-
I18NUtil.getClassLoader(), control);
116+
return ResourceBundle.getBundle(BUNDLE_PREFIX, locale, classLoader,
117+
control);
102118
}
103119

104120
static Logger getLogger() {

‎flow-server/src/main/java/com/vaadin/flow/i18n/I18NUtil.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ public final class I18NUtil {
5353
*
5454
* @return {@code true} if default property file found
5555
*/
56-
public static boolean containsDefaultTranslation() {
57-
URL resource = getClassLoader()
58-
.getResource(DefaultI18NProvider.BUNDLE_FOLDER + "/"
59-
+ DefaultI18NProvider.BUNDLE_FILENAME
60-
+ PROPERTIES_SUFFIX);
56+
public static boolean containsDefaultTranslation(ClassLoader classLoader) {
57+
URL resource = classLoader.getResource(DefaultI18NProvider.BUNDLE_FOLDER
58+
+ "/" + DefaultI18NProvider.BUNDLE_FILENAME
59+
+ PROPERTIES_SUFFIX);
6160
if (resource == null) {
6261
return false;
6362
}
@@ -73,10 +72,11 @@ public static boolean containsDefaultTranslation() {
7372
*
7473
* @return List of locales parsed from property files.
7574
*/
76-
public static List<Locale> getDefaultTranslationLocales() {
75+
public static List<Locale> getDefaultTranslationLocales(
76+
ClassLoader classLoader) {
7777
List<Locale> locales = new ArrayList<>();
7878

79-
URL resource = getClassLoader()
79+
URL resource = classLoader
8080
.getResource(DefaultI18NProvider.BUNDLE_FOLDER);
8181
if (resource == null) {
8282
return locales;
@@ -157,10 +157,6 @@ protected static List<File> getTranslationFiles(URL resource) {
157157
return files;
158158
}
159159

160-
protected static ClassLoader getClassLoader() {
161-
return Thread.currentThread().getContextClassLoader();
162-
}
163-
164160
protected static Logger getLogger() {
165161
return LoggerFactory.getLogger(I18NUtil.class);
166162
}

‎flow-server/src/test/java/com/vaadin/flow/i18n/DefaultInstantiatorI18NTest.java‎

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,38 @@ public void translationFileOnClasspath_instantiateDefaultI18N()
7676

7777
createTranslationFiles(translations);
7878

79-
try (MockedStatic<I18NUtil> util = Mockito.mockStatic(I18NUtil.class,
80-
Mockito.CALLS_REAL_METHODS)) {
81-
util.when(() -> I18NUtil.getClassLoader())
82-
.thenReturn(urlClassLoader);
83-
84-
VaadinService service = Mockito.mock(VaadinService.class);
85-
mockLookup(service);
86-
87-
I18NProvider i18NProvider = new DefaultInstantiator(service)
88-
.getI18NProvider();
89-
Assert.assertNotNull(i18NProvider);
90-
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider);
91-
92-
Assert.assertEquals("Suomi", i18NProvider.getTranslation("title",
93-
new Locale("fi", "FI")));
94-
95-
Assert.assertEquals("deutsch",
96-
i18NProvider.getTranslation("title", new Locale("de")));
97-
98-
Assert.assertEquals(
99-
"non existing country should select language bundle",
100-
"deutsch", i18NProvider.getTranslation("title",
101-
new Locale("de", "AT")));
102-
103-
Assert.assertEquals("Korean", i18NProvider.getTranslation("title",
104-
new Locale("ko", "KR")));
105-
106-
// Note!
107-
// default translations.properties will be used if
108-
// the locale AND system default locale is not found
109-
Assert.assertEquals("Default lang", i18NProvider
110-
.getTranslation("title", new Locale("en", "GB")));
111-
}
79+
VaadinService service = Mockito.mock(VaadinService.class);
80+
mockLookup(service);
81+
82+
DefaultInstantiator defaultInstantiator = new DefaultInstantiator(
83+
service) {
84+
@Override
85+
protected ClassLoader getClassLoader() {
86+
return urlClassLoader;
87+
}
88+
};
89+
I18NProvider i18NProvider = defaultInstantiator.getI18NProvider();
90+
Assert.assertNotNull(i18NProvider);
91+
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider);
92+
93+
Assert.assertEquals("Suomi",
94+
i18NProvider.getTranslation("title", new Locale("fi", "FI")));
95+
96+
Assert.assertEquals("deutsch",
97+
i18NProvider.getTranslation("title", new Locale("de")));
98+
99+
Assert.assertEquals(
100+
"non existing country should select language bundle", "deutsch",
101+
i18NProvider.getTranslation("title", new Locale("de", "AT")));
102+
103+
Assert.assertEquals("Korean",
104+
i18NProvider.getTranslation("title", new Locale("ko", "KR")));
105+
106+
// Note!
107+
// default translations.properties will be used if
108+
// the locale AND system default locale is not found
109+
Assert.assertEquals("Default lang",
110+
i18NProvider.getTranslation("title", new Locale("en", "GB")));
112111
}
113112

114113
@Test
@@ -119,34 +118,34 @@ public void onlyDefaultTranslation_instantiateDefaultI18N()
119118
Files.writeString(file.toPath(), "title=Default lang",
120119
StandardCharsets.UTF_8, StandardOpenOption.CREATE);
121120

122-
try (MockedStatic<I18NUtil> util = Mockito.mockStatic(I18NUtil.class,
123-
Mockito.CALLS_REAL_METHODS)) {
124-
util.when(() -> I18NUtil.getClassLoader())
125-
.thenReturn(urlClassLoader);
126-
127-
VaadinService service = Mockito.mock(VaadinService.class);
128-
mockLookup(service);
129-
130-
I18NProvider i18NProvider = new DefaultInstantiator(service)
131-
.getI18NProvider();
132-
Assert.assertNotNull(i18NProvider);
133-
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider);
134-
135-
Assert.assertEquals("Default lang", i18NProvider
136-
.getTranslation("title", new Locale("fi", "FI")));
137-
138-
Assert.assertEquals("Default lang",
139-
i18NProvider.getTranslation("title", new Locale("de")));
140-
141-
Assert.assertEquals("Default lang", i18NProvider
142-
.getTranslation("title", new Locale("ko", "KR")));
143-
144-
// Note!
145-
// default translations.properties will be used if
146-
// the locale AND system default locale is not found
147-
Assert.assertEquals("Default lang", i18NProvider
148-
.getTranslation("title", new Locale("en", "GB")));
149-
}
121+
VaadinService service = Mockito.mock(VaadinService.class);
122+
mockLookup(service);
123+
124+
DefaultInstantiator defaultInstantiator = new DefaultInstantiator(
125+
service) {
126+
@Override
127+
protected ClassLoader getClassLoader() {
128+
return urlClassLoader;
129+
}
130+
};
131+
I18NProvider i18NProvider = defaultInstantiator.getI18NProvider();
132+
Assert.assertNotNull(i18NProvider);
133+
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider);
134+
135+
Assert.assertEquals("Default lang",
136+
i18NProvider.getTranslation("title", new Locale("fi", "FI")));
137+
138+
Assert.assertEquals("Default lang",
139+
i18NProvider.getTranslation("title", new Locale("de")));
140+
141+
Assert.assertEquals("Default lang",
142+
i18NProvider.getTranslation("title", new Locale("ko", "KR")));
143+
144+
// Note!
145+
// default translations.properties will be used if
146+
// the locale AND system default locale is not found
147+
Assert.assertEquals("Default lang",
148+
i18NProvider.getTranslation("title", new Locale("en", "GB")));
150149
}
151150

152151
@Test
@@ -157,25 +156,25 @@ public void onlyLangTransalation_nonExistingLangReturnsKey()
157156
Files.writeString(file.toPath(), "title=No Default",
158157
StandardCharsets.UTF_8, StandardOpenOption.CREATE);
159158

160-
try (MockedStatic<I18NUtil> util = Mockito.mockStatic(I18NUtil.class,
161-
Mockito.CALLS_REAL_METHODS)) {
162-
util.when(() -> I18NUtil.getClassLoader())
163-
.thenReturn(urlClassLoader);
164-
165-
VaadinService service = Mockito.mock(VaadinService.class);
166-
mockLookup(service);
167-
168-
I18NProvider i18NProvider = new DefaultInstantiator(service)
169-
.getI18NProvider();
170-
Assert.assertNotNull(i18NProvider);
171-
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider);
172-
173-
Assert.assertEquals("No Default",
174-
i18NProvider.getTranslation("title", new Locale("ja")));
175-
176-
Assert.assertEquals("title", i18NProvider.getTranslation("title",
177-
new Locale("en", "GB")));
178-
}
159+
VaadinService service = Mockito.mock(VaadinService.class);
160+
mockLookup(service);
161+
162+
DefaultInstantiator defaultInstantiator = new DefaultInstantiator(
163+
service) {
164+
@Override
165+
protected ClassLoader getClassLoader() {
166+
return urlClassLoader;
167+
}
168+
};
169+
I18NProvider i18NProvider = defaultInstantiator.getI18NProvider();
170+
Assert.assertNotNull(i18NProvider);
171+
Assert.assertTrue(i18NProvider instanceof DefaultI18NProvider);
172+
173+
Assert.assertEquals("No Default",
174+
i18NProvider.getTranslation("title", new Locale("ja")));
175+
176+
Assert.assertEquals("title",
177+
i18NProvider.getTranslation("title", new Locale("en", "GB")));
179178
}
180179

181180
private static void createTranslationFiles(File translationsFolder)

0 commit comments

Comments
 (0)