Simple way to use parameterised UI messages in Wicket?

后端 未结 6 645
借酒劲吻你
借酒劲吻你 2021-01-02 02:43

Wicket has a flexible internationalisation system that supports parameterising UI messages in many ways. There are examples e.g. in StringResourceModel javadocs, such as thi

6条回答
  •  耶瑟儿~
    2021-01-02 03:33

    When faced with something like described in the question, I would now use:

    .properties:

    msg=Saving record %s with value %d
    

    Java:

    add(new Label("label", String.format(getString("msg"), record, value)));
    

    Why I like it:

    • Clean, simple solution
    • Uses plain Java and nothing else
    • You can replace as many values as you want (unlike with the ${} trick). Edit: well, if you actually need to support many languages where the replaced values might be in different order, String.format() is no good. Instead, using MessageFormat is a similar approach that properly supports this.

    Disclaimer: this is "too obvious", but it's simpler than the other solutions (and definitely nicer than my original replaceAll() hack). I originally sought for a "Wicket-y" way, while this kinda bypasses Wicket—then again, who cares? :-)

提交回复
热议问题