Should I use java.text.MessageFormat for localised messages without placeholders?

前端 未结 5 2142
北海茫月
北海茫月 2021-02-20 03:55

We are localising the user-interface text for a web application that runs on Java 5, and have a dilemma about how we output messages that are defined in properties files - the k

相关标签:
5条回答
  • 2021-02-20 04:38

    In the end we decided to side-step the single quote problem by always using ‘curly’ quotes:

    warning.item = This item\u2019s {0} is not valid.
    
    0 讨论(0)
  • 2021-02-20 04:38

    Use the ` character instead of ' for quoting. We use it all the time without problems.

    Use MessageFormat only when you need it, otherwise they only bloat up the code and have no extra value.

    0 讨论(0)
  • 2021-02-20 04:51

    Just write your own implementation of MessageFormat without this annoying feature. You may look at the code of SLF4J Logger.

    They have their own version of message formatter which can be used as followed:

    logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);
    

    Empty placeholders could be used with default ordering and numbered for some localization cases where different languages do permutations of words or parts of sentences.

    0 讨论(0)
  • 2021-02-20 04:52

    In my opinion, consistency is important for this sort of thing. Properties files and MessageFormat already have lots of limitations. If you find these troubling you could "compile" your properties files to generate properly-formed ones. But I'd say go with using MessageFormat everywhere. This way, as you maintain the code you don't need to worry about which strings are formatted and which aren't. It becomes simpler to deal with, since you can hand off message processing to a library and not worry about the details at a high level.

    0 讨论(0)
  • 2021-02-20 04:54

    Another alternative...When loading the properties file, just wrap the inputstream in a FilterInpuStream that doubles up every single quote.

    0 讨论(0)
提交回复
热议问题