問題描述
很多時候,當生成要顯示給用戶的消息時,該消息將包含一些我想告知客戶的東西.
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.
我舉個例子:客戶從 1 起選擇了多個項目,并單擊了刪除.現在我想給客戶一個確認信息,我想提一下他選擇的項目數量,以通過選擇一堆項目并在他只想刪除其中一個時單擊刪除來最大程度地減少他出錯的機會他們.
I'll give an example: The customer has selected a number of items from 1 and up, and has clicked delete. Now I want to give a confirmation message to the customer, and I want to mention the number of items he has selected to minimize the chance of him making a mistake by selecting a bunch of items and clicking delete when he only wants to delete one of them.
一種方法是制作這樣的通用消息:
One way is to make the generic message like this:
int noofitemsselected = SomeFunction();
string message = "You have selected " + noofitemsselected + " item(s). Are you sure you want to delete it/them?";
這里的問題"是 noofitemselected
為 1 的情況,我們必須寫 item 和 it 而不是 項目和他們.
The "problem" here is the case where noofitemselected
is 1, and we have to write item and it instead of items and them.
我的正常解決方案是這樣的
My normal solution will be something like this
int noofitemsselected = SomeFunction();
string message = "You have selected " + noofitemsselected + " " + (noofitemsselected==1?"item" : "items") + ". Are you sure you want to delete " + (noofitemsselected==1?"it" : "them") + "?";
如果代碼中有很多對數字復數的引用,那么這會變得很長而且非常快,而且實際的消息很難閱讀.
This gets quite long and quite nasty really fast if there are many references to the numbers plurality inside the code, and the actual message gets hard to read.
所以我的問題很簡單.有沒有更好的方法來生成這樣的消息?
So my questions is simply. Are there any better ways of generating messages like this?
編輯
我看到很多人對我提到消息應該顯示在消息框內的情況非常感興趣,并且只是簡單地給出了如何完全避免使用消息框的答案,并且都很好.
I see a lot of persons has got very hung up in the case that I mentioned that the message should be displayed inside a message box, and has simply given an answer of how to avoid using the message box at all, and that is all good.
但請記住,除消息框外,復數化問題也適用于程序中其他地方的文本.例如,顯示在網格中選擇的行數的網格旁邊的標簽將在復數方面存在相同的問題.
But remember that the problem of pluralization also apply to texts other places in the program in addition to message boxes. For example, a label alongside a grid displaying the number of lines selected in the grid will have the same problem regarding pluralization.
所以這基本上適用于大多數從程序以某種方式輸出的文本,然后解決方案并不像僅僅將程序更改為不再輸出文本那么簡單:)
So this basically apply to most text that is outputted in some way from programs, and then the solution is not as simple as to just change the program to not output text anymore :)
推薦答案
如果有任何機會,無論多么小,這個應用程序都需要翻譯成其他語言,那么兩者都是錯誤的.正確的做法是:
If there is ever any chance, no matter how small, that this app will need to be translated to other languages then both are wrong. The correct way of doing this is:
string message = ( noofitemsselected==1 ?
"You have selected " + noofitemsselected + " item. Are you sure you want to delete it?":
"You have selected " + noofitemsselected + " items. Are you sure you want to delete them?"
);
這是因為不同的語言處理復數的方式不同.有些像馬來語甚至沒有語法復數,所以字符串通常是相同的.將這兩個字符串分開可以更容易地在以后支持其他語言.
This is because different languages handle plurality differently. Some like Malay don't even have syntactic plurals so the strings would generally be identical. Separating the two strings makes it easier to support other languages later on.
否則,如果此應用程序旨在供公眾使用并且應該對用戶友好,那么第二種方法更可取.抱歉,我真的不知道更短的方法.
Otherwise if this app is meant to be consumed by the general public and is supposed to be user friendly then the second method is preferable. Sorry but I don't really know a shorter way of doing this.
如果此應用僅供貴公司內部使用,請執行快捷方式 "item(s)"
操作.在編寫企業代碼時,您實際上不必給任何人留下深刻印象.但我建議不要對公開消費的應用程序這樣做,因為這給人的印象是程序員很懶惰,從而降低了他們對應用程序質量的看法.相信我,像這樣的小事.
If this app is meant to be consumed only internally by your company then do the shortcut "item(s)"
thing. You don't really have to impress anybody when writing enterprisy code. But I'd advise against doing this for publicly consumed app because this gives the impression that the programmer is lazy and thus lower their opinion of the quality of the app. Trust me, small things like this matter.
這篇關于用戶消息中的多個的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!