Skip to content
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

Use Locale for determinating I18N values #1382

Open
pleku opened this issue Mar 21, 2018 · 7 comments
Open

Use Locale for determinating I18N values #1382

pleku opened this issue Mar 21, 2018 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed I18N vaadin-date-picker

Comments

@pleku
Copy link
Contributor

pleku commented Mar 21, 2018

After vaadin/vaadin-date-picker-flow#38, the component will have locale support for displaying the selected date value, but it is not applied to the date picker popup, which is localized via the properties in the DatePickerI18N object.

The set locale should be used for figuring out the following properties for the DatePickerI18N object:

  • month names
  • weekdays
  • weekdays short
  • first day of week

Acceptance Criteria

  • the locale set for the component on server side controls the properties described above
  • the user is still able to override those for the I18N object
@nemojmenervirat
Copy link

the user is still able to override those for the I18N object

it would be nice to override this only once and every date picker in UI will have new I18N object, instead of setting I18N object for every instance of date picker

@nbruck
Copy link

nbruck commented Apr 7, 2019

Just want the mention that the picker popup need to display also the correct year in case the locale is changing the year due to Transformation from gregorian calender to buddhist for example.

@vaadin-bot vaadin-bot transferred this issue from vaadin/vaadin-date-picker-flow Oct 6, 2020
@vaadin-bot vaadin-bot transferred this issue from vaadin/vaadin-date-picker May 19, 2021
@vaadin-bot vaadin-bot transferred this issue from vaadin/web-components May 21, 2021
@vaadin-bot vaadin-bot added enhancement New feature or request help wanted Extra attention is needed vaadin-date-picker labels May 21, 2021
@rolfsmeds rolfsmeds added the I18N label Jun 15, 2021
@web-padawan
Copy link
Member

Related issue in the web component (let's keep both open for now): vaadin/web-components#3383

@Frettman
Copy link

Frettman commented Mar 22, 2022

Just like the date pattern, the four properties mentioned in this issue can be determined automatically based on the locale (with information already provided by Java), which would help a lot and only leaves a few labels to be translated.

The following code determines the four properties based on locale. Unfortunately there's no hook to automatically set this; or update it in case of a locale change. But might still be useful to some:

DatePicker.DatePickerI18n i18n = new DatePicker.DatePickerI18n();

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

i18n.setMonthNames(Stream.of(Month.values()).map(m -> m.getDisplayName(TextStyle.FULL_STANDALONE, locale))
    .collect(toList()));

List<String> weekdays = Stream.of(DayOfWeek.values())
    .map(d -> d.getDisplayName(TextStyle.FULL_STANDALONE, locale)).collect(toList());
Collections.rotate(weekdays, 1);
i18n.setWeekdays(weekdays);

List<String> weekdaysShort = Stream.of(DayOfWeek.values())
    .map(d -> d.getDisplayName(TextStyle.SHORT_STANDALONE, locale)).collect(toList());
Collections.rotate(weekdaysShort, 1);
i18n.setWeekdaysShort(weekdaysShort);

@mstahv
Copy link
Member

mstahv commented Mar 31, 2022

@Frettman Your code is awesome 😍 I changed it slightly, so that it also works with weird languages, like the one we speak here in Finland. Then integrated into VDatePicker and VDateTimePicker in Viritin. They now read proper settings from active locale 😎:

viritin/flow-viritin@0f887f7

Available in in.virit:viritin:1.3.0 as soon as Maven central gets synchronised.

@Frettman
Copy link

@mstahv Glad it was useful :) There's one thing I should add though: WeekFields#getFirstDayOfWeek only works if the locale contains a country. Otherwise it falls back to Sunday being the first day of the week, which e.g. for de doesn't really make sense. So for locales without country I'm experimenting with just picking the first locale from Locale.getAvailableLocales() with the same language that has a country. Certainly not perfect and might get it wrong as much as it gets it right. The only save way would be to make sure all used locales contain a country. Certainly out of scope for your add-on but might be worth adding to the Javadoc somewhere or even a warning that's logged once :)

@mstahv
Copy link
Member

mstahv commented Mar 31, 2022

Yup, complete locale is needed indeed. I have no idea how common it is to use locales without country and if that is on purpose or not. Depending on that, it might be that the additional logic you described would make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed I18N vaadin-date-picker
Projects
None yet
Development

No branches or pull requests

8 participants