tw2017
tw2017

Reputation: 105

List of string int to determine int

I have a list of language codes with their Windows LCID values:

"de-de" 1031  
"de" 1031
"en-us" 1033
"en" 1033
"en-US" 1033

I would like to create a function that I can feed a string like "en-US" and have it tell me the LCID (which would be 1033 in this case).

Since I only have like 300 of these "items", I would like to hardcode it.

I'm however unsure what would be the appropriate way to code this.

I guess I shouldn't use

if (value == "en-us")
     return 1033;
else if (value == "en-US")
     return 1033;

Could somebody show me an adult way of dealing with this?

Upvotes: 0

Views: 289

Answers (5)

hnefatl
hnefatl

Reputation: 6037

Use a Dictionary, keyed on the language code with values being the LCID.

This can still be "hardcoding", as you can add key/values to the dictionary manually, but it makes the maintainability etc so much higher, and if you want to switch to a better way of loading them later, you can easily (just replace your manual adding by some file read+parse, for example).

Dictionary<string, int> lcidLookup = new Dictionary<string, int>();
lcidLookup.Add("de-de", 1031);
lcidLookup.Add("de", 1031);
lcidLookup.Add("en-us", 1033);
lcidLookup.Add("en", 1033);

...

int lcid = lcidLookup["en-us"];

Consider also ensuring that all the keys you use are converted to lower/upper case (see String.ToLower and analogous String.ToUpper), and ensuring that all the keys you search for match this convention, to avoid bugs around casing (en-US vs en-us).


Given that your LCID values map to actual locale codes, you should use @Waescher's answer instead - it prevents you from needing to do any hardcoding at all.

Upvotes: 0

tmighty
tmighty

Reputation: 11399

    public static int LCIDFromLangName(string uKey)
    {
        uKey = uKey.ToLower();

        Dictionary<string, int> _dic;
        _dic = new Dictionary<string, int>();
        _dic.Add("de-de", 1031);
        _dic.Add("en-us", 1033);
        (and so on)

        int iRet = 0;
        bool b = _dic.TryGetValue(uKey, out iRet);
        if (!b)
        {
            iRet = 1033;
        }
        return iRet;

Upvotes: -1

Narvarth
Narvarth

Reputation: 543

Use System.Globalization.CultureInfo:

CultureInfo ci = new CultureInfo("en-US");
Console.WriteLine(ci.LCID);

Upvotes: -1

Waescher
Waescher

Reputation: 5737

You need a function to feed culture codes to get the LCID? Just use the .NET default way:

return System.Globalization.CultureInfo.GetCultureInfo("de-DE").LCID;

If you need to manage a mapping from those codes to LCIDs, you can call GetCultures() because "only" 300 does not sound good to me to be hardcoded. We both know that every culture in .NET has that LCID. So what about listing all of them and maybe putting them into a dictinary to be resolved easily.

Get the default list from here:

    foreach (var c in  System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.UserCustomCulture | System.Globalization.CultureTypes.SpecificCultures))
        Console.WriteLine(c.ToString() + " code:" + c.LCID);

Upvotes: 3

Ousmane D.
Ousmane D.

Reputation: 56453

one solution is to use a switch statement i.e:

switch(value.ToLower())
{
       case "en-us":
       case "en":
            return 1033;
       case "de-de":
       case "de":
            return 1031;
       default: return -1;
}

Upvotes: 0

Related Questions