Skip to content

fix: refresh web component after updating i18n #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<description>Year Month Calendar Add-on for Vaadin Flow</description>

<properties>
<vaadin.version>24.1.0</vaadin.version>
<vaadin.version>24.4.4</vaadin.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>com.flowingcode.vaadin.addons.demo</groupId>
<artifactId>commons-demo</artifactId>
<version>3.6.0</version>
<version>4.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,10 +61,9 @@ public void setI18n(DatePickerI18n i18n) {

private void setI18nWithJS() {
runBeforeClientResponse(ui -> {
// Merge current web component I18N settings with new I18N settings
JsonObject i18nObject = (JsonObject) JsonSerializer.toJson(i18n);
for (String key : i18nObject.keys()) {
getElement().executeJs("this.set('i18n." + key + "', $0)", i18nObject.get(key));
}
getElement().executeJs("this.i18n = Object.assign({}, this.i18n, $0);", i18nObject);
});
}

Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/flowingcode/addons/ycalendar/LocaleSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*-
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* %%
* 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.
* #L%
*/
package com.flowingcode.addons.ycalendar;

import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.datepicker.DatePicker.DatePickerI18n;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.function.SerializableConsumer;
import java.text.DateFormatSymbols;
import java.time.DayOfWeek;
import java.time.temporal.WeekFields;
import java.util.List;
import java.util.Locale;

@SuppressWarnings("serial")
public class LocaleSelector extends Composite<RadioButtonGroup<Locale>> {

private final SerializableConsumer<DatePicker.DatePickerI18n> consumer;

public LocaleSelector(SerializableConsumer<DatePickerI18n> consumer) {
this.consumer = consumer;
getContent().addValueChangeListener(ev -> updateLocale(ev.getValue()));
getContent().setItems(Locale.ENGLISH, Locale.GERMAN, new Locale("es"));
getContent().setItemLabelGenerator(locale -> locale.getDisplayLanguage(Locale.ENGLISH));
getContent().setLabel("Choose Language");
getContent().setValue(Locale.ENGLISH);
}

private void updateLocale(Locale locale) {
DatePickerI18n i18n = new DatePickerI18n();
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
i18n.setMonthNames(List.of(dfs.getMonths()));
i18n.setWeekdays(List.of(dfs.getWeekdays()).stream().skip(1).toList());
i18n.setWeekdaysShort(List.of(dfs.getShortWeekdays()).stream().skip(1).toList());

DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
i18n.setFirstDayOfWeek(firstDayOfWeek.getValue() % 7);

consumer.accept(i18n);
}

}
8 changes: 5 additions & 3 deletions src/test/java/com/flowingcode/addons/ycalendar/MonthDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,8 +54,10 @@ public MonthDemo() {
selectedDate.setText("Selected date: " + ev.getDate());
});

Span instructions = new Span("Use arrow keys to move.");
add(new HorizontalLayout(instructions, selectedDate), calendar);
Span instructions = new Span("Use arrow keys to move."); // hide-source
add(new HorizontalLayout(instructions, selectedDate)); // hide-source
add(new LocaleSelector(calendar::setI18n)); // hide-source
add(calendar);
}

}
14 changes: 12 additions & 2 deletions src/test/java/com/flowingcode/addons/ycalendar/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,8 @@ public class TestUtils {
HOLIDAYS.addAll(computeEasterHolidays(2021, 4, 4));
HOLIDAYS.addAll(computeEasterHolidays(2022, 4, 17));
HOLIDAYS.addAll(computeEasterHolidays(2023, 4, 9));
HOLIDAYS.addAll(computeEasterHolidays(2024, 3, 31));
HOLIDAYS.addAll(computeEasterHolidays(2025, 4, 20));

HOLIDAYS.add(LocalDate.of(2021, 5, 24));
HOLIDAYS.add(LocalDate.of(2021, 6, 21));
Expand All @@ -42,6 +44,7 @@ public class TestUtils {
HOLIDAYS.add(LocalDate.of(2022, 8, 15));
HOLIDAYS.add(LocalDate.of(2021, 10, 8));
HOLIDAYS.add(LocalDate.of(2021, 10, 11));

HOLIDAYS.add(LocalDate.of(2022, 10, 7));
HOLIDAYS.add(LocalDate.of(2022, 10, 10));
HOLIDAYS.add(LocalDate.of(2021, 11, 20));
Expand All @@ -50,13 +53,20 @@ public class TestUtils {
HOLIDAYS.add(LocalDate.of(2022, 11, 21));
HOLIDAYS.add(LocalDate.of(2022, 12, 9));
HOLIDAYS.add(LocalDate.of(2022, 12, 25));

HOLIDAYS.add(LocalDate.of(2023, 5, 26));
HOLIDAYS.add(LocalDate.of(2023, 6, 19));
HOLIDAYS.add(LocalDate.of(2023, 8, 21));
HOLIDAYS.add(LocalDate.of(2023, 10, 13));
HOLIDAYS.add(LocalDate.of(2023, 10, 16));
HOLIDAYS.add(LocalDate.of(2023, 11, 20));

HOLIDAYS.add(LocalDate.of(2024, 4, 1));
HOLIDAYS.add(LocalDate.of(2024, 6, 17));
HOLIDAYS.add(LocalDate.of(2024, 6, 21));
HOLIDAYS.add(LocalDate.of(2024, 10, 11));
HOLIDAYS.add(LocalDate.of(2024, 11, 18));

}

private static List<LocalDate> computeEasterHolidays(int y, int m, int d) {
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/com/flowingcode/addons/ycalendar/YearDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.router.PageTitle;
Expand All @@ -45,6 +46,7 @@ public class YearDemo extends Div {
public YearDemo() {
addClassName("year-demo");
YearCalendar calendar = new YearCalendar();

calendar.setClassNameGenerator(date -> {
if (TestUtils.isPublicHoliday(date)) {
return "holiday";
Expand All @@ -62,6 +64,7 @@ public YearDemo() {
selectedDate.setText("Selected date: " + ev.getDate());
});

// #if vaadin eq 0
Span instructions = new Span("Use arrow keys or Ctrl+arrow keys to move.");

IntegerField yearField = new IntegerField();
Expand All @@ -78,9 +81,17 @@ public YearDemo() {
}

yearField.setValue(calendar.getYear());
yearField.setLabel("Change year");
yearField.addValueChangeListener(e -> calendar.setYear(e.getValue()));

add(new HorizontalLayout(instructions, selectedDate), yearField, calendar);
LocaleSelector localeSelector = new LocaleSelector(calendar::setI18n);
HorizontalLayout controls = new HorizontalLayout(yearField, localeSelector);
controls.setSpacing(true);
controls.setAlignItems(Alignment.BASELINE);

add(new HorizontalLayout(instructions, selectedDate), controls);
// #endif
add(calendar);
}

}
Expand Down
Loading