Skip to content

Commit

Permalink
fix: use quarkus compatible classloader (CP: 2.0) (#166)
Browse files Browse the repository at this point in the history
* fix: use quarkus compatible classloader

Use a classLoader that is
compatible with quarkus for
default i18N translation.

part of vaadin/flow#18977

* fix theme pom

---------

Co-authored-by: Mikael Grankvist <[email protected]>
  • Loading branch information
mcollovati and caalador authored Sep 3, 2024
1 parent 8595c07 commit 72e6493
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 643 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.quarkus.it.i18n;

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

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.i18n.I18NProvider;
import com.vaadin.flow.internal.LocaleUtil;
import com.vaadin.flow.router.Route;

@Route(value = "translations")
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);

Span defaultLang = new Span(getTranslation("label", Locale.ENGLISH));
defaultLang.setId("english");
Span german = new Span(getTranslation("label", Locale.GERMAN));
german.setId("german");
Span germany = new Span(getTranslation("label", Locale.GERMANY));
germany.setId("germany");
Span finnish = new Span(
getTranslation("label", new Locale("fi", "FI")));
finnish.setId("finnish");
Span french = new Span(getTranslation("label", Locale.FRANCE));
french.setId("french");
Span japanese = new Span(getTranslation("label", Locale.JAPAN));
japanese.setId("japanese");

Optional<I18NProvider> i18NProvider = LocaleUtil.getI18NProvider();
if (i18NProvider.isPresent()) {
add(new Span("Available translation locales:"));
StringBuilder locales = new StringBuilder();
for (Locale locale : i18NProvider.get().getProvidedLocales()) {
if (locales.length() > 0) {
locales.append(", ");
}
locales.append(locale.toString());
}
Span localeSpan = new Span(locales.toString());
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, new Div(),
dynamic);
}

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label=Default
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label=Deutsch
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label=Suomi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label=français
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label=日本語
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow.quarkus.it;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.vaadin.flow.component.html.testbench.SpanElement;
import com.vaadin.flow.quarkus.it.i18n.TranslationView;
import com.vaadin.flow.test.AbstractChromeIT;

@QuarkusIntegrationTest
public class TranslationIT extends AbstractChromeIT {
@Override
protected String getTestPath() {
return "/translations";
}

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

String locales = $(SpanElement.class).id(TranslationView.LOCALES_ID)
.getText();
Assertions.assertTrue(locales.contains("de"),
"Couldn't verify German locale");
Assertions.assertTrue(locales.contains("fi_FI"),
"Couldn't verify Finnish locale");
Assertions.assertTrue(locales.contains("fr_FR"),
"Couldn't verify French locale");
Assertions.assertTrue(locales.contains("ja_JP"),
"Couldn't verify Japanese locale");

Assertions.assertEquals("Default",
$(SpanElement.class).id("english").getText());
Assertions.assertEquals("Deutsch",
$(SpanElement.class).id("german").getText());
Assertions.assertEquals("Deutsch",
$(SpanElement.class).id("germany").getText());
Assertions.assertEquals("Suomi",
$(SpanElement.class).id("finnish").getText());
Assertions.assertEquals("français",
$(SpanElement.class).id("french").getText());
Assertions.assertEquals("日本語",
$(SpanElement.class).id("japanese").getText());
}

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

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

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

0 comments on commit 72e6493

Please sign in to comment.