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

前端 未结 7 536
青春惊慌失措
青春惊慌失措 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:34

    You can programatically list the cultures available in your application

    // 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.");
        }
    }
    

提交回复
热议问题