Click here to Skip to main content
Click here to Skip to main content
Go to top

.NET - Localization using Resource file

, 15 Nov 2003
Rate this:
Please Sign up or sign in to vote.
This article explains and demonstrates various ways to implement localization in your .NET application.

Introduction

The .NET framework provides support for localization. Localization deals with customizing data and resources for specific culture or locale or language. Supporting localization is important for an application if the application is required to be customizable based on the respective culture.

Here I am going to talk about two ways of implementing and accessing resources that you can use and access from the current running assembly. The resource files should be added and administered from outside the scope of the running assembly and should not form a part of the assembly.

This approach is helpful because the resource file is generated outside the building process of an application. It provides flexibility in terms of externally creating the language-specific resource, and adding it to the executing assembly without compiling the current project.

The Approach

The two approaches are:

  1. Creating a satellite assembly for culture specific resource file and using it in the executing assembly
  2. Creating a file based resource manager which enables accessing and reading a resource file from a location outside the assembly manifest

Let us now see these two approaches in detail.

  1. Creating a satellite assembly for culture specific resource file and using it in the executing assembly

    The .NET framework uses the hub and spoke model for packaging and deploying resources. The hub is the main assembly that contains the executable code, and the resource for a single culture. This culture, called as neutral or default is the fallback culture for the application. Each spoke connects to a satellite assembly that contains the resource for a single culture, but does not contain any executable code.

    Steps for creating a satellite assembly to include the resource file in your application:

    1. Create a default resource file at the root level of the application.

      Let’s say, MyApplication is the application name and MyResource.resx is the default resource. MyResource.resx is called a default resource because this is the resource file that .NET runtime will look for, if there are no language-specific resource files available.

      To create the above mentioned resource file using Visual Studio .NET, highlight a project and add a new item of type "assembly resource". Name the assembly resource as MyResource.resx. This will create a resource file with .resx extension.

    2. Create satellite assembly outside the project manifest.

      I will explain how to create a satellite assembly for UK English culture (culture name:en-GB), in the following steps:

      1. Create a resource file and name this resource file as MyResource.en-GB.resx.

        Run resource generator utility on this resource file by using the Visual Studio .NET command prompt. The command for generating a .resources file is:

        Resgen MyResource.en-GB.resx

        This will create a binary version of the above resource file named, MyResource.en-GB.resources.

      2. Run the AL (Assembly Linker) utility to generate a satellite assembly from the resource file generated in the previous step (Step a). The command for generating a satellite assembly looks like as below:
        Al.exe
        /t:lib
        /embed: MyResource.en-GB.resources,
        MyApplication.MyResource.en-GB.resources
        /culture:en-GB
        /out:MyApplication.resources.dll

        The explanation of the above command line is as below:

        /t:lib: indicates that you intend to create a .dll.

        /embed:MyResource.en-GB.resources,MyApplication. MyResource.en-GB.resources: Embeds and renames the resource to a name that matches the naming structure of Visual Studio IDE.

        /culture:en-GB: Indicates the intended culture

        /out:MyApplication.resources.dll: Indicates the name of the output DLL. The resultant DLL (MyApplication.resources.dll) should have the same naming convention to locate it.

      3. Place the created satellite assembly in the en-GB folder inside the bin directory of your application directory.

        The directory structure should look like as below:

        \MyApplication\bin\en-GB\MyApplication.resources.dll

        Note that you may need to create the en-GB folder inside the bin directory. You can create as many satellite assemblies as you like and place them in the bin folder of your application, under respective culture-specific directories.

    Now that we have created a satellite assembly and copied it in the bin folder of the project root directory, we are ready to use this assembly to read the resource text from the resource file.

    Following namespaces will be required for the code snippet written below:

    using System.Reflection;
    using System.Resources;
    using System.Globalization;

    To read from the resource file, we need to create an instance of the ResourceManager with the default resource name and current executing assembly, as shown below:

    ResourceManager resmgr = new ResourceManager("MyApplication.MyResource ", 
                                  Assembly.GetExecutingAssembly());

    Note that MyApplication is the name of the application and MyResource is the base name of the resource file.

    To read a value from resource file, GetString method of the ResourceManager is used, as shown below:

    string msg = resmgr.GetString(messageId,ci);

    Where,

    • messageId is the name of the key for the intended value in the resource file.

    • ci is the CultureInfo indicating the intended culture specific resource.

    Let's assume we need to read the UK specific (en-GB) resource file for which we have created a satellite assembly and placed it in the appropriate directory. The CultureInfo can be instantiated as below:

    CultureInfo ci = new CultureInfo("en-GB");

    If the runtime fails to load or locate this culture specific resource, it will read the intended resource text from the default resource (MyResource.resx) as discussed in Step 1 above.

  2. Creating a file based resource manager which enables accessing and reading from a resource file from a location outside the assembly manifest

    This approach is helpful when we need to keep the resource files at a separate location, isolated from the project manifest, and still want access to the culture specific resources from the current assembly.

    Steps to include resource file in your application from outside the project manifest:

    1. Create a default resource file using Visual Studio .NET IDE.

      Let’s say, MyResource.resx is the default resource name. MyResource.resx is called a default resource because it is this resource file that .NET runtime will look for, if there are no language-specific resource files available.

      To create above mentioned resource file using Visual Studio .NET, highlight a project, and add a new item of type “assembly resource”. Name the assembly resource as MyResource.resx. This will create a resource file with .resx extension.

      Similarly, create the resource file for intended culture. Let’s say, you need to create a resource file for UK English culture (culture Name: en-GB). Create this resource using Visual Studio .NET IDE and name it as MyResource.en-GB.resx.

      In the same way you can create other culture specific resource files with same naming convention.

    2. Create .resources file from .resx file created in Step 1 above.

      Run RESGEN utility on this resource file by using Visual Studio .NET command prompt. The command for creating .resources file is as below:

      Resgen MyResource.resx

      This will create a binary format of the resource file named MyResource.resources

      Similarly, create .resources for all culture-specific .resx files.

      For e.g. Resgen MyResource.en-GB.resx will result in MyResource.en-GB.resources file.

      Important: It is very important to follow correct naming convention for creating resource files, as it forms the base for the runtime to locate the correct resource file. These resource files have a specific naming structure as:

      <resource file basename>.<culture>.resources
    3. Keep all .resources files at the same location, e.g. C:\Program Files\ResourceSet

      The path for MyResource.resources will be:

      "C:\Program Files\ResourceSet\MyResource.resources"

      Similarly the path for MyResource.en-GB.resources will also be:

      "C:\Program Files\ResourceSet\MyResource.en-GB.resources"

    Following namespaces will be required for the code snippet written below:

    using System.Resources;
    using System.Globalization;

    To read the key values from the resource files created in the above step, we need to use CreateFileBasedResourceManager method of the ResourceManager, as shown below:

    ResourceManager resourceMgr = 
        ResourceManager.CreateFileBasedResourceManager(resourceName,
        resourcePath,null);

    Where,

    • resourceName is the base name of resource file. In this case, it is MyResource
    • resourcePath is the path of the resource repository from where culture specific resource files are to be picked. In this case, it is C:\Program Files\ResourceSet
    • null indicates that the default runtime ResourceSet is used.

    To read a value from resource file, we will use GetString method of the ResourceManager as below:

    string msg = resourceMgr.GetString(messageId,ci);

    Where,

    • messageId is the name of the key for the intended value in the resource file
    • ci is the CultureInfo indicating the intended culture specific resource.

    Let us assume we need to read the UK specific (en-GB) resource file for which we have created a resource file named MyResource.en-GB.resources. The CultureInfo can be instantiated as below:

    CultureInfo ci = new CultureInfo("en-GB");

    If the runtime fails to load or locate this culture specific resource, it will read the intended resource text from the default resource (MyResource.resources) as discussed in Step 1 and 2 above.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Prakash Kumar Singh
