Parameter Substitution In Resource Bundles

Tower Of Babel

If you use Java resource bundles to add multi language support to your application you sometimes need to add parameter to you properties but resource bundles from the JDK itself does not support parameter substitution.

messages.properties:

...
itemservice.item.added = User {0} added new item {1} to group {2}
...

messages_de.properties (German):

...
itemservice.item.added = Das Objekt {1} wurde von Benutzer {0} zur Gruppe {2} hinzugef\u00FCgt.
...

As you can see parameter order differs in German property file. That’s why its hard to realize this example without parameters. To replace parameters by values, you have to use method java.text.MessageFormat.format(String text, Object … arguments). Add a small wrapper class to you project:

public class Messages {	
    // property file is: package/name/messages.properties
    private static final String BUNDLE_NAME = "package.name.messages";
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
    private Messages() {
    }
    public static String getString(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
    public static String getString(String key, Object... params  ) {
        try {
            return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

You can now use the properties from the example above:

Messages.getString( "itemservice.item.added", user.getName(), item.getTitle(), group.getName());
    • Chaibi
    • 6. Juni 2011

    Thanks, it was useful for me.

    • Damian
    • 26. September 2011

    very useful! great article. Regards

    • Carlos
    • 2. Dezember 2011

    Thanks a lot!

    • Cathy
    • 17. Juli 2012

    Thank you. This is just what I needed.

    • Volodia
    • 15. Oktober 2012

    This is really useful post!
    Thank you!

  1. Thank you man.

    • bharat
    • 18. Juli 2014

    Thanks alot..🙂

    • Hrvoje
    • 20. November 2014

    Thanks.

  2. Can i use 3 parametres?
    bundletext=some text {0} some text {1} some text {2}
    returns
    some text v1 some text v2 some text {2}

  3. Yes you can use 3 or more parameters in your property file. Call

    Messages.getString(„key“, „p1“, „p2“, „p3“ )

    to get the result.

    • Runnerdave
    • 23. Mai 2016

    Danke schön!

  1. No trackbacks yet.

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: