Skip to content

Commit c5028da

Browse files
javier-godoypaodb
authored andcommitted
feat(demo): add I18N settings in YearDemo and MonthDemo
1 parent 974a750 commit c5028da

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-
2+
* #%L
3+
* Year Month Calendar Add-on
4+
* %%
5+
* Copyright (C) 2021 - 2024 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+
package com.flowingcode.addons.ycalendar;
21+
22+
import com.vaadin.flow.component.Composite;
23+
import com.vaadin.flow.component.datepicker.DatePicker;
24+
import com.vaadin.flow.component.datepicker.DatePicker.DatePickerI18n;
25+
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
26+
import com.vaadin.flow.function.SerializableConsumer;
27+
import java.text.DateFormatSymbols;
28+
import java.time.DayOfWeek;
29+
import java.time.temporal.WeekFields;
30+
import java.util.List;
31+
import java.util.Locale;
32+
33+
@SuppressWarnings("serial")
34+
public class LocaleSelector extends Composite<RadioButtonGroup<Locale>> {
35+
36+
private final SerializableConsumer<DatePicker.DatePickerI18n> consumer;
37+
38+
public LocaleSelector(SerializableConsumer<DatePickerI18n> consumer) {
39+
this.consumer = consumer;
40+
getContent().addValueChangeListener(ev -> updateLocale(ev.getValue()));
41+
getContent().setItems(Locale.ENGLISH, Locale.GERMAN, new Locale("es"));
42+
getContent().setItemLabelGenerator(locale -> locale.getDisplayLanguage(Locale.ENGLISH));
43+
getContent().setLabel("Choose Language");
44+
getContent().setValue(Locale.ENGLISH);
45+
}
46+
47+
private void updateLocale(Locale locale) {
48+
DatePickerI18n i18n = new DatePickerI18n();
49+
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
50+
i18n.setMonthNames(List.of(dfs.getMonths()));
51+
i18n.setWeekdays(List.of(dfs.getWeekdays()).stream().skip(1).toList());
52+
i18n.setWeekdaysShort(List.of(dfs.getShortWeekdays()).stream().skip(1).toList());
53+
54+
DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
55+
i18n.setFirstDayOfWeek(firstDayOfWeek.getValue() % 7);
56+
57+
consumer.accept(i18n);
58+
}
59+
60+
}

src/test/java/com/flowingcode/addons/ycalendar/MonthDemo.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Year Month Calendar Add-on
44
* %%
5-
* Copyright (C) 2021 - 2023 Flowing Code
5+
* Copyright (C) 2021 - 2024 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -54,8 +54,10 @@ public MonthDemo() {
5454
selectedDate.setText("Selected date: " + ev.getDate());
5555
});
5656

57-
Span instructions = new Span("Use arrow keys to move.");
58-
add(new HorizontalLayout(instructions, selectedDate), calendar);
57+
Span instructions = new Span("Use arrow keys to move."); // hide-source
58+
add(new HorizontalLayout(instructions, selectedDate)); // hide-source
59+
add(new LocaleSelector(calendar::setI18n)); // hide-source
60+
add(calendar);
5961
}
6062

6163
}

src/test/java/com/flowingcode/addons/ycalendar/YearDemo.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Year Month Calendar Add-on
44
* %%
5-
* Copyright (C) 2021 - 2023 Flowing Code
5+
* Copyright (C) 2021 - 2024 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import com.vaadin.flow.component.html.Div;
2525
import com.vaadin.flow.component.html.Span;
2626
import com.vaadin.flow.component.notification.Notification;
27+
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
2728
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
2829
import com.vaadin.flow.component.textfield.IntegerField;
2930
import com.vaadin.flow.router.PageTitle;
@@ -45,6 +46,7 @@ public class YearDemo extends Div {
4546
public YearDemo() {
4647
addClassName("year-demo");
4748
YearCalendar calendar = new YearCalendar();
49+
4850
calendar.setClassNameGenerator(date -> {
4951
if (TestUtils.isPublicHoliday(date)) {
5052
return "holiday";
@@ -62,6 +64,7 @@ public YearDemo() {
6264
selectedDate.setText("Selected date: " + ev.getDate());
6365
});
6466

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

6770
IntegerField yearField = new IntegerField();
@@ -78,9 +81,17 @@ public YearDemo() {
7881
}
7982

8083
yearField.setValue(calendar.getYear());
84+
yearField.setLabel("Change year");
8185
yearField.addValueChangeListener(e -> calendar.setYear(e.getValue()));
8286

83-
add(new HorizontalLayout(instructions, selectedDate), yearField, calendar);
87+
LocaleSelector localeSelector = new LocaleSelector(calendar::setI18n);
88+
HorizontalLayout controls = new HorizontalLayout(yearField, localeSelector);
89+
controls.setSpacing(true);
90+
controls.setAlignItems(Alignment.BASELINE);
91+
92+
add(new HorizontalLayout(instructions, selectedDate), controls);
93+
// #endif
94+
add(calendar);
8495
}
8596

8697
}

0 commit comments

Comments
 (0)