GetGeoInfo - get country names in different languages

120 Views Asked by At

I'm trying to get a list of countries in a handful of different languages. The issue I've got is that I also need to match these to the GeoId from Windows, which can be found in this list.

I've been using the code from this answer as a starting point, and trying to edit this so when I provide the list of codes that I want to get in multiple languages, I can change the language I want, and get the name of the country I need. This uses the GetGeoInfo Windows API to get the country details from the GeoId provided.

The code I have now been trying to get working as a proof of concept is here:

using System.Runtime.InteropServices;
using System.Text;

public class Program
{
    private enum SYSGEOTYPE
    {
        GEO_NATION = 0x0001,
        GEO_LATITUDE = 0x0002,
        GEO_LONGITUDE = 0x0003,
        GEO_ISO2 = 0x0004,
        GEO_ISO3 = 0x0005,
        GEO_RFC1766 = 0x0006,
        GEO_LCID = 0x0007,
        GEO_FRIENDLYNAME = 0x0008,
        GEO_OFFICIALNAME = 0x0009,
        GEO_TIMEZONES = 0x000A,
        GEO_OFFICIALLANGUAGES = 0x000B,
        GEO_ISO_UN_NUMBER = 0x000C,
        GEO_PARENT = 0x000D,
        GEO_DIALINGCODE = 0x000E,
        GEO_CURRENCYCODE = 0x000F,
        GEO_CURRENCYSYMBOL = 0x0010,
        GEO_NAME = 0x0011,
        GEO_ID = 0x0012
    };

    [DllImport("kernel32.dll")]
    private static extern int GetUserDefaultLCID();

    [DllImport("kernel32.dll")]
    private static extern int GetGeoInfo(int geoid, int geoType, StringBuilder lpGeoData, int cchData, int lcid);

    /// <summary>
    /// Returns machine current location as specified in Region and Language settings.
    /// </summary>
    public static string GetLocationName()
    {
        var geoId = 94;
        var lcid = 94;
        var locationBuffer = new StringBuilder(100);
        GetGeoInfo(geoId, (int)SYSGEOTYPE.GEO_FRIENDLYNAME, locationBuffer, locationBuffer.Capacity, lcid);

        return locationBuffer.ToString().Trim();
    }

    private static void Main(string[] args)
    {
        Console.WriteLine(GetLocationName());
    }
}

From playing around with this, if I change the geoId then it changes the country I am looking at, and that is working fine. If I try to change the lcid to anything, I'm not able to get it away from English. I am running in English on my Windows machine. I have trialled this on a VM, and installed the German language pack and then switched to it in Windows, and doing this then means that GetGeoInfo returns the country name in German.

What I don't understand is what does the last argument in the GetGeoInfo do, and is there a way I can use this to switch the language I want to retrieve from, even if I have to download a lot of language packs onto my machine to do this.

If there are any other ways of getting the country names in multiple languages along with the GeoId I'd love to hear them. I have looked at the Unicode Common Locale Data Repository but I'll need to get my head around where exactly I need to pull the countries from, and I'll also need to remove the requirements I currently have with the GeoId before I can move to this, so I am looking at trying to get the GetGeoInfo API working in the meantime.

0

There are 0 best solutions below