Interface MessageFormatter
- All Known Implementing Classes:
MessageFormatter.MessageFormatterI18n,MessageFormatter.MessageFormatterMessageFormat,MessageFormatter.MessageFormatterStringFormat
MessageFormatter interface provides methods to format messages
using different strategies. This can be achieved through standard formatting,
MessageFormat formatting, or localized formatting using an I18N instance.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumDefines the style of formatting to be used within theMessageFormatter.static final classA utility class that provides helper methods for formatting messages in various styles.static final recordRepresents a container for a format string and the corresponding arguments to be used in formatting operations.static final recordTheI18NFormatterclass is an implementation of theMessageFormatterinterface that formats messages using anI18Ninstance.static final classThe MessageFormatterMessageFormat class is an implementation of the MessageFormatter interface that formats messages using theMessageFormat.format(String, Object...)method.static final recordTheMessageFormatterStringFormatclass is an implementation of theMessageFormatterinterface that uses theString.format(Locale, String, Object...)method for formatting messages. -
Method Summary
Modifier and TypeMethodDescriptionCreates aMessageFormatterArgsinstance using the provided format string and arguments.empty()Creates an emptyMessageFormatterArgsinstance with no format string and no arguments.default StringFormats the given format string by replacing placeholders with the provided arguments.Formats the given format string by replacing placeholders with the provided arguments.Retrieves the formatting style currently in use by theMessageFormatter.static MessageFormatteri18n()Creates aMessageFormatterconfigured with the defaultI18Ninstance, which allows for internationalized message formatting.static MessageFormatterCreates aMessageFormatterconfigured with the specifiedI18Ninstance, which allows for internationalized message formatting.Creates aMessageFormatterArgsinstance where the given object's string value is output unchanged.static MessageFormatterCreates an instance ofMessageFormatterthat usesString.format(Locale, String, Object...)for formatting using the provided locale.static MessageFormatterReturns the instance ofMessageFormatterthat utilizesMessageFormatfor formatting messages.Creates aMessageFormatterArgsinstance using the given format string and arguments without applying any message lookup/internationalization (i18n).static MessageFormatterstandard()Returns an instance ofMessageFormatterthat usesString.format(Locale, String, Object...)for formatting, usingLocale.getDefault()as locale.default StringReturns the input string as is, without any formatting applied.
-
Method Details
-
args
Creates aMessageFormatterArgsinstance using the provided format string and arguments.- Parameters:
fmt- the format string containing placeholders to be replacedargs- the arguments to use for replacing the placeholders in the format string- Returns:
- a
MessageFormatterArgsinstance containing the format string and the provided arguments
-
literal
Creates aMessageFormatterArgsinstance where the given object's string value is output unchanged.This method is useful for cases where the string representation of an object should be used directly without any formatting or translation. The string value is determined by calling
String.valueOf(Object).- Parameters:
obj- the object whose string value should be output unchanged- Returns:
- a
MessageFormatterArgsinstance containing the object's literal string value
-
nonI18N
Creates aMessageFormatterArgsinstance using the given format string and arguments without applying any message lookup/internationalization (i18n).Implementation note: The provided format string will be prefixed with a null character ('\0') to indicate that the string should not be localized.
- Parameters:
fmt- the format string to be usedargs- the arguments to replace placeholders in the format string (optional, may benull)- Returns:
- a
MessageFormatterArgsinstance containing the prefixed format string and the provided arguments
-
empty
Creates an emptyMessageFormatterArgsinstance with no format string and no arguments.- Returns:
- an empty
MessageFormatterArgsinstance
-
format
Formats the given format string by replacing placeholders with the provided arguments.NOTE: The formatting process is implementation defined.
- Parameters:
fmt- the format string containing placeholders to be replacedargs- the arguments to replace the placeholders in the format string- Returns:
- the formatted string where placeholders are replaced with the corresponding arguments
-
format
Formats the given format string by replacing placeholders with the provided arguments.NOTE: The formatting process is implementation defined.
- Parameters:
args- an instance ofMessageFormatter.MessageFormatterArgsholding the format string and arguments- Returns:
- the formatted string where placeholders are replaced with the corresponding arguments
-
text
Returns the input string as is, without any formatting applied.- Parameters:
s- the input string to be returned- Returns:
- the same input string provided
-
getFormatStyle
MessageFormatter.FormatStyle getFormatStyle()Retrieves the formatting style currently in use by theMessageFormatter.The formatting style determines the approach taken for formatting messages or processing text input, see
MessageFormatter.FormatStylefor details.- Returns:
- the current
MessageFormatter.FormatStyleused for formatting
-
standard
Returns an instance ofMessageFormatterthat usesString.format(Locale, String, Object...)for formatting, usingLocale.getDefault()as locale.- Returns:
- the default
MessageFormatterinstance using standard formatting and the default locale
-
localized
Creates an instance ofMessageFormatterthat usesString.format(Locale, String, Object...)for formatting using the provided locale.- Parameters:
locale- theLocaleto use when formatting- Returns:
- a
MessageFormatterinstance using standard formatting and the provided locale
-
messageFormat
Returns the instance ofMessageFormatterthat utilizesMessageFormatfor formatting messages. This ensures a structured and localized message formatting process, where placeholders within a string are substituted with the provided arguments.- Returns:
- the singleton instance of
MessageFormatterMessageFormatused for formatting messages.
-
i18n
Creates aMessageFormatterconfigured with the specifiedI18Ninstance, which allows for internationalized message formatting.This implementation will look up texts using the supplied
I18Ninstance before formatting.- Parameters:
i18n- theI18Ninstance providing the localization rules and resources to be used for formatting messages- Returns:
- a
MessageFormatterconfigured to use the providedI18Ninstance
-
i18n
Creates aMessageFormatterconfigured with the defaultI18Ninstance, which allows for internationalized message formatting.This implementation will look up texts using the supplied
I18Ninstance before formatting.- Returns:
- a
MessageFormatterconfigured to use the providedI18Ninstance
-