In order to ensure that we do not require changing the
application source code each time we require to have support for a newer
culture in the application, we can ensure even loose coupling by setting the
culture name in the web.config file. This ensures that the application's
culture settings are configurable. This information can be read by the
application at run time and the appropriate culture settings set accordingly.
The <appSettings> element of the web.config file can
be used to specify the culture name as shown below.
<add key = "Culture" value = "fr-FR">
<add key = "Culture" value = "en-GB">
Note that fr-FR refers to French language that is spoken in France while en-GB refers to English language that is spoken in Great Britain.
The culture type that is specified in the web.config file
can be read by using System.ConfigurationSettings.AppSettings in the source
code. Note that System.Configuration.ConfigurationSettings.AppSettings is a
class in the System.Configuration namespace in the system.dll assembly. The source
code is provided below.
string culture =
ResourceManager resourceManager = new
CultureInfo cultureInfo = new CultureInfo(culture);
string message = resourceManager.GetString("ID",cultureInfo);
In the code listing shown above, an instance of the ResourceManager
class is created by passing the resource and the assembly in which the resource
is embedded as parameters. Here the culture and a reference of the current
executing assembly are passed to it as parameters. Then an object of the
CultureInfo class is created and the culture name passed to the parameterized
constructor of the class. The ResourceManager class enables access to the
resources for a particular culture using the GetObject and GetString methods. Now
we have to call the GetString method on the ResourceManager instance and pass
the ID string as key and the instance of the CultureInfo class as parameters. The
resource value is returned as string and the same can now be used as needed in
the application. For the sake of simplicity, I have displayed the message
using Response.Write method. This concept can be used to set the text of a
particular control in a specific language by reading the locale specific text
from the resource. The current locale is specified in the web.config file.
As an example, to set the copyright on a label control in
the web from use the code provided below.
lblCopyright.Text = resourceManager.GetString("Copyright",culture);
Here the copyright message would be displayed in the label
control based on the current culture that is set using the web.config file as
Note that either the satellite assembly or the application's
assembly should have the resources for all the cultures to be supported by the