Interface MessageFormatter

All Known Implementing Classes:
MessageFormatter.MessageFormatterI18n, MessageFormatter.MessageFormatterMessageFormat, MessageFormatter.MessageFormatterStringFormat

public interface MessageFormatter
The 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.
  • Method Details

    • args

      Creates a MessageFormatterArgs instance using the provided format string and arguments.
      Parameters:
      fmt - the format string containing placeholders to be replaced
      args - the arguments to use for replacing the placeholders in the format string
      Returns:
      a MessageFormatterArgs instance containing the format string and the provided arguments
    • literal

      static MessageFormatter.MessageFormatterArgs literal(@Nullable Object obj)
      Creates a MessageFormatterArgs instance 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 MessageFormatterArgs instance containing the object's literal string value
    • nonI18N

      static MessageFormatter.MessageFormatterArgs nonI18N(String fmt, @Nullable Object... args)
      Creates a MessageFormatterArgs instance 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 used
      args - the arguments to replace placeholders in the format string (optional, may be null)
      Returns:
      a MessageFormatterArgs instance containing the prefixed format string and the provided arguments
    • empty

      Creates an empty MessageFormatterArgs instance with no format string and no arguments.
      Returns:
      an empty MessageFormatterArgs instance
    • format

      String format(String fmt, @Nullable Object... args)
      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 replaced
      args - 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 of MessageFormatter.MessageFormatterArgs holding the format string and arguments
      Returns:
      the formatted string where placeholders are replaced with the corresponding arguments
    • text

      default String text(String s)
      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

      Retrieves the formatting style currently in use by the MessageFormatter.

      The formatting style determines the approach taken for formatting messages or processing text input, see MessageFormatter.FormatStyle for details.

      Returns:
      the current MessageFormatter.FormatStyle used for formatting
    • standard

      static MessageFormatter standard()
      Returns an instance of MessageFormatter that uses String.format(Locale, String, Object...) for formatting, using Locale.getDefault() as locale.
      Returns:
      the default MessageFormatter instance using standard formatting and the default locale
    • localized

      static MessageFormatter localized(Locale locale)
      Creates an instance of MessageFormatter that uses String.format(Locale, String, Object...) for formatting using the provided locale.
      Parameters:
      locale - the Locale to use when formatting
      Returns:
      a MessageFormatter instance using standard formatting and the provided locale
    • messageFormat

      static MessageFormatter messageFormat()
      Returns the instance of MessageFormatter that utilizes MessageFormat for 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 MessageFormatterMessageFormat used for formatting messages.
    • i18n

      static MessageFormatter i18n(I18N i18n)
      Creates a MessageFormatter configured with the specified I18N instance, which allows for internationalized message formatting.

      This implementation will look up texts using the supplied I18N instance before formatting.

      Parameters:
      i18n - the I18N instance providing the localization rules and resources to be used for formatting messages
      Returns:
      a MessageFormatter configured to use the provided I18N instance
    • i18n

      static MessageFormatter i18n()
      Creates a MessageFormatter configured with the default I18N instance, which allows for internationalized message formatting.

      This implementation will look up texts using the supplied I18N instance before formatting.

      Returns:
      a MessageFormatter configured to use the provided I18N instance