Localization / Globlization on the fly

Have you ever wondered about creating and editing .resx resources files at runtime ?
Let’s say you build an application supporting globalization/localization and the values of some of your items is not known upfront. In this case, we’ll assume that you allow the end-user to create his own resources entries that will be used at run time to generate resources files on the fly.

The .NET Framework provides very useful APIs for this matter. You can use the System.Resources namespace, which provides classes and interfaces that allow developers to create, store and manage various culture-specific resources used in an application.

Here are some of the most useful classes of that namespace:
ResourceManager: it allows the user to access and control resources stored in the main assembly or in resources satellite assemblies.
ResXResourceWriter: writes resources in an XML resource (.resx) file or an output stream.
ResXResourceReader: Enumerates XML resource (.resx) files and streams, and reads the sequential resource name and value pairs.

using System;
using System.Resources;
using System.Collections;

class ReadResXResources
{
   public static void Main()
   {

      // Create a ResXResourceReader for the file items.resx.
      ResXResourceReader rsxr = new ResXResourceReader("items.resx");

      // Iterate through the resources and display the contents to the console.
      foreach (DictionaryEntry d in rsxr)
      {
    Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
      }

     //Close the reader.
     rsxr.Close();
   }
}

Have fun on the .NET stack.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: