/** * Get a {@link NumberFormat} for numbers formatting and set it up according to context {@link Locale} and * {@link Localization} settings. * @param numberType Number type * @return NumberFormat * @throws LocalizationException If context is not localized */ default NumberFormat getNumberFormat(Class<? extends Number> numberType) { return getNumberFormat(numberType, -1, false); }
/** * 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); }
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[] }