How to find which resource cultures are available in the ResourceManager?

I have a situation where I would like to present a list of "available languages" for my application (which, incidentally, is an ASP.NET MVC 3 application, if that makes any odds). I thought I could somehow automatically get this list, since it should only be resx files that are included in the assembly (I do not need to support English UK, German Austria or anything else, only in English or German), and I came up with a circuit that I will give below (implemented as a singleton, as this is a slightly intensive approach).

The problem is that on some machines it returns “Arabic”, although I do not have such a resource, and on mine (since I installed VS 2012) it returns all of them (this makes more sense to me than returning only two real ones) culture plus Arabic, but it seems that the ResourceManager was simply not intended to allow me to get this information, so I probably shouldn't complain). Here is a diagram ...

(I have a file Strings.resx and Strings.de.resx)

IEnumerable<CultureInfo> cultures =
    CultureInfo.GetCultures(CultureTypes.NeutralCultures)
        .Where(c =>
                   {
                       // Exclude the invariant culture and then load up
                       // an arbitrary string so the resource manager
                       // loads a resource set, then get the set for the
                       // current culture specifically and it is, sometimes
                       // (I thought always but I was wrong) null if no
                       // set exists
                       if (c.LCID == CultureInfo.InvariantCulture.LCID)
                           return false;

                       var rm = Strings.ResourceManager;
                       rm.GetString("HELLO", c);
                       return rm.GetResourceSet(c, false, false) != null;
                   });

So then, I thought that I could do this based on whether a language-specific directory exists:

var neutralCulture = new[]
{
    CultureInfo
        .CreateSpecificCulture(((NeutralResourcesLanguageAttribute)
                                Assembly
                                    .GetExecutingAssembly()
                                    .GetCustomAttributes(
                                        typeof (NeutralResourcesLanguageAttribute),
                                        false)[0])
                                .CultureName)
};

IEnumerable<CultureInfo> cultures =
    CultureInfo.GetCultures(CultureTypes.NeutralCultures)
        .Where(c => Directory.Exists(c.TwoLetterISOLanguageName))
        .Union(neutralCulture);

"" ( ), , , , -, - . , , ( , ), ()...

, , , , (: )?

+5
2

. , ( ) , , .

, , . , , , . ( ).

, .NET Framework, .

+2

.

, . , . .

internal static class ThisAssembly
{
    static readonly string[] SupportedCultures = new string[] { "en-US", "de-DE", };
}

:

  • : ;
  • ResourceManager._resourceSets: .

.

0

All Articles