6

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?

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Makis Arvanitis
  • 1,175
  • 1
  • 11
  • 18

2 Answers2

2

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();
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
1

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.

Gishu
  • 134,492
  • 47
  • 225
  • 308