Plurality in user messages

前端 未结 25 1549
没有蜡笔的小新
没有蜡笔的小新 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:47

    The first thing I'd suggest is: use string.Format. That allows you to do something like this:

    int numOfItems = GetNumOfItems();
    string msgTemplate;
    msgTemplate = numOfItems == 1 ? "You selected only {0} item." : "Wow, you selected {0} items!";
    string msg = string.Format(msgTemplate, numOfItems);
    

    Further, in WPF apps, I've seen systems where a resource string would be pipe-delimited to have two messages: a singular and a plural message (or a zero/single/many message, even). A custom converter could then be used to parse this resource and use the relevant (formatted) string, so your Xaml is something like this:

    
    

提交回复
热议问题