@Override public Builder withInitialSystemLocale() { context.setLocalization(Localization.builder(Locale.getDefault()).build(), false); return this; }
/** * Get message value using MessageProvider for given code. Parent localizations are used as fallback, if any. * @param locale Locale for which to obtain the message localization * @param provider MessageProvider * @param code Message code * @return Optional message value */ protected Optional<String> getMessageFromProvider(Locale locale, MessageProvider provider, String code) { Optional<String> value = provider.getMessage(locale, code); if (!value.isPresent()) { // check parent Optional<Localization> parent = getLocalization().getParent(); while (parent.isPresent()) { value = provider.getMessage(parent.get().getLocale(), code); if (value.isPresent()) { return value; } parent = parent.get().getParent(); } } return value; }
df = lzn.getDefaultDateTemporalFormat().orElse(getDefaultDateFormatStyle().orElse(TemporalFormat.SHORT)); tf = lzn.getDefaultTimeTemporalFormat().orElse(getDefaultTimeFormatStyle().orElse(TemporalFormat.SHORT));
@Override public Optional<Locale> getLocale() { return Optional.ofNullable(isLocalized() ? getLocalization().getLocale() : null); }
@Override public NumberFormat getNumberFormat(Class<? extends Number> numberType, int decimalPositions, boolean disableGrouping) { int decimals = decimalPositions; if (decimals < 0) { // use default, if any if (getLocalization() != null) { decimals = getLocalization().getDefaultDecimalPositions().orElse(-1); } } NumberFormat format = null; if (TypeUtils.isDecimalNumber(numberType)) { format = NumberFormat.getInstance(checkLocalized()); if (decimals > -1) { format.setMinimumFractionDigits(decimals); format.setMaximumFractionDigits(decimals); } } else { format = NumberFormat.getIntegerInstance(checkLocalized()); } if (disableGrouping) { format.setGroupingUsed(false); } return format; }
df = lzn.getDefaultDateTemporalFormat().orElse(getDefaultDateFormatStyle().orElse(TemporalFormat.SHORT)); tf = lzn.getDefaultTimeTemporalFormat().orElse(getDefaultTimeFormatStyle().orElse(TemporalFormat.SHORT));
@Override public void localize(Localization localization, boolean fireEvent) { clearCaches(); if (localization != null && localization.getLocale() == null) { throw new LocalizationException("Invalid Localization: missing Locale"); } setLocalization(localization, fireEvent); }
decimals = getLocalization().getDefaultDecimalPositions().orElse(-1);
@Override public Builder withInitialLocale(Locale locale) { context.setLocalization(Localization.builder(locale).build(), false); return this; }
/** * Initialize context with given {@link Locale}, using Locale's default settings for numbers and dates formats and * symbols. * @param locale The new {@link Locale} to set, may be <code>null</code> * @param fireEvent Whether to fire the localization change event */ default void localize(Locale locale, boolean fireEvent) { localize((locale != null) ? Localization.builder(locale).build() : null, fireEvent); }
/** * Constructor with Locale initialization * @param locale Initial Locale */ public DefaultLocalizationContext(Locale locale) { super(); if (locale != null) { setLocalization(Localization.builder(locale).build(), false); } }
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[] }