Friday, November 10, 2006

Reading resource files from dotnet application

Overview:

This document is useful for creating multilingual application using dot net. Language and time change accordingly to area and geographic difference. This document will help you to create application which will take care of language difference and guide you to create multilingual application.


Introduction:
There are multiple approaches to develop multilingual application like:
1) Create multiple pages for different languages and display based on language setting.
2) Create multiple database tables for different languages and fetch data accordingly.
3) Create resource files for different languages.
This will have a better performance compared to fetching from the database.
Third approach will have a better performance compared to others approaches and easy to implement also. To implements this application requires a specific setting and knowledge about Globalization, Localization and culture.
Globalization is a process of identifying all the parts of your application that need to be different for respective languages and separate them from the core application.
Localization is process of creating and configuring your application for a specific language.
A culture is the combination of the language that you speak and the geographical location you belong to. It also includes the way you represent dates, times and currencies. A culture is represented as shown in the following example:

es-CO - For the culture determined by the Spanish in Colombia
fr-FR - For the culture determined by the French in France
fr-CA - For the culture determined by the French in Canada
en-US - culture represents English language being spoken in US
en - For the culture determined by the English in General
Source Code:
Create application to read resource file using C#.net windows or web application.
Follow the simple four steps to create application.
1) Set global environment language setting in web.config/app/config file.
For example:
<add key="GlobalEnvironmentLanguage" value="en"></add>
2) Create one .cs file (for example: LeadMgmtResourceFile.cs)
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Resources;
using System.Configuration;
namespace TEST.LeadMgmt
{
[Serializable]
public class LeadMgmtResourceFile
{
private CultureInfo culture;
public string GetResourceError(string errorId)
{
string resourceCulture = ConfigurationManager.AppSettings.Get("GlobalEnvironmentLanguage");
try
{
if (resourceCulture == string.Empty resourceCulture == null)
{
return "Language culture not mentioned in the config file.";
}
culture = CultureInfo.CreateSpecificCulture(resourceCulture);
if (culture.ToString() == "es-CO")
{
culture = CultureInfo.CreateSpecificCulture("es-CO");
}
else if (culture.ToString() == "fr-FR")
{
culture = CultureInfo.CreateSpecificCulture("fr-FR");
}
else if (culture.ToString() == "fr-CA")
{
culture = CultureInfo.CreateSpecificCulture("fr-CA");
}
else
{
culture = CultureInfo.CreateSpecificCulture("en");
}
ResourceManager rm = new ResourceManager("LeadMgmtUtility” + ResourceFileName , typeof(LeadMgmtResourceFile).Assembly);
return rm.GetString(errorId, culture);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
LeadMgmtUtility :
This is project name where LeadMgmtResourceFile.cs file exist
ResourceFileName :
This is Resource file for different -2 language
1) LeadMgmtResource.es-CO.resx
2) LeadMgmtResource.fr-CA.resx
3) LeadMgmtResource.fr-FR.resx
4) LeadMgmtResource.en.resx
LeadMgmtResourceFile :
This is class name where GetResourceError() exist to read resource file
3) Create resource files for each language like:
<base file name>.<locale>.resx
a. LeadMgmtResource.es-CO.resx
b. LeadMgmtResource.fr-CA.resx
c. LeadMgmtResource.fr-FR.resx
d. LeadMgmtResource.en.resx
The way to store error in resource file for example data store in the “LeadMgmtResource.en.resx” file is:
Name Value Comments
E01001 Invalid user name in English Use Case 01 Error Number 001
E01002 Node not present in XML file. Use Case 01 Error Number 002
4) Call data stored in the resource file.
// Create the object of resource file class.
LeadMgmtResourceFile objResourceError = new LeadMgmtResourceFile();
//Value from the Resource file for a particular error code
logResponse = objResourceError.GetResourceError(resource_ErrorId).Trim();


Ritesh Kesharwani
Infosys , Pune

1 comment:

Ritesh_Kesharwani said...

To Get the Resource file value in simple way.

using System.Resources;

ResourceManager rm = new ResourceManager("ProjectName.Resource1", typeof(Form1).Assembly);

string ResourceValue = rm.GetString("ResourceName");

Note: Resource1.resx file should be in the project root folder.