Formatting Messages
Formatting Messages Handling plurality adequately
Published: March 1st, 2016, Updated: Some days ago
Once in a while I stumble upon code to format message texts in C#, either mine or from developers on Stack Overflow. It looks like this:
string messageTemplate; if (_itemCount == _persistedItemCount) messageTemplate = Resources.ProtocolMessageAllItemsAreFiled; else if (_persistedItemCount == 0) messageTemplate = Resources.ProtocolMessageNoItemsAreFiled; else messageTemplate = Resources.ProtocolMessageSomeItemsAreFiled; return String.Format(messageTemplate, _persistedItemCount, _itemCount);
Developers get quite — let's say — "creative" to format readable message texts for the user, especially when it comes to plurality. Source code like this is ok, but we can do better.