开发者

Enhanced MessageFormat?

开发者 https://www.devze.com 2023-02-02 05:09 出处:网络
I\'ve been using MessageFormat.format() for a while, but there\'s one thing that I find annoying. Everytime you declare a parameter in a message, you have to know the position of the mapped argument.

I've been using MessageFormat.format() for a while, but there's one thing that I find annoying.

Everytime you declare a parameter in a message, you have to know the position of the mapped argument. e.g.

MessageFormat.format("{0} is annoying {1}.", "this", "indeed")

Is there a class that is the same as MessageFormat in every other way but lets you omit the argument position in parameter declaration altogether and have it defaul开发者_StackOverflow中文版t to its position in the message so that the first parameter maps to the first argument, the second parameter to the second argument and so on? e.g.

MessageFormat.format("{} is much better{}.", "this", "indeed")

I think later versions of log4j have a similar feature, but I just need the formatting class.

Happy New Year!

EDIT: I need this feature for assertion, so it's really for internal use, but I appreciate your insight into why MessageFormat works the way it does.


You should be using Formatter or its frontend, String.format, instead. You can do:

String.format("%s is much better %s", "this", "indeed");
String.format("%1$s provides positional formatting %2$s", "this", "indeed");


Providing what you request would defeat one of the main purposes of MessageFormat: I18N and L10N. That is, different natural languages may use different word orders to compose the "same" sentence. MessageFormat allows your code to force feed in a set number of arguments in a set order, and then vary the interpolation order when writing the localized natural language strings for each language—which you'd likely define in suitably-named Java Properties files that you'll load with class ResourceBundle.

The typical workflow involves first writing the user-visible messages in your primary natural language (say, English), then, for each additional language to which you wish to localize the software, define parallel Properties files that translate those parameterized messages into the target language, shuffling the input parameters around to suit the proper sentence structure.

If you don't need any of that capability, try just using String#format() instead.


I vaguely recall coming across such a thing. However, I can't recall where it was. Still, there are solutions you can use. For instance, you can store the format strings in an external bundle, just as you do now, but instead of using MessageFormat strings, use printf-style strings, as supported by String.format(). Those strings permit positional argument notation, but they also permit traditional "take them as they come in the argument list" ordering.

Note, however, that there is a reason for having to specify the index: Internationalization. If you decide to move the format strings to an external message bundle, it's typically to permit localization (to other languages), where the order of the words in the localized sentence might not match the order of the strings in the argument list.

In short, you're better off sticking with specifying the ordering. It makes things easier, if you find your application is fabulously successful and you have incredible sales opportunities in some other country, if only you can get all your messages to come out in their language...

0

精彩评论

暂无评论...
验证码 换一张
取 消