Programmatic way to get all the available languages (in satellite assemblies)

前端 未结 7 516
青春惊慌失措
青春惊慌失措 2020-11-28 08:11

I\'m designing a multilingual application using .resx files.

I have a few files like GlobalStrings.resx, GlobalStrings.es.resx, GlobalStrings.en.resx, etc. When I wa

7条回答
  •  离开以前
    2020-11-28 08:29

    @"Ankush Madankar" presents an interesting starting point but it has two problems: 1) Finds also resource folders for resources of refrenced assemblies 2) Doesn find the resource for the base assembly language

    I won't try to solve issue 2) but for issue 1) the code should be

    public List GetSupportedCultures()
    {
        CultureInfo[] culture = CultureInfo.GetCultures(CultureTypes.AllCultures);
    
        // get the assembly
        Assembly assembly = Assembly.GetExecutingAssembly();
    
        //Find the location of the assembly
        string assemblyLocation =
            Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(assembly.CodeBase).Path));
    
        //Find the file anme of the assembly
        string resourceFilename = Path.GetFileNameWithoutExtension(assembly.Location) + ".resources.dll";
    
        //Return all culture for which satellite folder found with culture code.
        return culture.Where(cultureInfo =>
            assemblyLocation != null &&
            Directory.Exists(Path.Combine(assemblyLocation, cultureInfo.Name)) &&
            File.Exists(Path.Combine(assemblyLocation, cultureInfo.Name, resourceFilename))
        ).ToList();
    }
    

提交回复
热议问题