Search code examples
c#asp.netlocalizationglobalization

ASP.NET Globalization Resources


I have a class library project with .resx resources for localized strings. Let's say the project has two files:

Strings.resx
Strings.en-GB.resx

How can I enumerate which cultures (default "en-US" in Strings.resx and "en-GB" in Strings.en-GB) are available?

In other words, I want to get an IEnumerable that returns "en-US" and "en-GB".

Note: WITHOUT reading .resx files from the file system. :)


Solution

  • If you build your application with embedded resx files, it will typically generate satellite assemblies in subfolders of your build output directory. The subfolder names will correspond to the culture specifier (e.g. "en-GB").

    The main resx files will be embedded in the main assembly. You could use the NeutralResourcesLanguage attribute to indicate what culture you've used in these resources:

    [assembly: NeutralResourcesLanguage("en-US")]
    

    At runtime you can then:

    • Retrieve the NeutralResourcesLanguage attribute to get the culture of the neutral resources.

    • Enumerate the subdirectories of the folder containing your main assembly to locate the satellite resources.