用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
btbtd的公告
<dl> <dd> <a href="http://www.btbtd.org/" target="_blank">www.btbtd.org</a> </dd> <dd> <a href="http://gi.2288.org/" target="_blank">gi.2288.org</a> </dd> </dl> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-1785954-3"); pageTracker._trackPageview(); </script>
文章分类
#1. Algorithm
Bit Twiddling Hacks - By Sean Eron Anderson
Paj's Home
XySSL
高度な JavaScript 技集
0. ASP
aspfaq.com
codeproject.com
motobit.com
0. ASP.NET(C#)
ASP .NET FAQ
C# Frequently Asked Questions
C# General FAQ
C# MVP Bloggers
0. CSS
Comparison of layout engines (CSS)
0. Javascript
Comparison of layout engines (DOM)
Comparison of layout engines (ECMAScript)
crockford.com
faqts.com/javascript
Pat's Place: A technology demonstrator project.
0. SQL
Comparison of different SQL implementations
Comparison of SQL syntax
What are reserved Access, ODBC and SQL Server keywords?
What are the main differences between Access and SQL Server?
0. Tool Kits
ECMA Standards
faqts.com
Mozilla docs
RFC's
W3C TR
1. Search Engines
Alexa Web Search
All the web
AltaVista Search
AOL Web Search
Baidu
CrazySearch!
CSDN Blog Search
Excite
FeedsS's Blog
Google
Kingsoft
Lycos Search
Lyric Search
Mpeg Search
MSN Search
Search.com
SoGua
Soudu
xx-hacker
Yahoo [cn]
Yahoo [en]
Yisou
2. English Forum
#旺旺英语论坛
#英语写作论坛
Learners english club
3. Linux Forum
#三峡大学 LINUX联盟(RSS)
#中国 Linux 公社(RSS)
#黄嘴企鹅论坛(RSS)
Fedora 1.0 问题(RSS)
LINUX 1000 Q(RSS)
Linux 伊甸园(RSS)
4. PHP Forum
#中文PHP俱乐部(RSS)
#超越 PHP(RSS)
5. Other Site
#中国网络研究与搜索论坛
#在线翻译(RSS)
#常用书籍打包
#电子图书下载(RSS)
#科迈网(RSS)
#蓝色理想(RSS)
#西安信息资源网
CSDN.NET(RSS)
Google Hacker Database
Web Design Group
Webmaster world
6. Enjoy Site
#新华网论坛(RSS)
#极点视觉绘画站(RSS)
#涂鸦王国(RSS)
#牧文财经(RSS)
#猫扑大杂烩(RSS)
7. Resource
Active Perl
Apache
j2sdk-1_4_2-windows-i586
mm.mysql-2.0.4-bin.jar
mod_jk_1.2.5_2.0.47.dll
mod_rewrite
MySql
PHP
phpMailer
Source Forge Xoops
Tomcat
Web Server Resource
Win2k3
Zend
8. My Blog
#搜索引擎优化 Seo-GI
#绿色学院 - Green Institute
Blogcn(RSS)
GI Technology
GI-BB Developer Team
Green Institute
MSN Blog
Wallop Flash
Wallop HTML (RSS)
卐-_卍 信仰,理想,追求,博爱,自强,厚德.(RSS)
9. Music & Album
GaeAhit
USTC
VeryCD-MTV
A. Manual
Apache Manual
CSS Level 2 Manual
HTML 4.01 Manual
Javascript Manual
MySql Manual (cn) 2
MySql Manual (en) 1
PEAR Manual
PHP Manual
PHP-GTK Manual
Regular Expression Library
Smarty Manual
W3C Technical Reports and Publications
XHTML 1.0 Manual
XML 1.0 Manual
B. Google Cheat
Blog.com
ChinaUnix
cnblogs
donews
Google Groups
Teeta
Tom blog
Yahoo Groups
Yculblog
C. Friend's Blog
#我爱生命中存在的那一切(RSS)
#老贼在干嘛?(RSS)
#鸣乐谷
Color_Se SeSe 颜色(RSS)
Drink Drank Drunk
PLANTUNT的珍贵资源
D. SEO
Keywords 1 (Google)
Keywords 2 (Baidu)
Keywords 3 (Yisou)
Marketleap.com
Pagerank 1
Pagerank 2
Pagerank 3
Trafficzap.com
Uptimebot.com
Webconfs
E. Technology
http://bt.it.to/
http://bt.qq.to/
http://bt.zz.to/
http://du.u.to/
http://gi.dd.to/
http://gi.if.to/
http://gi.kk.to/
http://gi.pc.to
http://gi.xx.to/
http://mail.it.to/
http://nba.it.to
http://se.u.to/
F. Literature
#逐浪文学
KK 书库
G. Math/Geometry
台湾一个不错的数学站
H. Works
宝莱迪
I. SEO Link
paolid
shawl.qiu CSharp DotNet 个人资料管理系统
shawl.qiu CSharp DotNet 个人资料管理系统
shawl.qiu CSharp DotNet 个人资料管理系统
依梦卫浴-依梦洁具(北京)厂家直销店
卫浴生活坊
满意洁具馆
热弯玻璃
热熔玻璃
热熔玻璃
玻璃
玻璃制品
艺术玻璃
存档

原创  在.net 1.x 中, 实现多语言功能, by shawl.qiu 收藏

在.net 1.x 中, 实现多语言功能, by shawl.qiu



说明:
这两天看了N种实现多语言的方案, 但都N麻烦, 动不动就有些功能不支持:(
像类 System.Resources.ResXResouceWriter 和 命名空间 Microsoft.JScript... 在我的.NET Framework 居然找不到...:(

有鉴于此, 虽然功能实现了, 但还是总结总结, 希望对其他人有帮助...

主要是使用 xml 形式的 DataSet 实现...
考虑中英文目录应该是两份站点文件, 所以用 web.config 设置各站点的主要功能... 比如默认语言..Cookie 名等...

嗯, 这个功能现在被我弄得还是挺简单的, 就不多说明了...

目录
1. web.config
2. default.aspx
3. English.xml
4. Chinese.xml

shawl.qiu
2007-10-14
http://blog.csdn.net/btbtd

下载:
http://files.myopera.com/btbtd/csharp/Example/MulitLanguage_DotNet(CSharp).7z

内容
1. web.config
<?xml version="1.0" encoding="UTF-8" ?> 
<configuration>
 
 <appSettings>
  <add key="DefaultLanguage" value="Chinese" />
  <add key="CookieName" value="CnLangCkName" />
 </appSettings>
 
 <location path="." allowOverride="true">
  <system.web>
   <httpRuntime maxRequestLength="20480"
    useFullyQualifiedRedirectUrl="true"
    executionTimeout="90"
    />

    
   <!--
    <pages validateRequest="false" />
   -->

          
 <!--  
   <customErrors defaultRedirect="/include/error/generalError.html"
    mode="RemoteOnly" >
    <error statusCode="404" redirect="/include/error/error404.html" />
   </customErrors>
 -->

   <customErrors defaultRedirect="/GlobalError/GeneralError.html"
     mode="RemoteOnly">

    <error statusCode="404" redirect="/GlobalError/404.htm" />
    <error statusCode="403" redirect="/GlobalError/404.htm" />
   </customErrors>
   
   <globalization 
    requestEncoding="UTF-8"
    responseEncoding="UTF-8"
    fileEncoding="UTF-8"
   />

   
 <!--  <sessionState mode="InProc"
    cookieless="false"
    timeout="20" >
   </sessionState>-->

   
 <!-- <sessionState mode="SQLServer"
   cookieless="true"
   timeout="20"
   sqlConnectionString="data source=127.0.0.1;user id=sqCsAtSysUser;password=sqCsAtSysUserPwd"
   >
  </sessionState>-->

  
  <compilation defaultLanguage="c#" 
    debug="true" 
    numRecompilesBeforeAppRestart="15">
 
    <assemblies> 
<!--       
     <add assembly="SQ"/>
     <add assembly="SqNs"/>
 -->

    </assemblies> 
  </compilation>
  
  </system.web>
 </location>
 
</configuration>  

<!--Compile Marker-->  

2. default.aspx
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ import namespace="System.Data" %>
<script runat="server">
 
 void Page_Load(Object s, EventArgs e)
 {
  string Lang = GetLangFromCookie();
  DataTable Language = DataSetXmlToDataTable(Lang+".xml""Language"true);
  
  //Test.Text = GetLangValue(Language, "Test");
  SetLangLabel(Language, "Test", Test);
  SetLangLabel(Language, "SelectLang", SelectLang);
  //ddl.SelectedValue = Lang;
  
  //Response.Write("<br/>Lang: "+Lang);
 } // end Page_Load
 
 void DdlIndexChange(Object o, EventArgs e)
 {
  string DefaultLanguage = ConfigurationSettings.AppSettings["DefaultLanguage"];
  string CookieName = ConfigurationSettings.AppSettings["CookieName"];
  
  //Response.Write("<br/>: "+ddl.SelectedValue);
  AddCookie(CookieName, ddl.SelectedValue);

//   HttpContext.Current.Response.Write("<script>setTimeout(function(){location.href=document.URL;}, 1000);</"+"script>");  
  //HttpContext.Current.Response.Write("ok");
  
  HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
 }
 
 string GetLangFromCookie()
 {
  string DefaultLanguage = ConfigurationSettings.AppSettings["DefaultLanguage"];
  string CookieName = ConfigurationSettings.AppSettings["CookieName"];
  
  string Lang = ReadCookie(CookieName);
  if(Lang!="")
  {
   if(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(Lang+".xml")))
   {
    //HttpContext.Current.Response.Write("<br/>Lang: "+Lang);
    //HttpContext.Current.Response.Write("<br/>no exists");
    Lang = DefaultLanguage;
   }
  } 
  if(Lang=="") Lang = DefaultLanguage;
  
  return Lang;
 }
 
 string GetLangValue(DataTable Dt, string Key)
 {
  DataRow[] Dr = Dt.Select("key='"+Key+"'");
  if(Dr.Length>0) return Dr[0]["Value"]+"";
  return ""
 }
  
 void SetLangLabel(DataTable Dt, string Key, Label Lbl)
 {
  DataRow[] Dr = Dt.Select("key='"+Key+"'");
  if(Dr.Length>0) Lbl.Text = Dr[0]["Value"]+"";
  else Lbl.Text = ""
 }
 
 DataTable DataSetXmlToDataTable(string path, string tblName)
 {
  DataSet dsTemp = new DataSet();
  DataTable dtTemp = new DataTable();
  dsTemp.ReadXml(path);
  dtTemp=dsTemp.Tables[tblName];
  return dtTemp;
 }
 
 DataTable DataSetXmlToDataTable(string path, string tblName, bool cov)
 {
  if(cov)
  {
   path = System.Web.HttpContext.Current.Server.MapPath(path);
  }
  return DataSetXmlToDataTable(path, tblName);
 }
 
 string ReadCookie(string key)
 { 
  return ReadCookie(key, true);
 }
 
 string ReadCookie(string key, bool decode)
 {
  HttpCookie hc = HttpContext.Current.Request.Cookies[key];
  if(hc==null) return "";
  if(decode) return HttpContext.Current.Server.UrlDecode(hc.Value);
  return hc.Value;
 }
 
 void AddCookie(string key, string value)
 {
  AddCookie(key, value, 60*24*7true);
 }
 
 void AddCookie(string key, string value, int minutes)
 {
  AddCookie(key, value, minutes, true);
 }
 
 void AddCookie(string key, string value, int minutes, string sPath)
 {
  AddCookie(key, value, minutes, true, sPath);
 }
 
 void AddCookie(string key, string value, int minutes, bool encode)
 {
  if(encode) value = HttpContext.Current.Server.UrlEncode(value);
  HttpCookie hc = new HttpCookie(key, value);
  DateTime dt = DateTime.Now;
  TimeSpan ts = new TimeSpan(00, minutes, 0);
  hc.Expires = dt.Add(ts);
  HttpContext.Current.Response.Cookies.Add(hc);
 }
 
 void AddCookie(string key, string value, int minutes, bool encode, string sPath)
 {
  if(encode) value = HttpContext.Current.Server.UrlEncode(value);
  HttpCookie hc = new HttpCookie(key, value);
  DateTime dt = DateTime.Now;
  TimeSpan ts = new TimeSpan(00, minutes, 0);
  hc.Expires = dt.Add(ts);
  hc.Path = sPath;
  HttpContext.Current.Response.Cookies.Add(hc);
 }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template</title>
</head>
<body>
 <form runat="server">
  
  <asp:Label id="SelectLang" runat="Server" />:
  <asp:DropDownList id="ddl" runat="Server" AutoPostBack="true"
   OnSelectedIndexChanged = "DdlIndexChange"
   >

   <asp:ListItem>请选择</asp:ListItem>
   <asp:ListItem Value="Chinese">Chinese</asp:ListItem>
   <asp:ListItem Value="English">English</asp:ListItem>
  </asp:DropDownList>
  <hr/>
  <asp:Label id="Test" runat="Server" />
 </form>
 
 <script type="text/javascript">
 /*<![CDATA[*/
 
 /*]]*/
 
</script>
</body>
</html>

3. English.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Language>
   <Key>Test</Key>
   <Value>test string</Value>
  </Language>
  
  <Language>
   <Key>Test1</Key>
   <Value>test1 string</Value>
  </Language>
  
  <Language>
   <Key>SelectLang</Key>
   <Value>Language</Value>
  </Language>
  
</NewDataSet>

4. Chinese.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Language>
   <Key>Test</Key>
   <Value>测试</Value>
  </Language>
  
  <Language>
   <Key>Test1</Key>
   <Value>测试1</Value>
  </Language>
  
  <Language>
   <Key>SelectLang</Key>
   <Value>语言</Value>
  </Language>
  
</NewDataSet>

发表于 @ 2007年10月14日 20:01:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:shawl.qiu javascript 日历控件类(Calendar) v1.0 | 新一篇:7-Zip for Asp.Net by shawl.qiu 2007-10-28

  • 发表评论
  • 评论内容:
  •  
Copyright © btbtd
Powered by CSDN Blog