Skip to content

Commit

Permalink
fix: i18n default translation from thread
Browse files Browse the repository at this point in the history
Fixes getting translation bundle
when executing from thread.

Fixes #18977
  • Loading branch information
caalador committed Apr 9, 2024
1 parent 90abc04 commit f1bc584
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected static List<File> getTranslationFiles(URL resource) {
}

protected static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
return I18NUtil.class.getClassLoader();
}

protected static Logger getLogger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.i18n.I18NProvider;
Expand All @@ -30,6 +32,8 @@ public class TranslationView extends Div {
public static final String TEST_VIEW_ID = "TranslationView";
public static final String LOCALES_ID = "available-locales";

private Span dynamic;

public TranslationView() {
setId(TEST_VIEW_ID);

Expand Down Expand Up @@ -61,7 +65,28 @@ public TranslationView() {
localeSpan.setId(LOCALES_ID);
add(localeSpan, new Div());
}
dynamic = new Span("waiting");
dynamic.setId("dynamic");

add(defaultLang, new Div(), german, new Div(), germany, new Div(),
finnish, new Div(), french, new Div(), japanese);
finnish, new Div(), french, new Div(), japanese, new Div(),
dynamic);
}

@Override
protected void onAttach(AttachEvent event) {
event.getUI().setPollInterval(100);
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
} finally {
event.getUI().access(() -> dynamic
.setText(getTranslation("label", Locale.FRANCE)));
event.getUI().setPollInterval(-1);
}
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ public void translationFilesExist_defaultI18NInstantiated_languagesWork() {
Assert.assertEquals("日本語",
$(SpanElement.class).id("japanese").getText());
}

@Test
public void translationFilesExist_defaultI18NInstantiated_updateFromExternalThreadWorks() {
open();

waitUntilNot(driver -> $(SpanElement.class).id("dynamic").getText()
.equals("waiting"));

Assert.assertEquals(
"Dynamic update from thread should have used correct bundle.",
"français", $(SpanElement.class).id("dynamic").getText());
}
}

0 comments on commit f1bc584

Please sign in to comment.