-1

I have been reviewing the following link

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx

But I still don't understand how can I randomly retrieve a valid 2 letter culture value using CultureInfo class such as "en" or "fr".... etc.

Please help.

Thanks

dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200
  • Did you try `new CultureInfo("en")`? – Vlad Aug 01 '12 at 16:33
  • why do you want to create a random culture? – Fakrudeen Aug 01 '12 at 16:34
  • Why would you want to "randomly create a valid 2 letter culture" ? You have the complete list on that site, these all contain the information for formatting numbers, dates etc. correctly, why create a new one? – Steen Tøttrup Aug 01 '12 at 16:35
  • Indeed, please clarify: Do you want to *create* or *retrieve* culture information, and do you mean *randomly* (non-deterministically chosen at runtime) or *arbitrarily* (somehow chosen by you, but in a way that it works whatever you choose)? – O. R. Mapper Aug 01 '12 at 16:37
  • why my question is down voted? I tried my best to message it as clearly as possible. – dotnet-practitioner Aug 01 '12 at 17:31

3 Answers3

5

(I'm not sure exactly why you'd want this, but...) You can get a random culture by using CultureInfo.GetCultures, then randomly selecting from the results:

var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
var random = new Random();
int index = random.Next(0, allCultures.Length);

var culture = allCultures[index];

string twoLetterCode = culture.TwoLetterISOLanguageName;

Note that this doesn't take into account that there are not an even number of cultures with the same 2 letter codes. This will randomly pick amongst all cultures, but not evenly through the 2 letter codes. If you want a more random distribution there, you could use:

var uniqueCultureCodes = CultureInfo.GetCultures(CultureTypes.AllCultures)
                      .Select(c => c.TwoLetterISOLanguageName)
                      .Distinct()
                      .ToList();

var random = new Random();
int index = random.Next(0, uniqueCultureCodes.Count);

string twoLetterCode = uniqueCultureCodes[index];

This creates the distinct list of two letter codes, then randomly picks from them.


Edit: If your goal is merely to create a CultureInfo given a two letter code such as "en" or "fr", you can do:

CultureInfo culture = new CultureInfo("en");
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • thanks this was exactly what I was looking for. Its not letting me accept the answer. – dotnet-practitioner Aug 01 '12 at 16:41
  • @dotnet-practitioner It will - you just have to wait a little while, as the system makes sure other people can answer, as well. – Reed Copsey Aug 01 '12 at 16:42
  • That's hardly random, as there is a bigger chance of getting an English culture than the German one (several "en-XX" cultures). – Steen Tøttrup Aug 01 '12 at 16:43
  • @SteenT It's unclear which is better - I edited to provide both options. The original picked a random *culture* then provided the 2 letter code, my edit shows picking randomly from available 2 letter codes. – Reed Copsey Aug 01 '12 at 16:52
1

But I still don't understand how can I randomly create a valid 2 letter culture value using CultureInfo class such as "en" or "fr".... etc.

You mean you want to get a random culture? Simply get all the valid cultures, and take a random element from that collection. (You can use System.Random, but be aware of the various issues you need to handle.)

EDIT: Note that if you are trying just to get a single culture from a known abbreviation, I'd recommend using CultureInfo.GetCultureInfo instead of the CultureInfo constructor. That allows for caching, and also gives you a read-only culture which is almost certainly what you want. (It's easier to reason about read-only data...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think, OP means "arbitrary" when he mentions "random". – Vlad Aug 01 '12 at 16:34
  • Thanks Jon.. Reed provided me the exact code that I was looking for. I am unable to accept his answer for now. – dotnet-practitioner Aug 01 '12 at 16:42
  • @dotnet-practitioner: Please read the link in my answer about issues with `Random` though: if you call Reed's code several times in quick succession, it may well give you the same culture multiple times. – Jon Skeet Aug 01 '12 at 16:43
  • Jon.. at this point, I am not looking for true random-ness.. but I appreciate your detailed feedback.. thanks – dotnet-practitioner Aug 01 '12 at 17:33
1

If you don't care about the region (the last part of the <language>-<region>), you'll want to use:

CultureInfo[] languages = CultureInfo.GetCultures(CultureTypes.NeutralCultures);

To retrieve the different languages. If you use:

CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

There's a bigger change of getting English than German (more English cultures than German).

Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36