public interface LocalizationContext
A LocalizationContext must be localized, i.e. initialized, using localize(Localization)
or
localize(Locale)
before using it.
Provides methods to localize messages, numbers and temporal data. For messages localization, it relies on
MessageProvider
s, which must be registered using LocalizationContext.Builder.messageProvider(MessageProvider)
at context
build time.
MessageProvider
Modifier and Type | Interface and Description |
---|---|
static interface |
LocalizationContext.Builder
Builder for
LocalizationContext creation |
static interface |
LocalizationContext.MissingMessageLocalizationListener
A listener which can be registered to a
LocalizationContext to be notified when a message translation is
missing from any of the available MessageProvider s. |
Modifier and Type | Field and Description |
---|---|
static String |
CONTEXT_KEY
Default
Context resource key |
Modifier and Type | Method and Description |
---|---|
static LocalizationContext.Builder |
builder()
Builder to create LocalizationContext instances
|
default String |
format(Date date,
TemporalType type)
Format given
date according to current Context Locale using default format styles. |
String |
format(Date date,
TemporalType type,
TemporalFormat dateFormat,
TemporalFormat timeFormat)
Format given
date according to current Context Locale . |
default String |
format(LocalDate temporal,
TemporalFormat dateFormat)
|
default String |
format(LocalTime temporal,
TemporalFormat timeFormat)
|
String |
format(Number number,
int decimalPositions,
NumberFormatFeature... features)
Format given
number according to current Context Locale . |
default String |
format(Number number,
NumberFormatFeature... features)
Format given
number according to current Context Locale . |
default String |
format(Temporal temporal)
Format given
temporal (for example a LocalDate , a LocalTime or a
LocalDateTime ) according to current Context Locale using default format styles. |
String |
format(Temporal temporal,
TemporalFormat dateFormat,
TemporalFormat timeFormat)
Format given
temporal (for example a LocalDate , a LocalTime or a
LocalDateTime ) according to current Context Locale . |
static Optional<LocalizationContext> |
getCurrent()
Convenience method to obtain the current
LocalizationContext made available as Context resource,
using default ClassLoader . |
default DateFormat |
getDateFormat(TemporalType type)
Get a
DateFormat for Date formatting and set it up according to context Locale and
Localization settings, using default format styles. |
DateFormat |
getDateFormat(TemporalType type,
TemporalFormat dateFormat,
TemporalFormat timeFormat)
Get a
DateFormat for Date formatting and set it up according to context Locale and
Localization settings. |
default DateTimeFormatter |
getDateTimeFormatter(TemporalType type)
Get a
DateTimeFormatter for javax.time.* types formatting and set it up according to context
Locale and Localization settings, using default format styles. |
DateTimeFormatter |
getDateTimeFormatter(TemporalType type,
TemporalFormat dateFormat,
TemporalFormat timeFormat)
Get a
DateTimeFormatter for javax.time.* types formatting and set it up according to context
Locale and Localization settings. |
Optional<Localizable> |
getDefaultBooleanLocalization(boolean value)
Gets the default
Localizable message to use to localize a boolean value |
Optional<Locale> |
getLocale()
Get Locale currently bound to this context, if any
|
default String |
getMessage(Localizable localizable)
Get the localized message for given
Localizable . |
String |
getMessage(Localizable localizable,
boolean lenient)
Get a possibly localized message if the
LocalizationContext is localized. |
String |
getMessage(String code,
String defaultMessage,
Object... arguments)
Get a message for given
code localized according to current Locale to which context is bound |
Optional<String> |
getMessageArgumentsPlaceholder()
Get the symbol to be used as message arguments placeholder.
|
default NumberFormat |
getNumberFormat(Class<? extends Number> numberType)
Get a
NumberFormat for numbers formatting and set it up according to context Locale and
Localization settings. |
NumberFormat |
getNumberFormat(Class<? extends Number> numberType,
int decimalPositions,
boolean disableGrouping)
Get a
NumberFormat for numbers formatting and set it up according to context Locale and
Localization settings. |
boolean |
isLocalized()
Returns whether this context is localized, i.e. it was initialized with
Localization informations
and ready to be used. |
default void |
localize(Locale locale)
Initialize context with given
Locale , using Locale's default settings for numbers and dates formats and
symbols. |
void |
localize(Localization localization)
Initialize context with given
Localization informations |
static LocalizationContext |
require()
Requires the current
LocalizationContext . |
static LocalizationContext |
requireLocalized()
Requires the current
LocalizationContext , checking that it is localized. |
void |
setDefaultBooleanLocalization(boolean value,
Localizable message)
Sets the default
Localizable message to use to localize a boolean value |
void |
setDefaultDateFormatStyle(TemporalFormat format)
Set the default
TemporalFormat style to use to format dates. |
void |
setDefaultTimeFormatStyle(TemporalFormat format)
Set the default
TemporalFormat style to use to format times. |
static String |
translate(Localizable localizable)
Try to translate given
localizable using the LocalizationContext available as
Context resource through getCurrent() . |
static String |
translate(Localizable localizable,
boolean lenient)
Try to translate given
localizable using the LocalizationContext available as
Context resource through getCurrent() . |
static String |
translate(String messageCode,
String defaultMessage)
Try to translate given
messageCode using the LocalizationContext available as
Context resource through getCurrent() . |
static String |
translate(String messageCode,
String defaultMessage,
boolean lenient)
Try to translate given
messageCode using the LocalizationContext available as
Context resource through getCurrent() . |
boolean isLocalized()
Localization
informations
and ready to be used.true
if context is localizeddefault void localize(Locale locale)
Locale
, using Locale's default settings for numbers and dates formats and
symbols.locale
- Localevoid localize(Localization localization)
Localization
informationslocalization
- LocalizationOptional<Locale> getLocale()
Optional<String> getMessageArgumentsPlaceholder()
String getMessage(String code, String defaultMessage, Object... arguments)
code
localized according to current Locale to which context is boundcode
- Message code (not null)defaultMessage
- Default message to return if no localized message could be foundarguments
- Optional translation argumentsdefaultMessage
if no suitable message was foundLocalizable.LocalizationException
- If context is not localizeddefault String getMessage(Localizable localizable)
Localizable
.localizable
- Localizable messageLocalizable.getMessage()
if Localizable.getMessageCode()
is
nullLocalizable.LocalizationException
- If context is not localizedString getMessage(Localizable localizable, boolean lenient)
LocalizationContext
is localized. If not and lenient
is set to true
, default Localizable.getMessage()
is returned.localizable
- Localizable messagelenient
- if true
and the LocalizationContext
is not localized, default
Localizable.getMessage()
is returned, otherwise a Localizable.LocalizationException
is thrownLocalizable.LocalizationException
- If not lenient
and context is not localizedvoid setDefaultBooleanLocalization(boolean value, Localizable message)
Localizable
message to use to localize a boolean valuevalue
- Boolean valuemessage
- The localizable message to use to localize given boolean valueOptional<Localizable> getDefaultBooleanLocalization(boolean value)
Localizable
message to use to localize a boolean valuevalue
- Boolean value to localizevoid setDefaultDateFormatStyle(TemporalFormat format)
TemporalFormat
style to use to format dates.
This is overriden by any Localization.getDefaultDateTemporalFormat()
value.
format
- Defalt date format stylevoid setDefaultTimeFormatStyle(TemporalFormat format)
TemporalFormat
style to use to format times.
This is overriden by any Localization.getDefaultTimeTemporalFormat()
value.
format
- Defalt time format styleString format(Number number, int decimalPositions, NumberFormatFeature... features)
number
according to current Context Locale
.number
- Number to format (may be null)decimalPositions
- Decimal positions to use (-1
for default, i.e. display all the decimal
positions of the given Number, if any)features
- Optional format featuresnull
if given Number was nullLocalizable.LocalizationException
- If context is not localizeddefault String format(Number number, NumberFormatFeature... features)
number
according to current Context Locale
.number
- Number to format (may be null)features
- Optional format featuresnull
if given Number was nullLocalizable.LocalizationException
- If context is not localizedNumberFormat getNumberFormat(Class<? extends Number> numberType, int decimalPositions, boolean disableGrouping)
NumberFormat
for numbers formatting and set it up according to context Locale
and
Localization
settings.numberType
- Number typedecimalPositions
- Decimal positions to use with decimal number types (-1
for default, i.e.
display all the decimal positions, if any)disableGrouping
- true
to not use grouping symbolLocalizable.LocalizationException
- If context is not localizeddefault NumberFormat getNumberFormat(Class<? extends Number> numberType)
NumberFormat
for numbers formatting and set it up according to context Locale
and
Localization
settings.numberType
- Number typeLocalizable.LocalizationException
- If context is not localizedString format(Date date, TemporalType type, TemporalFormat dateFormat, TemporalFormat timeFormat)
date
according to current Context Locale
.date
- Date to format (may be null)type
- Date temporal type (Only a date, only a time or a date and time)dateFormat
- Date format style (null for default)timeFormat
- Time format style (null for default)null
if given date was nullLocalizable.LocalizationException
- If context is not localizeddefault String format(Date date, TemporalType type)
date
according to current Context Locale
using default format styles.date
- Date to format (may be null)type
- Date temporal type (Only a date, only a time or a date and time)null
if given date was nullLocalizable.LocalizationException
- If context is not localizedDateFormat getDateFormat(TemporalType type, TemporalFormat dateFormat, TemporalFormat timeFormat)
DateFormat
for Date
formatting and set it up according to context Locale
and
Localization
settings.type
- Temporal typedateFormat
- Date format style, null
for defaulttimeFormat
- Time format style, null
for defaultLocalizable.LocalizationException
- If context is not localizeddefault DateFormat getDateFormat(TemporalType type)
DateFormat
for Date
formatting and set it up according to context Locale
and
Localization
settings, using default format styles.type
- Temporal typeLocalizable.LocalizationException
- If context is not localizedString format(Temporal temporal, TemporalFormat dateFormat, TemporalFormat timeFormat)
temporal
(for example a LocalDate
, a LocalTime
or a
LocalDateTime
) according to current Context Locale
.temporal
- Temporal to format (may be null)dateFormat
- Date format style (null for default)timeFormat
- Time format style (null for default)null
if given temporal was nullLocalizable.LocalizationException
- If context is not localizeddefault String format(Temporal temporal)
temporal
(for example a LocalDate
, a LocalTime
or a
LocalDateTime
) according to current Context Locale
using default format styles.temporal
- Temporal to format (may be null)null
if given temporal was nullLocalizable.LocalizationException
- If context is not localizeddefault String format(LocalDate temporal, TemporalFormat dateFormat)
temporal
- Date to format (may be null)dateFormat
- Date format stylenull
if given temporal was nullLocalizable.LocalizationException
- If context is not localizeddefault String format(LocalTime temporal, TemporalFormat timeFormat)
temporal
- Time to format (may be null)timeFormat
- Time format stylenull
if given temporal was nullLocalizable.LocalizationException
- If context is not localizedDateTimeFormatter getDateTimeFormatter(TemporalType type, TemporalFormat dateFormat, TemporalFormat timeFormat)
DateTimeFormatter
for javax.time.* types formatting and set it up according to context
Locale
and Localization
settings.type
- Temporal typedateFormat
- Date format style, null
for defaulttimeFormat
- Time format style, null
for defaultLocalizable.LocalizationException
- If context is not localizeddefault DateTimeFormatter getDateTimeFormatter(TemporalType type)
DateTimeFormatter
for javax.time.* types formatting and set it up according to context
Locale
and Localization
settings, using default format styles.type
- Temporal typeLocalizable.LocalizationException
- If context is not localizedstatic String translate(Localizable localizable, boolean lenient)
localizable
using the LocalizationContext
available as
Context
resource through getCurrent()
. The LocalizationContext must be
localized.localizable
- Localizable to translatelenient
- if true
and a LocalizationContext
is not available or it is not localized,
silently return default Localizable.getMessage()
. If false
, a
Localizable.LocalizationException
is thrown for that situationLocalizable.getMessage()
if a localized LocalizationContext is
not available and lenient is true
, or Localizable.getMessageCode()
is
null
.static String translate(Localizable localizable)
localizable
using the LocalizationContext
available as
Context
resource through getCurrent()
. The LocalizationContext must be
localized.localizable
- Localizable to translateLocalizable.getMessage()
if
Localizable.getMessageCode()
is null
Localizable.LocalizationException
- If a LocalizationContext
is not available as Context
resource or it
is not localizedstatic String translate(String messageCode, String defaultMessage, boolean lenient)
messageCode
using the LocalizationContext
available as
Context
resource through getCurrent()
. The LocalizationContext must be
localized.messageCode
- Message code to translatedefaultMessage
- Default messagelenient
- if true
and a LocalizationContext
is not available or it is not localized,
silently return default defaultMessage
. If false
, a
Localizable.LocalizationException
is thrown for that situationdefaultMessage
if a localized LocalizationContext is not
available and lenient is true
, or messageCode
is null
.static String translate(String messageCode, String defaultMessage)
messageCode
using the LocalizationContext
available as
Context
resource through getCurrent()
. The LocalizationContext must be
localized.messageCode
- Message code to translatedefaultMessage
- Default messagedefaultMessage
if messageCode
is
null
Localizable.LocalizationException
- If a LocalizationContext
is not available as Context
resource or it
is not localizedstatic Optional<LocalizationContext> getCurrent()
LocalizationContext
made available as Context
resource,
using default ClassLoader
.
See Context.resource(String, Class)
for details about context resources availability conditions.
static LocalizationContext require()
LocalizationContext
. If not available using getCurrent()
, an
IllegalStateException
is thrown.IllegalStateException
- LocalizationContext is not available as a Context
resourcestatic LocalizationContext requireLocalized()
LocalizationContext
, checking that it is localized. If not available using
getCurrent()
, an IllegalStateException
is thrown. If not localized, a
Localizable.LocalizationException
is thrown instead.IllegalStateException
- LocalizationContext is not available as a Context
resourceLocalizable.LocalizationException
- A LocalizationContext is available as context resource but it is not localizedisLocalized()
static LocalizationContext.Builder builder()
Copyright © 2018 The Holon Platform. All rights reserved.