|
using System;
using System.Data;
using System.Resources;
using System.Globalization;
using System.Reflection;
namespace GlobalizationModule
{
/// <summary>
/// Special LocalizeAttribute for localizing a DataList control.
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class DataListLocalizer : LocalizeAttribute
{
public DataListLocalizer()
{}
/// <summary>
/// Iterate over rows in target DataList and replace strings in column
/// 'ItemText' with language specific strings from resource.
/// </summary>
public override void LocalizeObject(object target, System.Web.UI.Page page)
{
// User's page class is superclass of the ASP.NET page class
Type userPageClass = page.GetType().BaseType;
// The user's assembly is the one that holds his/her page class
Assembly targetAssembly = userPageClass.Assembly;
CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
System.Web.UI.WebControls.DataList dl = (System.Web.UI.WebControls.DataList)target;
DataSet ds = (DataSet)dl.DataSource;
foreach (DataRow dr in ds.Tables[0].Rows)
{
dr["ItemText"] = base.LoadResource(
string.Format("{0}.{1}", ResourceName, dr[0]),
culture, page, targetAssembly);
}
dl.DataBind();
}
/// <summary>
/// Load string from resource.
/// </summary>
/// <param name="s">Specifies resource to load</param>
/// <param name="resMan">ResourceManager</param>
/// <param name="culture">Specifies language</param>
/// <returns></returns>
private string LocalizeString(string resourceName, ResourceManager resMan, CultureInfo culture)
{
string localized = string.Format("no resource '{0}.{1}' for language {2}", ResourceName, resourceName, culture.Name);
try
{
localized = resMan.GetString(string.Format("{0}.{1}", ResourceName, resourceName), culture);
}
catch (MissingManifestResourceException)
{}
return localized;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Sami Vaaraniemi has been working as a software developer since 1990, primarily on Microsoft technologies. After 12 years of Win32 API and C++ he switched to .NET. He currently works as an independent consultant and can be contacted through his website at www.capehill.net.