Question

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?

Was it helpful?

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();
}

OTHER TIPS

Each satellite assembly for a specific language is named the same but lies in a sub-folder named after the specific culture e.g. fr or fr-CA.
Maybe you can use this fact and scan the folder hierarchy to build up that menu dynamically.

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