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:

  • messages_de_DE.properties * messages_de_DE_bw.properties * messages_en.properties * messages.properties

    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_de_DE_bw.properties * messages_en.xml * messages.properties

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

Example:

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

messages_en.properties:

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

messages_de.properties:

  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.setConfiguredPrefix("male"); 
  bundle_en.getString("greetings"); // will return Hello Mr. {0}
  
  //while within another Thread the following could be done.
  bundle_en.setConfiguredPrefix("female"); 
  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, female.adult and so on.