Dynamically creating available localizations from a resource file

I have an MVC website that uses .resx

files for localization
. However, the current behavior to detect localization support requires iterating over the physical files .resx

, and they do not exist once the site has been compiled for publication.

Currently the folder structure is:

  • Language resources
    • Resource.en-US.resx
    • Resource.fr-CA.resx
    • Resource.hi.resx
    • Resource.resx

Trying to list all resource files through GetManifestResourceNames()

, according to this answer , creates only one file LanguageResources.Resource.resources

, which represents the master, a non-localized list. It may or may not have localizations built into it, but I haven't found that.

How can I tell at runtime that I support three languages?

<sub> The ultimate goal is to create a dropdown list based on these three values. If there is another way I should approach this problem, the answers whose addresses are also acceptable.

+3


source to share


1 answer


Following the sidebar links for my question, I eventually found this question , and Hans Holzbart has a great answer , which I reproduce below. The question itself approaches it from a different direction, but the answer is equally applicable to my situation.

// Pass the class name of your resources as a parameter e.g. MyResources for MyResources.resx
ResourceManager rm = new ResourceManager(typeof(MyResources));

CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo culture in cultures)
{
    try
    {
        ResourceSet rs = rm.GetResourceSet(culture, true, false);
        // or ResourceSet rs = rm.GetResourceSet(new CultureInfo(culture.TwoLetterISOLanguageName), true, false);
        string isSupported = (rs == null) ? " is not supported" : " is supported";
        Console.WriteLine(culture + isSupported);
    }
    catch (CultureNotFoundException exc)
    {
        Console.WriteLine(culture + " is not available on the machine or is an invalid culture identifier.");
    }
}

      



For my purposes, I had to filter out invariant culture with using .Where(x => x.Name != "")

, but other than that it worked great.

0


source







All Articles