Introducing String Resource Generator

I’m currently using a .resx file at my projects to contain my friendly client’s messages. I’m reading from this file via ResourceManager class which allows me to supply consistent messages at my GUI layer, e.g for every successful delete in my application you’ll get the same message format “The {0} was deleted successfully” – in my “Workers” form you’ll see The worker was deleted successfully and in my “Cars” form you’ll see The car was deleted successfully;


I’ve encounter the SR (String Resource) Generator a week ago and I thought to give it a try. So, what is SR generator ?


” This custom tool allows the generation of a resource class (and optionally .resx file) from either a .resx file, or the Microsoft SR.strings file.


I think that a simple example will show the tool’s capabilities.



  1. I’ve downloaded the source which also includes the .msi file and installed it.
  2. I’ve added a file named SR.strings to my web application:

             SRGSolutionOnAdd.gif


  3. I’ve used the “Custom Tool” option and call SRCodeGen – this is an EXE which will parse your .strings file.
             SRGFileProperties.gif

     

  4. Next I’ve edit the SR.strings file –       

      [strings]
      Worker = Worker
      ItemDeleted(string itemName) = The {0} was deleted successfully.
      ItemAdded(int itemId, string itemName) = The {1} was added successfully, it’s new ID is: {0}.


      [strings.he]
      Worker = עובד
      ItemDeleted(string itemName) = ה{0} נמחק בהצלחה.
      ItemAdded(int itemId, string itemName) = ה{1} נוסף בהצלחה, מספרו החדש {0}.


  5. I’ve saved the file in UTF-8 format, for Hebrew support.
             SRGFileEncoding.gif

     

  6. Rebuild the web project, this will cause the SRCodeGen to generate required files:

  7.                   SRGGeneratedFiles.gif

  8. Now lets use the SR class which was generated for us, here is my usage of it 

  9.          PageMessage.Text = SR.ItemDeleted(SR.Worker); // In my Worker.Delete()

     

             OR

             

             PageMessage.Text = SR.ItemAdded(newId, SR.Worker); // In my Worker.Add()

     

  10. Finally we can select the SR.Culture we want to use, so the messages will be formated in the correct language. This can be done easily be calling:

                  SR.Culture = new System.Globalization.CultureInfo(“he”);

   

                  OR

 

                  SR.Culture = new System.Globalization.CultureInfo(“en”);

         You can put this code in your Global.asax.cs in Application_Start method or 
         change it at any time, according to your needs.


 


This tool is a MUST in my opinion for two main reasons:



  1. IntelliSense, IntelliSense, IntelliSense ! (don’t you love it ?!?!).

  2. Changing the messages Culture requires only one line of code, simple as that.