Asp.net中多语言的实现

来源:互联网 发布:数据库desc是什么意思 编辑:程序博客网 时间:2024/05/21 14:02

1.首先得先了解下面的知识:
  
   了解一个名词:表达式语法
   表达式语法格式:<%$ ... %>
   它是ASP.NET2.0新增了一种声明性表达式语法,可在分析页之前将值替换到页中.
   ASP.NET表达式是基于运行时计算的信息设置控件属性的一种声明性方式.
   ASP.NET表达式主要应用在:连接字符串,应用程序设置,资源文件等地方.
  
   ASP.NET 表达式的基本语法如下:
   <%$ expressionPrefix: expressionValue %>
  
   下面演示表达式语法在连接字符串和应用程序设置的应用:
  
   a.表达式语法在连接字符串的应用
   将存储在Web.config文件中的连接字符串的值设置为控件的连接字符串属性
   <asp:SqlDataSource ID="SqlDataSource1" Runat="server"
   SelectCommand="SELECT * FROM Employees"
   ConnectionString="<%$ ConnectionStrings:NorthwindCon %>">
   </asp:SqlDataSource>
  
   <configuration>
   <connectionStrings>
   <add name="NorthwindCon"
   connectionString="Data Source=yanfa0;Integrated Security=SSPI;Initial Ctalog=Northwind;"
   providerName="System.Data.SqlClient" />
   </connectionStrings>
   </configuration>
  
  
   b.表达式语法在应用程序设置的应用
   使用表达式来引用在Web.config配置文件中定义的应用程序设置
   <asp:Label ID="Label1" runat="server" Text="<%$ AppSettings:Txt %>"></asp:Label>
  
   <appSettings>
   <add key="Txt" value="Abc"/>
   </appSettings>
  
   其实表达式语法还有一个重要的应用,就是在资源文件中的使用,而资源文件正是我实现本地化的基础.
  
  
  
   区域性的格式设置
   区域性名称和标识符名称遵循RFC 1766标准,具体请查看msdn
   请看下图,抓取的部分区域性图片:
  
   区域性通常分为三个类型:固定区域性、非特定区域性和特定区域性。
   a.固定区域性不区分区域性。可以使用空字符串("")或者按区域性标识符0x007F来指定固定区域性,它与英语语言关联,但不与任何国家/地区关联。
   b.非特定区域性是与某种语言关联但不与国家/地区关联的区域性。
   c.特定区域性是与某种语言和某个国家/地区关联的区域性。
   例如:"fr"(法语)是非特定区域性,而"fr-FR"(法语(法国))是特定区域性
   特别提示:"zh-CHS"(简体中文)和"zh-CHT"(繁体中文)是非特定区域性。
  
   区域性是有层次结构的,即特定区域性的父级是非特定区域性,而非特定区域性的父级是InvariantCulture
  
   利用ASP.NET2.0可轻松地逐页更改区域性设置
   只需将UICulture和Culture(区域性)属性添加到.aspx等类似页面内的Page指令即可
   <%@ Page Culture="Auto" UICulture="Auto" %>
  
   要想对站点中的所有页面都设置相同的区域性设置,只需将以下元素添加到位于站点的根处的web.config文件中即可,这样就不必分别为每个页面进行分配了
   <globalization uiCulture="auto" culture="auto" />
  
   还可以为ASP.NET指定一个默认区域性
   以下指定页面的用户界面区域性是自动,默认区域性是英语,页的区域性是自动,默认区域性是英语(美国)
   <globalization uiCulture="auto:en" culture="auto:en-US" />
  
   Culture:指示页的区域性设置
   UICulture:指定用于页的用户界面(UI)区域性设置
  
  
  
   B.设置语言首选项
   自动检测功能所选择的默认Culture和UICulture有时可能不是用户所需要的
   例如:一个美国籍业务员来到中国出差,在中国总部上网下业务单.这种情况下,Web应用程序应该为用户提供显式更改语言的功能,给他提供英文版的业务下单页
  
   using System.Threading;
   using System.Globalization;
  
   protected override void InitializeCulture ( )
   {
   //显式的指定区域为en,为美国籍业务员显示英文页
   //实际应用中可以根据用户选择来动态设置语言
   //还可以利用Profile来记住用户选择的语言,以便用户以后访问网站时自动进入相应语言页
   Thread.CurrentThread.CurrentUICulture = new CultureInfo ( "en" );
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ( "en-US" );
   }
  
   注意:对Thread.CurrentThread.CurrentCulture和 Thread.CurrentThread.CurrentUICulture所做的更改需要在InitializeCulture()方法中进行,因为 对首选浏览器语言的自动检测是在页生存期的早期发生的
  
   特别提示:
   CultureInfo类的实例化一般有两个途径,如下所示:
   CultureInfo culture = CultureInfo. CreateSpecificCulture (name);
   CultureInfo culture = new CultureInfo(name);
   二者的区分:
   使用第一种方法,只能创建固定区域性或特定区域性的CultureInfo实例。
   使用第二种方法,则是建立一个name所指定的区域性的CultureInfo实例,它可以是固定的,非特定的或特定区域性的
  
   Thread类的CurrentCulture属性用来获取或配置当前线程的区域性,它必须被配置为特定区域性。
   假如Thread.CurrentThread.CurrentCulture = new CultureInfo ( "en" );就会报错!
  
   2.多语言的实现
  
   其实实现是比较简单的,只需要下面几个步骤
   1.创建全局资源表
   2.在session中存放表示当前的culture的变量,如“en-us”,"zh-cn"
  
   3.创建所有页面的基类PageBase,覆盖InitializeCulture函数
  
   4.页面上的文本值, 表达式语法赋值
  
  
   添加两个全局资源表
   Strings.resx和Strings.en-us.resx(注意-后面的字符要符合RFC 1766标准,没带后缀的话就是默认值)
  
  
  
   如下图:
   PageBase
  
   1using System;
   2using System.Collections.Generic;
   3using System.Linq;
   4using System.Web;
   5using System.Globalization;
   6/// <summary>
   7/// Summary description for PageBase
   8/// </summary>
   9public class PageBase:System.Web.UI.Page
   10{
   11 public PageBase()
   12 {
   13 //
   14 // TODO: Add constructor logic here
   15 //
   16 }
   17 protected override void InitializeCulture()
   18 {
   19 string currentCulture = (string)Session["Culture"];
   20 if (string.IsNullOrEmpty(currentCulture))
   21 {
   22 currentCulture = "zh-cn";
   23 Session["Culture"] = "zh-cn";
   24 }
   25
   26 System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(currentCulture);
   27 System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(currentCulture);
   28
   29 }
   30}
   31
  
  
   基本页面:
  
   1using System;
   2using System.Collections.Generic;
   3using System.Linq;
   4using System.Web;
   5using System.Web.UI;
   6using System.Web.UI.WebControls;
   7using System.Globalization;
   8public partial class _Default :PageBase
   9{
   10 protected void Page_Load(object sender, EventArgs e)
   11 {
   12
   13 }
   14 protected void btnChinese_Click(object sender, EventArgs e)
   15 {
   16 Session["Culture"] = "zh-cn";
   17/////可以尝试注释下面这句出现的效果
   18 this.RegisterClientScriptBlock("reload", "<script>window.location=window.self.location;</script>");
   19 }
   20 protected void btnEnglish_Click(object sender, EventArgs e)
   21 {
   22 Session["Culture"] = "en-us";
   23/////可以尝试注释下面这句出现的效果
   24 this.RegisterClientScriptBlock("reload", "<script>window.location=window.self.location;</script>");
   25 }
   26}
   27
   简单的一个多语言的例子
  
   缺点:切换语言时需要load两次
  
   建议:可以利用ajax请求修改完session值后在执行window.location=window.self.location;
  
   如:
  
   1 function PageRefresh(language)
   2 {
   3 //使用JQUERY做ajax的请求
   4 try
   5 {
   6
   7 var virthPath="http://localhost/MulitLanguage“;
   8
   9
   10 $.ajax({
   11 type: "POST",
   12 url: virthPath+"/AjaxHandler.ashx",///AjaxHandler.ashx中改变session值
   13 success: function(msg){
   14 ChangeLanguage_Rslt(msg);
   15 }
   16 });
   17
   18
   19 }
   20 catch(e)
   21 {
   22 alert('Error');
   23 }
   24
   25
   26 }
   27
   28 function ChangeLanguage_Rslt(data)
   29 {
   30 if(data=="OK")
   31 window.location=window.self.location;
   32
   33 }
   34 else
   35 {
   36 alert(data);
   37 }
   38 }

 

 

--------也可以参照

http://blog.csdn.net/zyip/archive/2009/02/16/3895994.aspx

原创粉丝点击