Get all available cultures from the .resx file group.

I need to program a list of available cultures in the resx file group, but the ResourceManager class does not seem to help.

I may have:

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

etc.

However, how can I find these three (or how many) crops at runtime?

+6
resources internationalization localization resx
source share
3 answers

Look at the satellite assembly in the application directory: for each subdirectory, check if its name matches the name of the culture, and if it contains the .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.EnumerateDirectories() on c.IetfLanguageTag equals d.Name where d.EnumerateFiles(resourceFileName).Any() select c; } 
+7
source share

There is a more elegant approach described here.

+3
source share

based on @ hans-holzbart's answer to the Programmatic way to get all available languages โ€‹โ€‹(in assembly builds) , but fixed so as not to return an InvariantCulture and wrapped in a reusable method:

 public static IEnumerable<CultureInfo> GetAvailableCultures() { List<CultureInfo> result = new List<CultureInfo>(); ResourceManager rm = new ResourceManager(typeof(Resources)); CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); foreach (CultureInfo culture in cultures) { try { if (culture.Equals(CultureInfo.InvariantCulture)) continue; //do not use "==", won't work ResourceSet rs = rm.GetResourceSet(culture, true, false); if (rs != null) result.Add(culture); } catch (CultureNotFoundException) { //NOP } } return result; } 

with this method you can get a list of strings to add to some ComboBox with the following:

 public static ObservableCollection<string> GetAvailableLanguages() { var languages = new ObservableCollection<string>(); var cultures = GetAvailableCultures(); foreach (CultureInfo culture in cultures) languages.Add(culture.NativeName + " (" + culture.EnglishName + " [" + culture.TwoLetterISOLanguageName + "])"); return languages; } 
+2
source share

All Articles