Plurality in user messages

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

    I'd go with not hardcoding the message, but providing two messages in an seperate Resource file. Like

    string DELETE_SINGLE = "You have selected {0} item. Are you sure you want to delete it?";
    string DELETE_MULTI = "You have selected {0} items. Are you sure you want to delete them?";
    

    and then feeding them into String.Format like

    if(noofitemsselected == 1)
        messageTemplate = MessageResources.DELETE_SINGLE;
    else
        messageTemplate = MessageResources.DELETE_MULTI;
    
    string message = String.Format(messageTemplate, noofitemsselected)
    

    I think that this approach is easier to localize and maintain. All UI messages would be at a single locaion.

提交回复
热议问题