This article uses the .NET Framework 1.1.
What is Globalization?
Globalization is the process of developing a program core whose features and code design are not solely based on a single language or locale. Instead, their design is developed for the input, display, and output of a defined set of Unicode-supported language scripts and data related to specific locales.
Globalization in .NET
The .NET Framework has three namespaces to help you develop world-ready applications:
System.Threading includes a very known class called
Thread class has a property called
CurrentCulture that you can use to get/set the current culture of the thread.
System.Globalization namespace contains classes that define culture-related information, including the language, the country/region, the calendars in use, the format patterns for dates, currency, and numbers, and the sort order for strings. These classes are useful for writing globalized (internationalized) applications.
System.Resources namespace provides classes and interfaces that allow developers to create, store, and manage various culture-specific resources used in an application. One of the most important classes of the
System.Resources namespace is the
Using resources files and the ResourceManager class
Resources files are XML documents that have all the resources for a specific culture. The
ResourceManager class provides convenient access to culture-specific resources at runtime.
Retrieving a value stored in a resource file is as simple as invoking the
GetString method of the
protected System.Web.UI.HtmlControls.HtmlAnchor LinkDetalhes;
protected System.Web.UI.WebControls.Label TituloIndex;
private void Page_Load(object sender, System.EventArgs e)
ResourceManager rm = new ResourceManager("sample",
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
TituloIndex.Text = rm.GetString(TituloIndex.ID, ci);
LinkDetalhes.InnerText = rm.GetString(LinkDetalhes.ID, ci);
Now, it would be great if we could call the
Injecting the resource file content into the HTML
The first thing we have to do is to get the resource file contents. The
ResourceManager has a method called
GetResourceSet that obtains a
ResourceSet that represents the resources localized for the specified culture.
Then, we must find the form control of our page and start creating hidden inputs with the ID and the value of the resources.
protected void AddResourceSetToHtml()
HtmlForm theForm = null;
theForm = Page.Controls[i] as HtmlForm;
ResourceSet rs = rm.GetResourceSet(ci, true, true);
IDictionaryEnumerator de = rs.GetEnumerator();
HtmlInputHidden ih = new HtmlInputHidden();
string key = de.Key as String;
string val = de.Value as String;
if(key!=null && val!=null)
ih.ID = "res"+key;
ih.Value = val;
Since the IDs of my controls is equal to the IDs of the resources stored in the resource file, when I'm creating hidden inputs, I have to add something to the ID. If I don't do that, the result would be two controls with the same ID. If the ID stored in the resource file isn't the same as the ID of the control, it wouldn't be necessary.
btnAlert = document.getElementById("btnAlert");
btnAlert.value = document.getElementById("resBtnAlertTitulo").value;
The XML of a resource file has the following format:
The RSS header has some information about the resource type. The element we're interested in is the
data element. The
data element represents an entry in the resource file. Just like an entry in a Dictionary, you'll find a pair: name and value. The name is an attribute and the value is an element.
Let's look at a sample XSLT that uses this approach:
<th colspan="2">Globalization + XSL</th>
As always, comments/sugestions are welcome!