/** * Get the {@link LocalizationContext} context to use. * @return The configured {@link LocalizationContext} or the context {@link LocalizationContext}, if available */ protected Optional<LocalizationContext> getLocalizationContext() { if (localizationContext != null) { return Optional.of(localizationContext); } return LocalizationContext.getCurrent(); }
/** * Requires the current {@link LocalizationContext}. If not available using {@link #getCurrent()}, an * {@link IllegalStateException} is thrown. * @return Current LocalizationContext * @throws IllegalStateException LocalizationContext is not available as a {@link Context} resource */ static LocalizationContext require() { return getCurrent().orElseThrow( () -> new IllegalStateException("LocalizationContext is not available as context resource")); }
/** * Convert a {@link Boolean} value * @param value Value to convert * @return String value */ private static String convertBoolean(Boolean value) { return LocalizationContext.getCurrent().flatMap(u -> u.getDefaultBooleanLocalization(value)) .map(m -> LocalizationContext.translate(m, true)).orElse(String.valueOf(value)); }
/** * 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); }
/** * 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; }
/** * 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; }
if (locale == null) { locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(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 sessionscope() { // tag::sessionscope[] VaadinSession.getCurrent().setAttribute(LocalizationContext.CONTEXT_KEY, LocalizationContext.builder().withInitialLocale(Locale.US).build()); // <1> LocalizationContext.getCurrent().ifPresent(localizationContext -> { // <2> // LocalizationContext obtained from current Vaadin session }); // end::sessionscope[] }
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[] }
public void sessionscope() { // tag::sessionscope[] VaadinSession.getCurrent().setAttribute(LocalizationContext.CONTEXT_KEY, LocalizationContext.builder().withInitialLocale(Locale.US).build()); // <1> LocalizationContext.getCurrent().ifPresent(localizationContext -> { // <2> // LocalizationContext obtained from current Vaadin session }); // end::sessionscope[] }
/** * 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); }
/** * 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); }
.getCurrent().flatMap(l -> l.asMessageResolver().getMessage(locale, localizable.getMessageCode(), localizable.getMessageArguments())) .orElse(localizable.getMessage()));
Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(getLocale()); if (locale == null) {
public void scope2() { // tag::scope2[] VaadinSession.getCurrent().setAttribute(LocalizationContext.class, // <1> LocalizationContext.builder().withInitialLocale(Locale.US).build()); Optional<LocalizationContext> localizationContext = Context.get().resource(LocalizationContext.class); // <2> localizationContext = LocalizationContext.getCurrent(); // <3> // end::scope2[] }