/** * Format given {@link LocalTime} according to current Context {@link Locale} using given time format style. * @param temporal Time to format (may be null) * @param timeFormat Time format style * @return Formatted temporal, or <code>null</code> if given temporal was null * @throws LocalizationException If context is not localized */ default String format(LocalTime temporal, TemporalFormat timeFormat) { return format(temporal, TemporalFormat.DEFAULT, timeFormat); }
/** * Format given {@link LocalDate} according to current Context {@link Locale} using given date format style. * @param temporal Date to format (may be null) * @param dateFormat Date format style * @return Formatted temporal, or <code>null</code> if given temporal was null * @throws LocalizationException If context is not localized */ default String format(LocalDate temporal, TemporalFormat dateFormat) { return format(temporal, dateFormat, TemporalFormat.DEFAULT); }
/** * Format given <code>date</code> according to current Context {@link Locale} using default format styles. * @param date Date to format (may be null) * @param type Date temporal type (Only a date, only a time or a date and time) * @return Formatted date, or <code>null</code> if given date was null * @throws LocalizationException If context is not localized */ default String format(Date date, TemporalType type) { return format(date, type, TemporalFormat.DEFAULT, TemporalFormat.DEFAULT); }
/** * Format given <code>number</code> according to current Context {@link Locale}. * @param number Number to format (may be null) * @param features Optional format features * @return Formatted number, or <code>null</code> if given Number was null * @throws LocalizationException If context is not localized */ default String format(Number number, NumberFormatFeature... features) { return format(number, -1, features); }
/** * Format given <code>temporal</code> (for example a {@link LocalDate}, a {@link LocalTime} or a * {@link LocalDateTime}) according to current Context {@link Locale} using default format styles. * @param temporal Temporal to format (may be null) * @return Formatted temporal, or <code>null</code> if given temporal was null * @throws LocalizationException If context is not localized */ default String format(Temporal temporal) { return format(temporal, TemporalFormat.DEFAULT, TemporalFormat.DEFAULT); }
/** * 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; }
/** * 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; }
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[] }
/** * 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; }