/** * Get the new {@link LocalizationContext} {@link Locale}, if available. * @return Optional {@link Locale} */ default Optional<Locale> getLocale() { return getSource().getLocale(); }
/** * Gets the NumberFormat to use to convert values * @param locale Locale to use * @return the numberFormat If a NumberFormat was specified using {@link #setNumberFormat(NumberFormat)}, this one * is returned. Otherwise, a NumberFormat is obtained using given Locale */ public NumberFormat getNumberFormat(Locale locale) { Locale lcl = (locale != null) ? locale : LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); return (numberFormat != null) ? numberFormat : TypeUtils.isDecimalNumber(numberType) ? NumberFormat.getNumberInstance(lcl) : NumberFormat.getIntegerInstance(lcl); }
/** * Gets the NumberFormat to use to convert values * @param locale Locale to use * @return the numberFormat If a NumberFormat was specified using {@link #setNumberFormat(NumberFormat)}, this one * is returned. Otherwise, a NumberFormat is obtained using given Locale */ public NumberFormat getNumberFormat(Locale locale) { Locale lcl = (locale != null) ? locale : LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); return (numberFormat != null) ? numberFormat : TypeUtils.isDecimalNumber(numberType) ? NumberFormat.getNumberInstance(lcl) : NumberFormat.getIntegerInstance(lcl); }
@Override public Optional<Locale> getLocale() { // check UI final UI currentUi = UI.getCurrent(); Locale locale = currentUi == null ? null : currentUi.getLocale(); if (locale == null) { // check LocalizationContext locale = getLocalizationContext().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()).orElse(null); } if (locale == null) { // check I18NProvider locale = getI18nProvider().map(p -> { List<Locale> locales = p.getProvidedLocales(); if (locales != null && !locales.isEmpty()) { return locales.get(0); } return null; }).orElse(null); } return Optional.ofNullable(locale); }
/** * Get the most suitable {@link Locale} to use. * @return the field, UI or {@link LocalizationContext} locale */ protected Locale findLocale() { Locale locale = getLocale(); if (locale == null && UI.getCurrent() != null) { locale = UI.getCurrent().getLocale(); } if (locale == null) { locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); } return locale; }
/** * Get the most suitable {@link Locale} to use. * @return the field, UI or {@link LocalizationContext} locale */ protected Locale findLocale() { Locale locale = getLocale(); if (locale == null && UI.getCurrent() != null) { locale = UI.getCurrent().getLocale(); } if (locale == null) { locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); } return locale; }
if (locale == null) { locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(null);
/** * Renders a Temporal value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderTemporal(Property<? extends T> property) { TemporalInputBuilder builder = null; if (LocalDate.class.isAssignableFrom(property.getType())) { builder = input.localDate(false); } else if (LocalDateTime.class.isAssignableFrom(property.getType())) { builder = input.localDateTime(false); } else { throw new UnsupportedTemporalTypeException( "Temporal type " + property.getType().getName() + " is not supported by default field renderer"); } final TemporalInputBuilder<Temporal, ?> b = builder; // set locale from LocalizationContext, if any LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap((c) -> c.getLocale()) .ifPresent((l) -> b.locale(l)); return postProcessField(b.asField(), property); }
/** * Renders a numeric value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderNumber(Property<? extends T> property) { // Number format Class<? extends Number> type = (Class<? extends Number>) property.getType(); int decimals = property.getConfiguration().getParameter(StringValuePresenter.DECIMAL_POSITIONS).orElse(-1); boolean disableGrouping = property.getConfiguration().getParameter(StringValuePresenter.DISABLE_GROUPING) .orElse(Boolean.FALSE); Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); NumberFormat numberFormat = LocalizationContext.getCurrent().filter(l -> l.isLocalized()) .map((l) -> l.getNumberFormat(type, decimals, disableGrouping)) .orElse(TypeUtils.isDecimalNumber(property.getType()) ? NumberFormat.getNumberInstance(locale) : NumberFormat.getIntegerInstance(locale)); if (decimals > -1) { numberFormat.setMinimumFractionDigits(decimals); numberFormat.setMaximumFractionDigits(decimals); } if (disableGrouping) { numberFormat.setGroupingUsed(false); } return postProcessField(input.number(type).numberFormat(numberFormat).asField(), property); }
/** * Renders a numeric value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderNumber(Property<? extends T> property) { // Number format Class<? extends Number> type = (Class<? extends Number>) property.getType(); int decimals = property.getConfiguration().getParameter(StringValuePresenter.DECIMAL_POSITIONS).orElse(-1); boolean disableGrouping = property.getConfiguration().getParameter(StringValuePresenter.DISABLE_GROUPING) .orElse(Boolean.FALSE); Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); NumberFormat numberFormat = LocalizationContext.getCurrent().filter(l -> l.isLocalized()) .map((l) -> l.getNumberFormat(type, decimals, disableGrouping)) .orElse(TypeUtils.isDecimalNumber(property.getType()) ? NumberFormat.getNumberInstance(locale) : NumberFormat.getIntegerInstance(locale)); if (decimals > -1) { numberFormat.setMinimumFractionDigits(decimals); numberFormat.setMaximumFractionDigits(decimals); } if (disableGrouping) { numberFormat.setGroupingUsed(false); } return postProcessField(input.number(type).numberFormat(numberFormat).asField(), property); }
/** * Renders a Temporal value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderTemporal(Property<? extends T> property) { TemporalInputBuilder builder = null; if (LocalDate.class.isAssignableFrom(property.getType())) { builder = input.localDate(false); } else if (LocalDateTime.class.isAssignableFrom(property.getType())) { builder = input.localDateTime(false); } else { throw new UnsupportedTemporalTypeException( "Temporal type " + property.getType().getName() + " is not supported by default field renderer"); } final TemporalInputBuilder<Temporal, ?> b = builder; // set locale from LocalizationContext, if any LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap((c) -> c.getLocale()) .ifPresent((l) -> b.locale(l)); return postProcessField(b.asField(), property); }
Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(getLocale()); if (locale == null) {
Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(getLocale()); if (locale == null) {
public void localization() { // tag::localization[] LocalizationContext ctx = LocalizationContext.builder() .withMessageProvider(MessageProvider.fromProperties("messages").build()).withInitialLocale(Locale.US) .build(); ctx.getLocale().ifPresent(l -> System.out.println(l)); // <1> String localizedMessage = ctx.getMessage("test.message", "defaultMessage"); // <2> localizedMessage = ctx .getMessage(Localizable.builder().message("defaultMessage").messageCode("test.message").build()); // <3> ctx.format(2.56); // <4> ctx.format(0.5, NumberFormatFeature.PERCENT_STYLE); // <5> ctx.format(5600.678, 2); // <6> NumberFormat nf = ctx.getNumberFormat(Integer.class); // <7> ctx.format(new Date(), TemporalType.DATE); // <8> ctx.format(new Date(), TemporalType.DATE_TIME, TemporalFormat.LONG, TemporalFormat.LONG); // <9> ctx.format(LocalDate.of(2017, Month.MARCH, 15)); // <10> ctx.format(LocalDateTime.of(2017, Month.MARCH, 15, 16, 48), TemporalFormat.FULL, TemporalFormat.SHORT); // <11> DateFormat df = ctx.getDateFormat(TemporalType.DATE); // <12> DateTimeFormatter dtf = ctx.getDateTimeFormatter(TemporalType.DATE_TIME); // <13> // end::localization[] }