Web Developer
United Kingdom United Kingdom
Hi!!
I am working for Wipro Technologies, Bangalore,India.I am basically into design and development of enterprise software application,primarily on .Net technologies.
 
I am also Microsoft Certified Solutions Developer(MCSD)for .Net
 
I have been hung on this site for quite some time now, and found it extremely useful for anyone who is seeking basic to advance level info about .Net(C# in particular),XML,MFC & so on.
 


Comments and Discussions

 
GeneralSuper! Short and full. Thanks PinmemberIgor Sokolov2-May-14 7:39 
Questioneasy way to localize .resx files PinmemberDo Xa9-Jan-14 20:52 
QuestionMy Vote of 5 PinmemberbharatRajM165878-Apr-13 1:16 
QuestionAwesome perfect Pinmembersofygrafe3-Dec-12 15:11 
GeneralMy vote of 5 PinmemberMember 81358325-Jun-12 23:25 
superb article, the way it explains using example is great.....
QuestionExactly what I was looking for! PinmemberNaerling17-May-12 8:16 
GeneralMy vote of 5 Pinmemberlukeer25-Jan-12 2:36 
GeneralMy vote of 5 PinmemberDiMann22-Sep-11 1:13 
GeneralMy vote of 5 PinmemberAdam Roderick J6-Jul-11 23:33 
GeneralBaseName for Resource File is a property Pinmemberkvanpal14-Jun-11 11:09 
GeneralMy vote of 5 PinmemberBaesky10-Apr-11 19:08 
QuestionAssembly Linker Error PinmemberRichik Sinha Roy17-Sep-09 0:26 
AnswerRe: Assembly Linker Error PingroupSkyhawk11-Jan-13 3:29 
GeneralLocalization of data read from database PinmemberManjuHanzan8-Apr-09 0:37 
GeneralI suggest to use Microsoft's default approach Pinmemberdjdane8-Mar-09 14:54 
GeneralRe: I suggest to use Microsoft's default approach PinmemberYaroslav Trofimov15-Apr-09 2:05 
GeneralError when publishing my website with satellite assemblies PinmemberSydBarrett24-Feb-09 9:46 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko24-Sep-08 2:03 
GeneralLinking dll not working PinmemberRupesh R. Tarwade6-Jul-08 19:30 
Questionerror CS0234: when compiling Pinmembervalmir19692-Jul-08 4:11 
GeneralLinker wouldn't work under VS2005 until I removed the rename value Pinmemberkmillen8-May-08 10:37 
GeneralApproach A not working Pinmemberanni317-Apr-08 0:13 
GeneralIssue using Satellite Assembly [modified] PinmemberRuchi.Shivnani8-Apr-08 20:25 
GeneralIssue using Satellite Assembly PinmemberRuchi.Shivnani7-May-08 21:06 
GeneralRe: Issue using Satellite Assembly Pinmemberyoyoyo00923-Sep-08 20:55 
GeneralRe: Issue using Satellite Assembly PinmemberYaroslav Trofimov15-Apr-09 2:11 
GeneralRe: Issue using Satellite Assembly PinmemberLenin.N.C16-Jun-10 23:08 
Questionlocalization in .NET Pinmemberkiran_veernapu26-Dec-07 7:43 
AnswerRe: localization in .NET Pinmemberrichardmoss27-May-08 0:27 
QuestionLocalization Problem Pinmemberengwafaa28-Nov-07 2:49 
GeneralSatellite Assembly not giving correct output Pinmembertprashanth27-Nov-07 23:55 
GeneralRe: Satellite Assembly not giving correct output PinmemberAsterix868-Mar-09 3:50 
QuestionLocalize Combo Box Dropdown PinmemberStudent119-Oct-07 21:33 
AnswerRe: Localize Combo Box Dropdown PinmemberJohn Jermanis10-Oct-07 13:37 
GeneralSPACE error PinmemberKirill Metrik23-Aug-07 22:33 
GeneralRe: SPACE error PinmemberDoubleService3-Feb-10 5:58 
QuestionCulture of the default resource file? Pinmembergavthegenius15-Aug-07 3:26 
AnswerRe: Culture of the default resource file? PinmemberVyas Bharghava15-Aug-07 22:28 
AnswerRe: Culture of the default resource file? PinmemberMatt Clarkson7-Oct-09 4:58 
GeneralSatelite dll empty when view in ildasm Pinmemberhndavis120-Jul-07 4:40 
QuestionNot reading label Pinmemberib000428-Jun-07 0:45 
QuestionHELP, Linker Error! PinmemberNeuromancer_11-Mar-07 10:21 
AnswerRe: HELP, Linker Error! [modified] Pinmembernussknacker25-Apr-07 13:03 
GeneralNice article! PinmemberNilesh K.6-Mar-07 21:44 
NewsFree, translation friendly resource editor Pinmemberpapadi5-Mar-07 2:38 
Generalgood article PinmemberJuraj Borza21-Jan-07 20:05 
Generalnice article [modified] Pinmemberabdulmalikm7-Nov-06 23:00 
Questionlinking multiple resource files into one dll Pinmemberzsepi2-Jun-06 4:24 
AnswerRe: linking multiple resource files into one dll PinmemberJeff Hoover17-Jan-07 8:52 
GeneralPerformance while reading resource files Pinmemberta191028-May-06 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 16 Nov 2003
Article Copyright 2003 by Prakash Kumar Singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid