Implementing Multilingual Applications in Microsoft .NET
page 6 of 12
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 53434/ 79

Working with Embedded Resource Files

An embedded resource is one that is embedded inside the application's code.  Thus, when the application is compiled, this resource gets stored in the assembly that is generated after the compilation of the application's code.  Let us create a text resource file called TextFile.txt and make the same an embedded resource by selecting the properties of the same file and then setting the Build Action in the item properties to "Embedded Resource."  This means the resource will be embedded directly into the current assembly along with the code.  Further, let the resource file that has just been created contain a text message.

The following code snippet shows how we can retrieve the content of the resource as a string from the current executing assembly.

Listing 6

public string GetResourceFromAssembly(string resourceName) 
{
  Assembly assembly = Assembly.GetExecutingAssembly();
  TextReader txtReader = new
  StreamReader(assembly.GetManifestResourceStream(resourceName));
  string str = txtReader.ReadToEnd();
  txtReader.Close();
  return str;
}

The above method accepts the fully qualified path to the resource as a parameter and returns the resource as a string from the current executing assembly.  Invoke the above method as shown below.

Listing 7

Response.Write(GetResourceFromAssembly("Test.TextFile.txt"));

The GetResourceFromAssembly method would return the resource value from the specified resource file.

It is also possible to list all the embedded resources in the assembly as shown in the section below.  The following method shows how we can display the names of all the embedded resources in the current executing assembly.

Listing 8

public string GetResourceNamesFromAssembly()
{
   Assembly assembly = Assembly.GetExecutingAssembly();
   string [] resourceNames = assembly.GetManifestResourceNames(); 
   StringBuilder stringBuilder = new StringBuilder();
   foreach(string str in resourceNames)
   {
    stringBuilder.Append(str);
    stringBuilder.Append("<BR>");
   }
  return stringBuilder.ToString();
}

The above method would return all the resource names from the current executing assembly. Invoke the above method as shown in the listing below.

Listing 9

Response.Write(GetResourceNamesFromAssembly());

View Entire Article

User Comments

Title: What about localized Databases?   
Name: Rojalin
Date: 2006-09-28 6:54:50 AM
Comment:
many many tx for this article
Title: Implementing Multilingual Applications in Microsoft .NET   
Name: rambabu
Date: 2006-09-12 7:22:14 AM
Comment:
The article is very nice and the author explained in simple terms to understand the various aspects of Globailization and Localization.I liked this article very much.
Title: What about localized Databases?   
Name: Max
Date: 2006-09-12 4:58:25 AM
Comment:
Hi,
thank you very much for your article!
I have always been looking around for a standardized way of localizing db-entries. For instance if I run a web-shop which must be available to german and english customers my descriptions of my articles must be in german and english.
Of course this has to be stored in a DB and not in embedded resources.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 8:42:03 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search