|
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Xml;
namespace Localization
{
/// <summary>
/// The ResourceManagerXml is an implementation of the ResourceManager driven by XML files
/// </summary>
public class ResourceManagerXml : ResourceManager
{
#region Fields and Properties
private string fileName;
#endregion
#region Constructors
public ResourceManagerXml(NameValueCollection parameters)
{
if (parameters == null || parameters["languageFilePath"] == null)
{
throw new ApplicationException("ResourceManagerXml requires fileName attribute in configuraiton.");
}
fileName = parameters["languageFilePath"];
}
#endregion
#region Provider API
/// <remarks>
/// When in DEBUG mode, an ApplicationException will be thrown if the key isn't found
/// </remarks>
protected override string RetrieveString(string key)
{
NameValueCollection messages = GetResources();
if (messages[key] == null)
{
messages[key] = string.Empty;
#if DEBUG
throw new ApplicationException("Resource value not found for key: " + key);
#endif
}
return messages[key];
}
protected override LocalizedImageData RetrieveImage(string key)
{
Hashtable imageData = GetImages();
if (imageData[key] == null)
{
imageData[key] = new LocalizedImageData(0, 0, string.Empty);
#if DEBUG
throw new ApplicationException("Resource value not found for key: " + key);
#endif
}
return (LocalizedImageData)imageData[key];
}
#endregion
#region Private Methods
private NameValueCollection GetResources()
{
string currentCulture = ResourceManager.CurrentCultureName;
string defaultCulture = LocalizationConfiguration.GetConfig().DefaultCultureName;
string cacheKey = "Localization:" + defaultCulture + ':' + currentCulture;
if (HttpRuntime.Cache[cacheKey] == null)
{
NameValueCollection resource = new NameValueCollection();
LoadResources(resource, defaultCulture, cacheKey);
if (defaultCulture != currentCulture)
{
try
{
LoadResources(resource, currentCulture, cacheKey);
}
catch (FileNotFoundException)
{}
}
}
return (NameValueCollection)HttpRuntime.Cache[cacheKey];
}
private void LoadResources(NameValueCollection resource, string culture, string cacheKey)
{
string file = string.Format("{0}\\{1}\\Resource.xml", fileName, culture);
XmlDocument xml = new XmlDocument();
xml.Load(file);
foreach (XmlNode n in xml.SelectSingleNode("Resource"))
{
if (n.NodeType != XmlNodeType.Comment)
{
resource[n.Attributes["name"].Value] = n.InnerText;
}
}
HttpRuntime.Cache.Insert(cacheKey, resource, new CacheDependency(file), DateTime.MaxValue, TimeSpan.Zero);
}
private Hashtable GetImages()
{
string currentCulture = ResourceManager.CurrentCultureName;
string defaultCulture = LocalizationConfiguration.GetConfig().DefaultCultureName;
string cacheKey = "LocalizationImage:" + defaultCulture + ':' + currentCulture;
if (HttpRuntime.Cache[cacheKey] == null)
{
Hashtable resource = new Hashtable();
LoadImage(resource, defaultCulture, cacheKey);
if (defaultCulture != currentCulture)
{
try
{
LoadImage(resource, currentCulture, cacheKey);
}
catch (FileNotFoundException)
{}
}
}
return (Hashtable)HttpRuntime.Cache[cacheKey];
}
private void LoadImage(Hashtable resource, string culture, string cacheKey)
{
string file = string.Format("{0}\\{1}\\Images.xml", fileName, culture);
XmlDocument xml = new XmlDocument();
xml.Load(file);
foreach (XmlNode n in xml.SelectSingleNode("Images"))
{
if (n.NodeType != XmlNodeType.Comment)
{
LocalizedImageData data = new LocalizedImageData();
data.Alt = n.InnerText;
data.Height = Convert.ToInt32(n.Attributes["height"].Value);
data.Width = Convert.ToInt32(n.Attributes["width"].Value);
resource[n.Attributes["name"].Value] = data;
}
}
HttpRuntime.Cache.Insert(cacheKey, resource, new CacheDependency(file), DateTime.MaxValue, TimeSpan.Zero);
}
#endregion
}
}
|
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.
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.