Search code examples
c#assembliesinternationalizationglobalizationsatellite

Get all the supported Cultures from a satellite assembly


I am using a satellite assembly to hold all the localization resources in a C# application.

What I need to do is create a menu in the GUI with all the available languages that exists for the application. Is there any way to get information dynamically?


Solution

  • This function returns an array of all the installed cultures in the App_GlobalResources folder - change search path according to your needs. For the invariant culture it returns "auto".

    public static string[] GetInstalledCultures()
    {
        List<string> cultures = new List<string>();
        foreach (string file in Directory.GetFiles(HttpContext.Current.Server.MapPath("/App_GlobalResources"), \\Change folder to search in if needed.
            "*.resx", SearchOption.TopDirectoryOnly))
        {
            string name = file.Split('\\').Last();
            name = name.Split('.')[1];
    
            cultures.Add(name != "resx" ? name : "auto"); \\Change "auto" to something else like "en-US" if needed.
        }
        return cultures.ToArray();
    }
    

    You could also use this one for more functionality getting the full CultureInfo instances:

    public static CultureInfo[] GetInstalledCultures()
    {
        List<CultureInfo> cultures = new List<CultureInfo>();
        foreach (string file in Directory.GetFiles(HttpContext.Current.Server.MapPath("/App_GlobalResources"), "*.resx", SearchOption.TopDirectoryOnly))
        {
            string name = file.Split('\\').Last();
            name = name.Split('.')[1];
    
         string culture = name != "resx" ? name : "en-US";
         cultures.Add(new CultureInfo(culture));
        }
        return cultures.ToArray();
    }