Prefixed ResourceBunlde:

With PrefixedResourceBundle you can have localized and environment depended messages. It works like a normal ResourceBundle with language_country_variant as localization identifier within your filename.

For example:

  • * * *

    Since in the back a normal PrefixedProperties object does the work. You can also have xml or json as your message format and even mix them. PrefixedResourceBundle will find them automatically.

    For example:

  • messages_de_DE.json * * messages_en.xml *

    To create a PrefixedResourceBundle you can use one of three factory methods to instantiate it.


In this example we use PrefixedResourceBundle to load the messages for a specific locale and environment.

  greetings=Hello {0}
  male.greetings=Hello Mr. {0}
  female.greetings=Hello Mrs. {0}

  greetings=Hallo {0}
  male.greetings=Hallo Herr {0}
  female.greetings=Hallo Frau {0}

usage with PrefixedRescourceBundle:

  //At first configure and create the PrefixedResourceBundle 
  final PrefixedResourceBundle bundle_en = PrefixedResourceBundle.getPrefixedResourceBundle("messages", Locale.ENGLISH, "environment");
  final PrefixedResourceBundle bundle_de = PrefixedResourceBundle.getPrefixedResourceBundle("messages", Locale.GERMAN, "environment");
  //in this case we use Thread depended configurations of the prefix.
  bundle_en.getString("greetings"); // will return Hello Mr. {0}
  //while within another Thread the following could be done.
  bundle_en.getString("greetings"); // will return Hello Mrs. {0}

As you can see this gets quite easy for you to access different resource bundles entries according to a specific state or environment just by changing the context of the ResourceBundle. So instead of making a decision if a user is male or female and choose the right bundle key, just set the context once and than get your message entries straight forward.

You can also use cascading prefixes for your configuredPrefix for example male.child, male.teene, and so on.