Hi,
Hi,

You can use: public final String getString(final String key, final
IModel<?> model) (
https://github.com/apache/wicket/blob/46713eef353c31372482e990ddcffe7a5ffb1bf1/wicket-core/src/main/java/org/apache/wicket/Component.java
)
The IModel parameter may be a POJO or a Map. Any placeholder like
${something} will be looked up as a bean property in case of POJO or a key
in case of a Map.
See PropertyResolver for more details.
The advantage here compared to your suggestion is that it uses named
parameters instead of indexed ones. Although you may use Array/List as
model object and use indexed parameters if you try harder.

While I agree on named parameters being easier to work with than using plain indices, we usually have <= 3 placeholders per string resource and for those it IMHO really doesn't matter all that much. And I would bet that most applications will not use much more than 3 placeholders.

Because I got curious about my last statement I did a quick check across our ~300 .properties file with ~10000 string properties, ~550 of them using parameter substitution:

$ for file in `find . -name '*.properties' | grep -v target | grep -v test` ; do cat $file| grep \{ ; done | sed 's/[^\{]//g' | tr -d '\\' | sort | uniq -c
    319 {
    129 {{
     54 {{{
     24 {{{{
      9 {{{{{
      2 {{{{{{
      3 {{{{{{{
      2 {{{{{{{{

So ~93% of our string properties using substitution have three or less placeholders.

If I understood you correctly, your solution would be to replace

MessageFormat.format(getString("some.string.resource"), getValue1(), getValue2());

with

getString("some.string.resource",()->Arrays.asList(getValue1(), getValue2()));

, right?

While this works I'm not too happy about the intermediate objects being created here (assuming the compiler is not smart enough to get rid of them, not counting the array creation for the asList() call as my varargs getString() method would have to pay the same price) and the additional noise.

I'd still prefer to be able to write

                 getString("some.string.resource", getValue1(), getValue2())

... so how are the odds of getting such a pull request accepted ? :-)

Cheers,
Tobi


On Wed, Apr 17, 2019 at 2:27 PM Tobias Gierke <[email protected]>
wrote:

Hi,

Is there any shorter way (shorter as in 'less characters to type') to do
this ?


feedback.info(MessageFormat.format(getString("feedback.info.reportDeleted"),

toDelete.getReportName()));

I know about StringResourceModel but

       feedback.info(new
StringResourceModel("feedback.info.reportDeleted").setParameters(
toDelete.getReportName() ).getObject());

is not exactly shorter :/

It would be nice to have something like the following method on both
Component and Localizer:

      /**
* Returns a localized string while applying <code>MessageFormat</code>
placeholder substitution.
*
* The placeholder syntax is the one supported by {@link
java.text.MessageFormat}.
*
* @param key Key of string resource in property file
* @param parameters Optional parameters for placeholder substitution
* @return The String
* @see java.text.MessageFormat
*/
public String getString(final String key,Object... parameters);


Cheers,
Tobias

P.S. We're on Wicket 8 btw



Reply via email to