Plurality in user messages

前端 未结 25 1559
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 01:53

Many times, when generating messages to show to the user, the message will contain a number of something that I want to inform the customer about.

I\'ll give a

25条回答
  •  一个人的身影
    2021-01-30 02:38

    What about what Java has had for years: java.text.MessageFormat and ChoiceFormat? See http://download.oracle.com/javase/1.4.2/docs/api/java/text/MessageFormat.html for more information.

    MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
    form.applyPattern(
       "There {0,choice,0#are no files|1#is one file|1

    In your case you want something somewhat simpler:

    MessageFormat form = new MessageFormat("Are you sure you want to delete {0,choice,1#one item,1<{0,number.integer} files}?");
    

    The advantage of this approach is that it works well with the i18n bundles, and you can provide translations properly for languages (like Japanese) that have no concept of plural or singular words.

提交回复
热议问题