using System;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Resources;
using System.Web;
using System.Web.UI;
using System.Threading;
namespace Globalization
{
///
/// Summary description for Globalization.
///
public class Globalization
{
static protected ResourceManager manager = null;
static private string resourceName = null;
static private DateTime lastModification = DateTime.Now;
static Thread ThreadcheckForUpdates;
static private string xmlName;
static private string path;
static public string Language
{
get { return HttpContext.Current.Request.UserLanguages[0]; }
}
static public string Path
{
set { path = value; }
}
static public string XmlPath
{
set { xmlName = value; }
}
static public string ResourcePath
{
set{ resourceName = value; manager = ResourceManager.CreateFileBasedResourceManager(value, path, null); }
}
static public string GetString(string component)
{
string CurrentPage = HttpContext.Current.Request.RawUrl.Substring(HttpContext.Current.Request.RawUrl.LastIndexOf("/")+1);
if (CurrentPage.IndexOf("?") > 0)
CurrentPage = CurrentPage.Remove(CurrentPage.IndexOf("?")-5, CurrentPage.Length - (CurrentPage.IndexOf("?")-5));
else
CurrentPage = CurrentPage.Remove(CurrentPage.Length-5, 5);
return GetString(CurrentPage, component);
}
static public string GetString(string page, string component)
{
FileInfo fi = new FileInfo(path + resourceName + ".resources");
if (!fi.Exists)
GenerateResourceFiles();
if (manager == null)
throw new Exception("Xml File Url is undefined");
try
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);
}
catch (Exception)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}
string returnString;
try { returnString = manager.GetString(page + "." + component).Replace("[BR]", "
"); }
catch (Exception) { returnString = ""; }
manager.ReleaseAllResources();
return returnString;
}
static private void GenerateResourceFiles()
{
FileStream fs = new FileStream(path + resourceName + ".resources", FileMode.OpenOrCreate, FileAccess.Write);
FileStream fsEN = new FileStream(path + resourceName + ".EN.resources", FileMode.OpenOrCreate, FileAccess.Write);
IResourceWriter writer = new ResourceWriter(fs);
IResourceWriter writerEN = new ResourceWriter(fsEN);
try
{
FileInfo fi = new FileInfo(path + "_" + xmlName);
if (!fi.Exists)
XMLTransform();
XmlDocument document = new XmlDocument();
document.Load(path + "_" + xmlName);
XmlNode rootnode = document.FirstChild;
foreach (XmlNode datanode in rootnode)
foreach (XmlNode nodeLanguage in datanode)
{
if (nodeLanguage.Name == "FR")
writer.AddResource(datanode.Attributes["name"].Value, nodeLanguage.InnerText);
else
writerEN.AddResource(datanode.Attributes["name"].Value, nodeLanguage.InnerText);
}
}
catch (Exception) { }
writer.Generate();
writer.Close();
writerEN.Generate();
writerEN.Close();
try { fs.Close(); }
catch (Exception) {}
try { fsEN.Close(); }
catch (Exception) {}
manager = ResourceManager.CreateFileBasedResourceManager(resourceName, path, null);
}
static private void XMLTransform()
{
StreamReader sr = new StreamReader(path + xmlName, System.Text.Encoding.Default);
StreamWriter sw = new StreamWriter(path + "_" + xmlName, false, System.Text.Encoding.UTF8);
string buffer = sr.ReadToEnd();
buffer = buffer.Replace("&", "&");
buffer = buffer.Replace("é", "é");
buffer = buffer.Replace("è", "e");
buffer = buffer.Replace("ê", "e");
buffer = buffer.Replace("ç", "ç");
buffer = buffer.Replace("à", "'");
buffer = buffer.Replace("'", "'");
buffer = buffer.Replace("î", "î");
sw.Write(buffer);
sw.Close();
sr.Close();
}
static private void CheckForUpdatesThread()
{
FileInfo fi = new FileInfo(path + xmlName);
Globalization.GenerateResourceFiles();
lastModification = fi.LastWriteTime;
while (true)
{
if (fi.Exists && fi.LastWriteTime != lastModification)
{
Globalization.GenerateResourceFiles();
lastModification = fi.LastWriteTime;
}
Thread.Sleep(60000);
}
}
static public void CheckForUpdates()
{
ThreadcheckForUpdates = new Thread(new ThreadStart(CheckForUpdatesThread));
ThreadcheckForUpdates.Start();
}
}
}