rosko
rosko

Reputation: 474

Load multiple resource files into one resource manager

I'm searching for a way to make one resource manager that holds all the data form multiple resource files. Is this even possible? It would be useful for me if yes because I've got like 10+ resources files with translations. I want to make one wrapper class for this and make one resource manager so if I e.g. use rm.GetString("string"); I get this value from one of resource files. I probably think that this is not the best idea but... if you have any good ideas please share here!

I'm trying the following code:

var rm = new ResourceManager("ProjectNameSpace.ResourceName",
    Assembly.GetExecutingAssembly());

By doing this I only load resources from file that I specified by: ProjectNameSpace.ResourceName, am I right?

Is there any nice workaround for this or different approach?

Upvotes: 3

Views: 2926

Answers (1)

RenniePet
RenniePet

Reputation: 11658

This is not exactly what you are asking about, but maybe it will help. This is some cut-down copy-and-paste code from a program I use that reads multiple resource files and creates a combined Dictionary of all of the icons in the resource files.

   class Program
   {
      static void Main(string[] args)
      {
         IconManager.FindIconsInResources(Resources1.ResourceManager);
         //IconManager.FindIconsInResources(Resources2.ResourceManager);
         //IconManager.FindIconsInResources(Resources3.ResourceManager);

         Image iconImage = IconManager.GetIcon("Incors_office_building_16x16");
      }
   }


   public static class IconManager
   {
      private static readonly Dictionary<string, ResourceSet> _iconDictionary = 
                                                             new Dictionary<string, ResourceSet>();

      /// <summary>
      /// Method to read the resources info for an assembly and find all of the icons and add them 
      /// to the icon collection.
      /// </summary>
      public static void FindIconsInResources(ResourceManager resourceManager)
      {
         // Get access to the resources (culture shouldn't matter, but has to be specified)
         ResourceSet resourceSet = 
                   resourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-us"), true, true);
         if (resourceSet == null)
            throw new Exception("Unable to create ResourceSet.");

         // Top of loop to examine each resource object 
         foreach (DictionaryEntry dictionaryEntry in resourceSet)
         {
            // Check it's an icon (or some kind of graphic)
            if (!(dictionaryEntry.Value is Bitmap))
               continue;

            // Get the resource name, which is basically the filename without ".png" and with '-' 
            //  and blanks converted to '_'. Ignore .ico files.
            string resourceKey = (string)dictionaryEntry.Key;
            if (resourceKey.EndsWith("_ico", StringComparison.Ordinal))
               continue;

            // Add (or replace) the icon in the icon dictionary
            _iconDictionary[resourceKey] = resourceSet;
         }
      }


      /// <summary>
      /// Method to get an icon image from one of several resource files.
      /// </summary>
      public static Image GetIcon(string iconName)
      {
         ResourceSet resourceSet;
         _iconDictionary.TryGetValue(iconName, out resourceSet);
         if (resourceSet == null)
            return null;

         return (Image)resourceSet.GetObject(iconName);
      }
   }

Upvotes: 1

Related Questions