views:

41

answers:

1

I need to programatically list the available cultures in a resx file group, but the ResourceManager class doesn't seem to help.

I may have :

Labels.resx
Labels.fr-FR.resx
Labels.ro-RO.resx

and so on

However, how can I find these three (or how many would there be) cultures at runtime?

+2  A: 

Look for satellite assemblies in your application's directory : for each subdirectory, check if it's name corresponds to a culture name, and if it contains a '.resources.dll' file :

public IEnumerable<CultureInfo> GetAvailableCultures()
{
    var programLocation = Process.GetCurrentProcess().MainModule.FileName;
    var resourceFileName = Path.GetFileNameWithoutExtension(programLocation) + ".resources.dll";
    var rootDir = new DirectoryInfo(Path.GetDirectoryName(programLocation));
    return from c in CultureInfo.GetCultures(CultureTypes.AllCultures)
           join d in rootDir.GetDirectories() on c.IetfLanguageTag equals d.Name
           where d.GetFiles(resourceFileName).Any()
           select c;
}
Thomas Levesque