0

In Java, there is a class ChoiceFormat that can be used to localize strings based on a number value:

 ChoiceFormat fmt = new ChoiceFormat("0#no values found | 1#one value found|1<many values found");

 System.out.println(fmt.format(0)); // no values found
 System.out.println(fmt.format(1));  // one value found
 System.out.println(fmt.format(2)); // many values found
 System.out.println(fmt.format(10)); // many values found

This is of course very important when internationalizing software, i.e. when supporting languages that have not only a singular and plural, but a dual, too. Such a language might have something like this "0#no values found | 1#one value found|2#both values found |1<many values found" and fmt.format(2) and fmt.format(10) would print different values unlike English.

What is the counterpart in C# for this functionality?

Stefan S.
  • 3,950
  • 5
  • 25
  • 77
  • You might want to have a look at [Humanizer](https://github.com/Humanizr/Humanizer). – 41686d6564 stands w. Palestine Nov 03 '22 at 08:56
  • "In C#" is probably too big of a scope, actually. What Frontend tech are we talking? WinForms, WPF, ASP.Net, Blazor, ... ? – Fildor Nov 03 '22 at 08:57
  • @Fildor I'm not talking any frontend, but backend. I said "C#" because `ChoiceFormat` is part of the Java core API, and I assumed the same functionality (however it is called) was part of the C# core API as well. – Stefan S. Nov 03 '22 at 09:06
  • In that case, you may indeed want to have a look at Humanizer. I am not aware of any built-in technology that would take care of this in C# ( or actually .NET for that matter ). – Fildor Nov 03 '22 at 09:07
  • @41686d6564standsw.Palestine I've only skimmed the readme for now, but how would the stated example be converted into Humanizer API? – Stefan S. Nov 03 '22 at 09:09
  • @StefanS. The [`ToQuantity` method](https://github.com/Humanizr/Humanizer#toquantity) gets you as far as "zero values found", "one value found", "[two|three|etc.] values found". You can customize that by registering a custom factory class that implements the `INumberToWordsConverter`. But honestly, if you're just going to use it for the purpose you described above, writing your own logic is probably going to be simpler. – 41686d6564 stands w. Palestine Nov 03 '22 at 09:50
  • [Maybe writing you own string formatter may be an option to you](https://stackoverflow.com/questions/35577011/custom-string-formatter-in-c-sharp) – CookedCthulhu Nov 03 '22 at 10:31

0 Answers0