|
| 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 | +} |
0 commit comments