/** * Convert a {@link Date} value * @param value Value to convert * @param type Temporal type * @return String value */ private static String convertDate(Date value, TemporalType type) { if (value != null) { return LocalizationContext.getCurrent().filter(l -> l.isLocalized()).map((c) -> c.format(value, type)) .orElse(DateFormat.getDateInstance().format(value)); } return null; }
/** * 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); }
/** * Convert a {@link Temporal} value * @param value Value to convert * @return String value */ private static String convertTemporal(Temporal value) { if (value != null) { return LocalizationContext.getCurrent().filter(l -> l.isLocalized()).map((c) -> c.format(value)) .orElse(convertTemporalWithDefaultLocale(value)); } return null; }
/** * 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); }
/** * Requires the current {@link LocalizationContext}, checking that it is localized. If not available using * {@link #getCurrent()}, an {@link IllegalStateException} is thrown. If not localized, a * {@link LocalizationException} is thrown instead. * @return Current LocalizationContext * @throws IllegalStateException LocalizationContext is not available as a {@link Context} resource * @throws LocalizationException A LocalizationContext is available as context resource but it is not localized * @see #isLocalized() */ static LocalizationContext requireLocalized() { LocalizationContext localizationContext = require(); if (!localizationContext.isLocalized()) { throw new LocalizationException("LocalizationContext is not localized"); } return localizationContext; }
@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; }
if (locale == null) { locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(null);
/** * Convert a {@link Number} value * @param value Value to convert * @param decimals Decimal positions (-1 for default) * @param features Number format features * @return String value */ private static String convertNumber(Number value, int decimals, NumberFormatFeature[] features) { if (value != null) { return LocalizationContext.getCurrent().filter(l -> l.isLocalized()) .map((c) -> c.format(value, decimals, features)) .orElse(getNumberFormatForDefaultLocale(value, NumberFormatFeature.hasFeature(NumberFormatFeature.DISABLE_GROUPING, features), NumberFormatFeature.hasFeature(NumberFormatFeature.HIDE_DECIMALS_WHEN_ALL_ZERO, features)) .format(value)); } return null; }
/** * 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; }
public void localize() { // tag::localize[] LocalizationContext localizationContext = LocalizationContext.getCurrent() .orElseThrow(() -> new IllegalStateException("Missing LocalizationContext")); // <1> localizationContext.localize(Locale.US); // <2> boolean localized = localizationContext.isLocalized(); // <3> localizationContext.localize(Localization.builder(Locale.JAPAN).defaultDecimalPositions(2) .defaultDateTemporalFormat(TemporalFormat.FULL).build()); // <4> // end::localize[] }
/** * 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); }
/** * Try to translate given <code>localizable</code> using the {@link LocalizationContext} available as * {@link Context} resource through {@link LocalizationContext#getCurrent()}. The LocalizationContext must be * localized. * @param localizable Localizable to translate * @param lenient if <code>true</code> and a {@link LocalizationContext} is not available or it is not localized, * silently return default {@link Localizable#getMessage()}. If <code>false</code>, a * {@link LocalizationException} is thrown for that situation * @return The localized message, or default {@link Localizable#getMessage()} if a localized LocalizationContext is * not available and lenient is <code>true</code>, or {@link Localizable#getMessageCode()} is * <code>null</code>. */ static String translate(final Localizable localizable, final boolean lenient) { ObjectUtils.argumentNotNull(localizable, "Localizable must be not null"); Optional<LocalizationContext> lc = LocalizationContext.getCurrent().filter(l -> l.isLocalized()); if (!lc.isPresent() && !lenient) { throw new LocalizationException( "A LocalizationContext is not available from context or it is not localized"); } return lc.map(l -> l.getMessage(localizable, lenient)) .orElse(FormatUtils.resolveMessageArguments(MessageProvider.DEFAULT_MESSAGE_ARGUMENT_PLACEHOLDER, localizable.getMessage(), localizable.getMessageArguments())); }
/** * 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) {