Virtual Developer Workshop: Containerized Development with Docker
While looking for the use of satellite assemblies, I came across many examples. But, when I tried to run them, most of them did not fulfill my expectations from a satellite assembly.
In this article, you'll see what satellite assemblies are, how to create them, and how to use satellite assemblies with strong named assemblies with very simple and small pieces of code chunks.
What Is a Satellite Assembly?
A definition from MSDN says something like this: "A .NET Framework assembly containing resources specific to a given language. Using satellite assemblies, you can place the resources for different languages in different assemblies, and the correct assembly is loaded into memory only if the user elects to view the application in that language."
This means that you develop your application in a default language and add flexibility to react with change in the locale. Say, for example, you developed your application in an en-US locale. Now, your application has multilingual support. When you deploy your code in, say, India, you want to show labels, messages shown in the national language which is other than English.
Satellite assemblies give this flexibility. You create any simple text file with translated strings, create resources, and put them into the bin\debug folder. That's it. The next time, your code will read the CurrentCulture property of the current thread and accordingly load the appropriate resource.
This is called the hub and spoke model. It requires that you place resources in specific locations so that they can be located and used easily. If you do not compile and name resources as expected, or if you do not place them in the correct locations, the common language runtime will not be able to locate them. As a result, the runtime uses the default resource set.
Creating a Satellite Assembly
- Create a folder with a specific culture name (for example, en-US) in the application's bin\debug folder.
- Create a .resx file in that folder. Place all translated strings into it.
- Create a .resources file by using the following command from the .NET command prompt. (localizationsample is the name of the application namespace. If your application uses a nested namespace structure like MyApp.YourApp.MyName.YourName as the type of namespace, just use the uppermost namespace for creating resources files—MyApp.)
- In the code, find the user's language; for example, en-US. This is culture specific.
- Give the assembly name as the name of .resx file. In this case, it is Strings.
resgen Strings.en-US.resx LocalizationSample. Strings.en-US.resources al /embed:LocalizationSample.Strings.en-US.resources /out:LocalizationSample.resources.dll /c:en-US
The above step will create two files, LocalizationSample.Strings.en-US.resources and LocalizationSample.resources.dll. Here, LocalizationSample is the name space of the application.
Using a Satellite Assembly
Follow these steps:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(specCult); Thread.CurrentThread.CurrentUICulture = new CultureInfo(specCult); ResourceManager resMgr = new ResourceManager(typeof(Form1).Namespace + "." + asmName, this.GetType().Assembly); btnTest.Text = resMgr.GetString("Jayant");
That's it. See how simple is it to create a satellite assembly and use it in your code.
Here's how to use a satellite assembly if your assembly is a strong named assembly. When you create an assembly with a string name, all the assemblies it refers to must have a strong name. This is true with a satellite assembly also. Here are the steps to create a strong named satellite assembly.
String Naming a Satellite Assembly
al /embed:ExploreDotNet2005.Strings.en-US.resources /out:ExploreDotNet2005.resources.dll /c:en-US /template:../ExploreDotNet2005.exe /keyfile:../../.. /KeyPair.snk
Remember that /template is very important because it inherits the parent assembly manifest, and the strong name key pair must be the same as that for the running assembly. I've tried using different strong names for satellite assembly and executing the assembly. but it throws an exception.
That's it, friends. In case of any queries, let me know.