Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

Developing multilingual applications in ASP.NET (Part - III)

Introduction

The previous article of this series explained how to read resources stored in a resource file (.resources). Though that approach works in many situations one drawback was that you need to provide physical directory path to load the resource file from. This means you should know where the resource is located. Wouldn't it be more elegant if you can load resources dynamically based on current culture of your application? That is what Satellite Assemblies do. Rest of this article is going to show you how to use Satellite Assemblies to achieve this.

What are satellite assemblies?

As I mentioned in Part-I, Satellite Assemblies are compiled DLLs that contain only resource data and no other code. Satellite assemblies can be created with the help of AL utility provided with .NET SDK or you can use VS.NET for creating them. They can be deployed at later stage after main application has been deployed. They are ideal for product based development where in companies can come up with additional language support at later date.

How to read resources from an assembly?

In order to compile your resources in a satellite assembly, you need to follow these steps:
  • Create a resource file (.txt or .resx)
  • Compile resource files (.resource)
  • Create satellite assembly (.dll) using AL command line tool
  • Create folders for storing the satellite assembly
  • Place corresponding satellite assembly in to the folders
  • Access the resources inside the application

How to create Satellite Assemblies using AL utility?

Using AL command line utility you can embed your resources in an assembly. The assembly thus formed is nothing but a Satellite Assembly. Following code shows how this is done:
AL 
/t:lib 
/culture:en-GB 
/embed:mymessage.en-GB.resources 
/out:ResourcesDemo.resources.dll
Here, /t switch tells that the output is of type library (DLL). The culture switch tells that the resources being embedded in the assembly are for en-GB culture. /embed switch specifies the .resources file that is to be embedded in the Satellite Assembly. Finally, /out switch specifies the name of output assembly.

How to create Satellite Assemblies using VS.NET utility?

If you are using VS.NET, you can simply add the resource file to your project (library project) and make sure to set its action property in the property window to Embedded Resource. At compile time VS.NET automatically embeds the resources inside the assembly being generated.

Creating folders for storing the satellite assembly

I mentioned in the beginning that Satellite Assemblies can be dynamically loaded based on current culture of your application. In order that this works as expected, you need to follow a specific folder structure to place Satellite Assemblies. You need to create sub-folder inside the main folder of the application (i.e. where EXE of your application resides or BIN folder for ASP.NET application) with the same name as the culture. For example, if your Satellite Assembly is for culture en-GB then you need to create a folder called en-GB and place the Satellite Assembly there.

Access the resources inside the application

In order to access a resource inside Satellite Assembly, we will again use ResourceManager class. Following code shows how this works:
Dim x As ResourceManager
Dim ci As New CultureInfo("en-GB")
Thread.CurrentThread.CurrentCulture = ci
x = New 
ResourceManager("mymessage.en-GB",
[Assembly].GetExecutingAssembly.GetSatelliteAssembly(ci))
Label1.Text=x.GetString("label1")
Here, we have created an instance of CultureInfo class by passing culture code "en-GB" to its constructor. Then we set this culture as the current culture of running thread. We then create an instance of ResourceManager class by specifying resource base name. The second parameter of ResourceManager constructor is of our interest. We are passing the Satellite Assembly that contains resources for specified culture. We get this Satellite Assembly by GetSatelliteAssembly() method of the executing assembly. Finally, we get culture specific string by calling GetString() method on the resource manager. Note, that we never pass any physical path to any file here as in case of CreateFileBasedResourceManager() method that we saw in Part-II.

Summary

In this article we saw how to create Satellite Assemblies from resource files. Satellite Assemblies simplify deployment of resources as additional resources can be easily deployed at later time. In order to deploy them you need to create sub-folders in the application folder with the same name as the culture.



Bipin Joshi is a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 12 Oct 2003



Tags : ASP.NET Web Forms Visual Studio Components