Plurality in user messages

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

    To be able to have pluralized messages which will be possible to localize properly, my opinion is that it would be wise to first create a layer of indirection between the number and a message.

    For example, use a constant of some sort to specify which message you want to display. Fetch the message using some function that will hide the implementation details.

    get_message(DELETE_WARNING, quantity)
    

    Next, create a dictionary that holds the possible messages and variations, and make variations know when they should be used.

    DELETE_WARNING = {
       1: 'Are you sure you want to delete %s item',
       >1: 'Are you sure you want to delete %s items'
       >5: 'My language has special plural above five, do you wish to delete it?'
    }
    

    Now you could simply find the key that corresponds to the quantity and interpolate the value of the quantity with that message.

    This oversimplified and naive example, but I don't really see any other sane way to do this and be able to provide good support for L10N and I18N.

提交回复
热议问题