Question

I'm trying to obtain region info by ISO 3166-1 TwoLetters country name - "MD".

var r = new RegionInfo("MD");

But I'm obtaining the following exception:

Culture name 'MD' is not supported.

This is strange because Microsoft Table of supported countries Moldova is present:

http://msdn.microsoft.com/en-us/library/dd374073.aspx

Was it helpful?

Solution

According to the MSDN documentation on RegionInfo about the cultures names:

The predefined culture names are listed in the National Language Support (NLS) API Reference at the Go Global Developer Center.

When you go to National Language Support (NLS) API Reference, MD is not found in there.

OTHER TIPS

You may create your own culture info.

Run Visual Studio as Administrator. In your project, add a reference to sysglobl.

using System;
using System.IO;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;

class Program
{
    public static void Main()
    {
        CultureAndRegionInfoBuilder cib = null;
        try
        {
            // Create a CultureAndRegionInfoBuilder 
            // object named "ro-MD".
            cib = new CultureAndRegionInfoBuilder(
                                    "ro-MD", CultureAndRegionModifiers.None);

            // Populate the new CultureAndRegionInfoBuilder 
            // object with culture information.
            CultureInfo ci = new CultureInfo("ro-RO");
            cib.LoadDataFromCultureInfo(ci);

            // Populate the new CultureAndRegionInfoBuilder 
            // object with region information.
            RegionInfo ri = new RegionInfo("RO");
            cib.LoadDataFromRegionInfo(ri);

            var filePath = "ro-MD.xml";

            if (File.Exists(filePath))
                File.Delete(filePath);

            // Save as XML
            cib.Save(filePath);

            // TODO: modify the XML
            var xDoc = XDocument.Load(filePath);
            var ns = 
                "http://schemas.microsoft.com/globalization/2004/08/carib/ldml";

            xDoc.Descendants(XName.Get("regionEnglishName", ns))
                .FirstOrDefault().Attribute("type").SetValue("Moldova");

            xDoc.Descendants(XName.Get("regionNativeName", ns))
                .FirstOrDefault().Attribute("type").SetValue("Moldova");

            // and so on
            xDoc.Save(filePath);

            var roMd = CultureAndRegionInfoBuilder
                .CreateFromLdml(filePath);

            // this may throw an exception if the culture info exists 
            try
            {
                CultureAndRegionInfoBuilder.Unregister("ro-MD");
            }
            catch (Exception)
            {
                //throw;
            }

            // Register the custom culture.
            roMd.Register();

            // Display some of the properties of the custom culture.
            var riMd = new RegionInfo("ro-MD");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

You'll just have to take care of the XML modification.

Note: It seems that you may also save the culture without administrative privileges. Here's a reference: How to: Save Custom Cultures Without Administrative Privileges. I haven't tested it myself, but the only comment on that article seems to suggest it doesn't work.

[UPDATE]

This is also an interesting approach (a wrapper for invoking native methods):

Get the full list of Windows supported countries with C#.

System.Globalization.RegionInfo uses the culture data and if there is no culture data with that region, then it will not succeed. That is also why creating a custom culture for a language with that region will cause it to succeed.

What you probably want instead is to use the new Windows.Globalization.GeographicRegion which does support all the current ISO-3166 countries.

Or you can p/Invoke into GetGeoInfo and EnumSystemGeoId as they support Moldova as indicated in the documentation you reference above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top