Skip to content

Commit f78e120

Browse files
author
Felipe Lang
authored
feat: add i18n support
Close #43
1 parent ace5125 commit f78e120

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@
193193
<artifactId>jetty-maven-plugin</artifactId>
194194
<version>${jetty.version}</version>
195195
<configuration>
196+
<systemProperties>
197+
<systemProperty>
198+
<key>vaadin.i18n.provider</key>
199+
<value>com.flowingcode.vaadin.addons.errorwindow.TranslationProvider</value>
200+
</systemProperty>
201+
</systemProperties>
196202
<scanIntervalSeconds>3</scanIntervalSeconds>
197203
<!-- Use test scope because the UI/demo classes are in
198204
the test package. -->

src/main/java/com/flowingcode/vaadin/addons/errorwindow/ErrorWindowI18n.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
package com.flowingcode.vaadin.addons.errorwindow;
2323

24+
import com.vaadin.flow.function.SerializableFunction;
25+
2426
/**
2527
* Internationalization object for customizing the component UI texts. An instance with the default
2628
* messages can be obtained using {@link ErrorWindowI18n#createDefault()}
@@ -45,11 +47,26 @@ private ErrorWindowI18n() {
4547
this.clipboard = "Copy details to clipboard";
4648
}
4749

50+
private ErrorWindowI18n(SerializableFunction<String, String> i18nTranslator) {
51+
this.caption = i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.caption");
52+
this.instructions =
53+
i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.instructions");
54+
this.close = i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.close");
55+
this.details = i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.details");
56+
this.defaultErrorMessage =
57+
i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.defaultErrorMessage");
58+
this.clipboard = i18nTranslator.apply("com.flowingcode.vaadin.addons.errorwindow.clipboard");
59+
}
60+
4861
/** @return a new instance with the default messages */
4962
public static ErrorWindowI18n createDefault() {
5063
return new ErrorWindowI18n();
5164
}
5265

66+
public static ErrorWindowI18n create(SerializableFunction<String, String> i18nTranslator) {
67+
return new ErrorWindowI18n(i18nTranslator);
68+
}
69+
5370
/** Sets the caption of the error window. */
5471
public void setCaption(final String caption) {
5572
this.caption = caption;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*-
2+
* #%L
3+
* Error Window Add-on
4+
* %%
5+
* Copyright (C) 2017 - 2022 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
21+
22+
package com.flowingcode.vaadin.addons.errorwindow;
23+
24+
public class I18nErrorWindowFactory implements ErrorWindowFactory {
25+
26+
private final ErrorWindowI18n errorWindowI18n;
27+
28+
I18nErrorWindowFactory(ErrorWindowI18n errorWindowI18n){
29+
this.errorWindowI18n = errorWindowI18n;
30+
}
31+
32+
@Override
33+
public void showError(ErrorDetails details) {
34+
new ErrorWindow(details, errorWindowI18n).open();
35+
}
36+
}

src/test/java/com/flowingcode/vaadin/addons/errorwindow/ErrorwindowDemo.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
public class ErrorwindowDemo extends VerticalLayout {
3939

4040
public ErrorwindowDemo() {
41+
ErrorWindowI18n errorWindowI18n = ErrorWindowI18n.create(this::getTranslation);
42+
ErrorManager.setErrorWindowFactory(new I18nErrorWindowFactory(errorWindowI18n));
43+
4144
Button errorButton =
4245
new Button(
4346
"Throw Error",
@@ -87,6 +90,17 @@ public ErrorwindowDemo() {
8790
}
8891
});
8992

93+
Button throwErrori18nSupport =
94+
new Button(
95+
"Throw Error with i18n support",
96+
ev -> {
97+
try {
98+
Integer.parseInt("asdf");
99+
} catch (NumberFormatException e) {
100+
new ErrorWindow(e, "CUSTOM ERROR MESSAGE", errorWindowI18n).open();
101+
}
102+
});
103+
90104
Checkbox productionModeCb = new Checkbox("Production Mode");
91105
productionModeCb.addValueChangeListener(
92106
e -> {
@@ -106,7 +120,8 @@ public ErrorwindowDemo() {
106120
errorButton,
107121
throwErrorWithoutErrorHandler,
108122
throwErrorWithCustomMessageAndCustomTexts,
109-
throwErrorWithCustomMessage);
123+
throwErrorWithCustomMessage,
124+
throwErrori18nSupport);
110125

111126
add(new Button("Navigation error",
112127
ev -> UI.getCurrent().navigate(ThrowInConstructorView.class)));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.flowingcode.vaadin.addons.errorwindow;
2+
3+
import com.vaadin.flow.i18n.I18NProvider;
4+
import java.text.MessageFormat;
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Locale;
9+
import java.util.MissingResourceException;
10+
import java.util.ResourceBundle;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class TranslationProvider implements I18NProvider {
14+
15+
public static final String BUNDLE_PREFIX = "messages";
16+
17+
public final Locale LOCALE_ES = new Locale("es");
18+
public final Locale LOCALE_EN = new Locale("en");
19+
20+
private List<Locale> locales = Collections.unmodifiableList(Arrays.asList(LOCALE_ES, LOCALE_EN));
21+
22+
@Override
23+
public List<Locale> getProvidedLocales() {
24+
return locales;
25+
}
26+
27+
@Override
28+
public String getTranslation(String key, Locale locale, Object... params) {
29+
if (key == null) {
30+
LoggerFactory.getLogger(TranslationProvider.class.getName())
31+
.warn("Got lang request for key with null value!");
32+
return "";
33+
}
34+
35+
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_PREFIX, locale);
36+
37+
String value;
38+
try {
39+
value = bundle.getString(key);
40+
} catch (final MissingResourceException e) {
41+
LoggerFactory.getLogger(TranslationProvider.class.getName()).warn("Missing resource", e);
42+
return "!" + locale.getLanguage() + ": " + key;
43+
}
44+
if (params.length > 0) {
45+
value = MessageFormat.format(value, params);
46+
}
47+
return value;
48+
}
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
com.flowingcode.vaadin.addons.errorwindow.caption=An error has occurred
2+
com.flowingcode.vaadin.addons.errorwindow.instructions=Please report the following code to your system administrator
3+
com.flowingcode.vaadin.addons.errorwindow.close=Close
4+
com.flowingcode.vaadin.addons.errorwindow.details=Show error details
5+
com.flowingcode.vaadin.addons.errorwindow.defaultErrorMessage=Please contact the system administrator for more information.
6+
com.flowingcode.vaadin.addons.errorwindow.clipboard=Copy details to clipboard
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
com.flowingcode.vaadin.addons.errorwindow.caption=Ha ocurrido un error
2+
com.flowingcode.vaadin.addons.errorwindow.instructions=Reporta el siguiente código al administrador del sistema
3+
com.flowingcode.vaadin.addons.errorwindow.close=Cerrar
4+
com.flowingcode.vaadin.addons.errorwindow.details=Ver detalles del error
5+
com.flowingcode.vaadin.addons.errorwindow.defaultErrorMessage=Para más información contacta al administrador del sistema.
6+
com.flowingcode.vaadin.addons.errorwindow.clipboard=Copiar detalles al portapapeles

0 commit comments

Comments
 (0)