Get all available cultures from a .resx file group

耗尽温柔 提交于 2019-12-06 00:26:35

问题


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?


回答1:


Look for satellite assemblies in your application's directory: for each subdirectory, check if its 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.EnumerateDirectories() on c.IetfLanguageTag equals d.Name
           where d.EnumerateFiles(resourceFileName).Any()
           select c;
}



回答2:


based on the answer by @hans-holzbart at Programmatic way to get all the available languages (in satellite assemblies) but fixed to not return the InvariantCulture too and wrapped into 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;
}

using that 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;
}


来源:https://stackoverflow.com/questions/3226974/get-all-available-cultures-from-a-resx-file-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!