How do I avoid repetition in Java ResourceBundle strings?

对着背影说爱祢 提交于 2019-12-03 02:04:31

I think this is a fundamental flaw in the way ResourceBundles are designed to function: keys that reference other keys automatically violate the DRY (don't repeat yourself) principle. The way I got around this was similar to your method: create a ReflectiveResourceBundle class that allows you to specify Resource keys in the messages using EL notation.

THE WRONG WAY:

my.name.first=Bob
my.name.last=Smith
my.name.full=Bob Smith

THE RIGHT WAY:

my.name.first=Bob
my.name.last=Smith
my.name.full=${my.name.first} ${my.name.last}

I've uploaded the code to GitHub so you or anyone else can download it. Additionally, I've added some sample code for anyone using the Stripes Framework (http://www.stripesframework.org/) to get you quickly up-and-running.

The trick to getting this to work with standard JSTL fmt taglibs was to set up an interceptor that replaced the HttpServletRequest's resource with our own. The code looks something like this:

ResourceBundle bundle = MyStaticResourceHoldingTheBundle.getBundle();
Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle, locale));

Take a look at the stripes.interceptor package in the link above for more details.

If the string repetitions are localized in the sense that you know a certain string will be repeated but only within the same project such that sharing resource bundles is not a design nightmare, then you might consider breaking the strings down into multiple key-value parts. Separate the parts that repeat from those that do not and reuse the repeated parts. For example, lets say you have the following two strings you need to display:

  1. "The Red-capped Robin is a small passerine bird native to Australia. "
  2. "The Red-capped Robin is found in dryer regions across much of the continent."

The resource bundle could be as follows:

robin.name=The Red-capped Robin
robin.native=is a small passerine bird native to Australia.
robin.region=is found in dryer regions across much of the continent.

and then combine the required parts where needed bundle.getString("robin.name")+bundle.getString(robin.native).

One thing you need to be careful about though is that the grammar rules like subject predicate order etc. might not be the same in all languages. So you would need to be a little careful when splitting sentences.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!