Skip to content

Support resolving messages with named arguments in MessageSource (java.util.Map)Β #36147

@ooraini

Description

@ooraini

Overview

The MessageSource interface only supports positional arguments in messages. Alternative implementation with Map style argument have to create a parallel interface and implementation classes to support named arguments. Most i18n libraries I worked with in other ecosystem(non-Java) support named arguments. Unicode's new Message Format 2.0 doesn't event support positional arguments.

Proposal

Add additional methods to support named arguments in messages. The default implementation should throw UnsupportedOperationException. The documentation for MessageSource should indicate that an implementation could support either/both positional/named argument messages. Implementations will throw UnsupportedOperationException if they don't support it.

Additionally, offer a path to extend the existing MessageSource implementations with named argument support.

(Idea) Introduce an additional interface NamedMessageSoruce and make MessageSoruce extend it. All existing concrete classes should offer a way of delegating to NamedMessageSoruce. That way, you can support both positional(through the existing implementation) and named through delegation. Boot can later offer support for wiring such object.

Imaginary code:

@Bean
NamedMessageSoruceFactory namdMessageSoruceFactory() { 
  // custom impl here
}

// boot autowires NamedMessageSoruce into its MessageSource implementation to delegate the new methods

// application code
messageSoruce.getMessage("my_message", Map.of("date", ...), Locale.EN);

// messages.properties
my_message={$date :styel=short}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions