客户端多语技术
来源:互联网 发布:软件开发预算方案 编辑:程序博客网 时间:2024/04/29 14:22
定义一个类继承自IhttpModule,在Init方法中注册preRequestHandlerExecute事件,
通过实例化HttpApplication获取http请求内容,然后根据这些请求内容
从资源文件中取得客户端提示,
并且以js变量串的形式输出到当前页面
/ <summary>
/// ClientTipMultiLanguageHandler 的摘要说明
/// </summary>
public class ClientTipHandler : IHttpModule
{
public void Init(HttpApplication app)
{
app.PreRequestHandlerExecute += new EventHandler(PreRequestHandlerExecuteHandler);
}
void PreRequestHandlerExecuteHandler(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext currentContext = app.Context;
IHttpHandler currentHandler=app.Context.Handler;
if (currentHandler != null && currentHandler is Page)
{
Page currentPage = (Page)currentHandler;
//从当前线程中获取使用的语言
string lang = Thread.CurrentThread.CurrentUICulture.Name; //ConfigurationManager.AppSettings["lang"];
string currentURL = currentContext.Request.Url.ToString();
//当前请求的页的名字,不包含后缀
string currentPageName = null;
if (currentURL != null)
{
string[] temp = currentURL.Split('/');
//当前请求页的名字,不包括后缀,如为InventoryEdit
currentPageName = temp[temp.Length - 1].Split('.')[0];
}
#region 向业务模块的页面中输出自己页面所需要的js变量(也就是客户端提示语)
//由当前请求的URL来获取当前页所属模块对应的资源文件的名字,不包含后缀和文化信息,如当文件名为"AA.en-US.resx"时,此处得到的是"AA"
string resFileName = GetResouceFileNameOfModule(HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath);
string cacheKey =resFileName+"."+lang+"."+currentPageName;
//如果缓存为空,则从资源文件中取得客户端提示,并且以js变量串的形式输出到当前页面,且缓存起来。
object cachedContent = currentContext.Cache[cacheKey];
if (cachedContent == null)
{
#region
#region 获取当前模块对应的资源文件的路径和名称
string directory = GetResourceDirectory();
string fileName = HttpContext.Current.Server.MapPath(@"~/" + directory + "//" + resFileName + "." + lang + ".resx");
if (!File.Exists(fileName))
{
fileName = HttpContext.Current.Server.MapPath(@"~/" + directory + "//" + resFileName + ".resx");
if (!File.Exists(fileName))
{
return;
}
}
#endregion
#region 按规则读取资源文件中的条目,并以javascript变量的形式输出到页面中
string key = null;
string value = null;
StringBuilder clientTipsVars = new StringBuilder();
ResXResourceReader reader = new ResXResourceReader(fileName);
IDictionaryEnumerator id = reader.GetEnumerator();
clientTipsVars.Append("<script type=/"text/javascript/">" + "/n");
foreach (DictionaryEntry entry in reader)
{
key = entry.Key.ToString();
//当是模块的客户端提示的公共变量时(当前模块的所有页都可以用的JS变量)提取出来
//或为当前页需要的变量时,提取出来
//由于用户输入的URL不区分大小写,所以把currentPageName转换为小写,key也转换为小写,来比较
if (key.StartsWith("ClientTip_Common_")||key.ToLower().StartsWith("clienttip_"+currentPageName.ToLower()+"_"))
{
value = entry.Value.ToString();
clientTipsVars.Append("var " + key + " = /"" + value + "/";" + "/n");
}
}
clientTipsVars.Append("</script>/n");
#endregion
currentContext.Cache.Add(cacheKey, clientTipsVars.ToString(), null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
currentPage.RegisterStartupScript("ClientTip", clientTipsVars.ToString());
#endregion
}
else
{
currentPage.RegisterStartupScript("ClientTip", cachedContent.ToString());
}
#endregion
#region 向业务模块的页面中输出公共的js变量(也就是客户端提示语)
string relativeURL = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
string isBizPage = GetResouceFileNameOfModule(relativeURL);
//如果当前请求的页面是业务做的页面(而不是平台做的页面),则向页面中输出公共提示
if (isBizPage != "IsNotBizPage")
{
string uaCachKey = "UA_Common_ClientTip."+lang;
//如果缓存中没有,则产生,并加入到缓存中
if (HttpContext.Current.Cache[uaCachKey] == null)
{
#region
#region 获取"UA"资源文件的路径和名称
string resDirctory = GetResourceDirectory();
string uaFileName = HttpContext.Current.Server.MapPath(@"~/" + resDirctory + "//" + "UA" + "." + lang + ".resx");
if (!File.Exists(uaFileName))
{
uaFileName = HttpContext.Current.Server.MapPath(@"~/" + resDirctory + "//" + "UA" + ".resx");
if (!File.Exists(uaFileName))
{
return;
}
}
#endregion
#region 按规则读取UA资源文件中的条目,并以javascript变量的形式输出到页面中
string key = null;
string value = null;
StringBuilder clientTipsVars = new StringBuilder();
ResXResourceReader reader = new ResXResourceReader(uaFileName);
IDictionaryEnumerator id = reader.GetEnumerator();
clientTipsVars.Append("<script type=/"text/javascript/">" + "/n");
foreach (DictionaryEntry entry in reader)
{
key = entry.Key.ToString();
//UA中的以“UA_Common_ClientTip_”开头的串输出到页面
if (key.StartsWith("UA_Common_ClientTip_"))
{
value = entry.Value.ToString();
clientTipsVars.Append("var " + key + " = /"" + value + "/";" + "/n");
}
}
clientTipsVars.Append("</script>/n");
#endregion
currentContext.Cache.Add(uaCachKey, clientTipsVars.ToString(), null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
currentPage.RegisterStartupScript("ClientTip_Common", clientTipsVars.ToString());
#endregion
}
else
{
currentPage.RegisterStartupScript("ClientTip_Common", HttpContext.Current.Cache[uaCachKey].ToString());
}
}
#endregion
}
}
public void Dispose()
{
}
//获取资源文件所在目录
string GetResourceDirectory()
{
string directory = "";
if (ConfigurationManager.AppSettings["released"] == null)
{
directory = "App_GlobalResources";
}
else
{
if (ConfigurationManager.AppSettings["released"].ToLower() == "true")
{
directory = "ClientTipResources";
}
else
{
directory = "App_GlobalResources";
}
}
return directory;
}
//根据模块页面的基路径,获取它对应的资源文件的名字,如果不是业务模块的页面(而是平台的),则返回串“IsNotBizPage”
string GetResouceFileNameOfModule(string relativeURL)
{
SortedList<string, string> moduleURLBaseNameAndResourceNamePairs = new SortedList<string, string>();
moduleURLBaseNameAndResourceNamePairs.Add("BaseInfo", "AA");
moduleURLBaseNameAndResourceNamePairs.Add("SA", "SA");
moduleURLBaseNameAndResourceNamePairs.Add("CS", "CS");
moduleURLBaseNameAndResourceNamePairs.Add("ARAP", "ARAP");
moduleURLBaseNameAndResourceNamePairs.Add("Stock", "ST");
moduleURLBaseNameAndResourceNamePairs.Add("UA", "UA");
moduleURLBaseNameAndResourceNamePairs.Add("CommonPage", "UA");
moduleURLBaseNameAndResourceNamePairs.Add("Purchase", "PU");
moduleURLBaseNameAndResourceNamePairs.Add("RT", "UA");
foreach (string key in moduleURLBaseNameAndResourceNamePairs.Keys)
{
string relativeURLUpperCase=relativeURL.ToUpper();
string moduleURLBaseUpperCase = key.ToUpper();
//由于用户输入的URL不区分大小写,所以统一转换为大写
if (relativeURLUpperCase.StartsWith("~/" + moduleURLBaseUpperCase + "/"))
{
return moduleURLBaseNameAndResourceNamePairs[key];
}
}
return "IsNotBizPage";
}
}
- 客户端多语技术
- IBM Workplace客户端技术
- 客户端技术分类
- 解释一下智能客户端技术
- 客户端技术之抉择
- Ajax客户端技术
- 富客户端交互技术
- 智能客户端技术
- 客户端自动化技术漫谈
- 客户端自动化技术漫谈
- 客户端的web技术
- 客户端存储技术-PersistJS
- 客户端交互技术简介
- javascript客户端检测技术
- javascript客户端检测技术
- JavaScript客户端检测技术
- 客户端检测技术
- 客户端自动化技术漫谈
- WinCE中nandflash驱动开发介绍
- oracle创建database link远程连接
- asp.net自带ajax功能总结
- Google App Engine JAVA 开发环境搭建
- 非专业心理测试--有意思的测试
- 客户端多语技术
- 分页存储过程运用
- 开始C的学习,从基础做起。
- 生活小常识
- 一个完整的对日期、金额格式化的标签及使用示例
- 在线QQ、MSN、Zanpo、Email客服源代码
- 音频编解码开发中
- 清明节~
- CCIE没有缩水