ASP.NET 2.0高级编程(特别版)-16.8 站点地图的本地化

来源:互联网 发布:腾讯视频没有mac版吗 编辑:程序博客网 时间:2024/05/21 23:00
 

16.8  站点地图的本地化

改进的资源文件(.resx)是本地化ASP.NET应用程序的一种好方法。www.Phontol.com本书的第30章介绍了如何使用ASP.NET对Web应用程序进行本地化。www.Phontol.com这里主要介绍如何把本地化功能应用于应用程序的页面上,不进一步演示如何将该本地化功能应用于诸如Web.sitemap文件等具体的项上。www.Phontol.comlink.Phontol.com

16.8.1  为本地化构建Web.sitemap文件

可以把本地化指令应用于ASP.NET Web应用程序的页面上,也可以使用相同的框架在Web.sitemap文件中完成本地化任务。www.Phontol.com为了说明这一点,程序清单16-31构建了一个Web.sitemap文件,它有点类似于程序清单16-1中的Web.sitemap文件,但要简单得多。www.Phontol.comlink.Phontol.com

程序清单16-31  为本地化创建一个基本的.sitemap文件link.Phontol.com

<?xml version="1.0" encoding="utf-8" ?>link.Phontol.com

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" link.Phontol.com

 enableLocalization="true">link.Phontol.com

 <siteMapNode url="Default.aspx" resourceKey="Home">link.Phontol.com

         <siteMapNode url="News.aspx" resourceKey="News">link.Phontol.com

                <siteMapNode url="News.aspx?cat=us" resourceKey="NewsUS" />link.Phontol.com

                <siteMapNode url="News.aspx?cat=world" resourceKey="NewsWorld" />link.Phontol.com

                <siteMapNode url="News.aspx?cat=tech" resourceKey="NewsTech" />link.Phontol.com

                <siteMapNode url="News.aspx?cat=sport" resourceKey="NewsSport" />link.Phontol.com

         </siteMapNode>link.Phontol.com

 </siteMapNode>link.Phontol.com

</siteMap>link.Phontol.com

程序清单16-31是一个相当简单的Web.sitemap文件。www.Phontol.com为了在Web.sitemap文件中支持本地化功能,必须使用<siteMap>元素中的enableLocalization属性,把它设置为true,以启用这个功能。www.Phontol.com之后,就可以使用<siteMapNode>元素定义每个导航节点了。www.Phontol.com但在这个例子中,由于要在各个.resx文件中定义这些导航部分的内容(尤其是title和description属性),所以不需要在这个文件中重复定义这些项。www.Phontol.com也就是说,这个例子只需要定义url属性。www.Phontol.com但要注意,还可以通过.resx文件定义这个属性,从而根据终端用户定义的文化设置,把他们定向到不同的页面上。www.Phontol.comlink.Phontol.com

下一个要注意的属性是<siteMapNode>元素中使用的resourceKey。www.Phontol.com它是各个.resx文件中使用和定义的键。www.Phontol.com以下面的<siteMapNode>元素为例:link.Phontol.com

<siteMapNode url"News.aspx" resourceKey="News">link.Phontol.com

   ...link.Phontol.com

</siteMapNode>link.Phontol.com

在这个例子中,resourceKey的值(和.resx文件中使用的键)是News。www.Phontol.com这表示可以使用下面的语法,在.resx文件中定义title和description属性的值:link.Phontol.com

News.Titlelink.Phontol.com

News.Descriptionlink.Phontol.com

有了Web.sitemap文件,下一步就是对Web.config文件进行一些小的修改,如下所述。www.Phontol.comlink.Phontol.com

16.8.2  修改Web.config文件

有了Web.sitemap文件,下一步就是给Web.config文件添加一些内容。www.Phontol.com为了让Web应用程序自动检测查看各个页面的用户所使用的文化设置,需要在@Page指令中设置Culture和UICulture属性,或者在Web.config文件的<globalization>元素中设置这些属性,以支持自动检测。www.Phontol.comlink.Phontol.com

在处理导航和Web.sitemap文件时,最好在Web.config文件中进行这些修改,使它们在应用程序的每个页面上自动发挥作用。www.Phontol.com这么做也比较简单,因为不需要给每个页面添加这些修改。www.Phontol.comlink.Phontol.com

要进行这些修改,打开Web.config文件,添加一个<globalization>元素,如程序清单16-32所示。www.Phontol.comlink.Phontol.com

程序清单16-32  给Web.config文件添加文化检测功能link.Phontol.com

<configuration>link.Phontol.com

   <system.web>link.Phontol.com

      <globalization culture="auto" uiCulture="auto" />link.Phontol.com

   </system.web>link.Phontol.com

</configuration>link.Phontol.com

为了支持自动检测功能,只需把culture和uiCulture属性设置为auto。www.Phontol.com还可以把其值定义为auto:en-US,它表示支持自动文化检测功能,但如果所定义的文化在各个资源文件中未找到,就把en-US(美国英语)用作默认文化。www.Phontol.com但因为我们要定义一组Web.sitemap默认值,所以不需要这样做。www.Phontol.comlink.Phontol.com

接下来,需要创建程序集资源文件,以定义Web.sitemap文件使用的值。www.Phontol.comlink.Phontol.com

16.8.3  创建程序集资源(.resx)文件

要创建一组用于Web.sitemap文件的程序集资源文件,应在项目中创建一个文件夹App_GlobalResources。www.Phontol.com如果使用的是Visual Studio 2005或Visual Web Developer,就可以右击项目,选择Add Folder | App_GlobalResources Folder,来添加这个文件夹。www.Phontol.comlink.Phontol.com

添加了文件夹后,下一步是给这个文件夹添加两个程序集资源文件,第一个文件命名为Web.sitemap.resx,第二个文件命名为Web.sitemap.fi.resx。www.Phontol.comWeb.sitemap.resx文件包含一组在Web.sitemap.resx文件中定义的、用于Web.sitemap文件的默认值,Web.sitemap.fi.resx文件包含这些值的芬兰语版本。www.Phontol.comlink.Phontol.com

文件名中有fi值,表示该文件由把首选语言设置为fi-FI的用户使用。www.Phontol.com这些设置的其他值如表16-6所示。www.Phontol.comlink.Phontol.com

表  16-6link.Phontol.com

.resx  文  件link.Phontol.com

对应的文化link.Phontol.com

Web.sitemap.resxlink.Phontol.com

终端用户的文化不能通过另一个.resx文件来识别,就使用默认值link.Phontol.com

Web.sitemap.en.resxlink.Phontol.com

用于所有en(英语)用户的资源文件link.Phontol.com

Web.sitemap.en-gb.resxlink.Phontol.com

用于所有英国英语用户的资源文件link.Phontol.com

Web.sitemap.fr-ca.resxlink.Phontol.com

用于所有加拿大法语用户的资源文件link.Phontol.com

Web.sitemap.ru.resxlink.Phontol.com

用于所有俄语用户的资源文件link.Phontol.com

有了Web.sitemap.resx文件和Web.sitemap.fi.resx文件之后,下一步是给这些文件填充值。www.Phontol.com为此,使用前面在Web.sitemap文件中定义的键。www.Phontol.com图16-31显示了结果。www.Phontol.comlink.Phontol.com

link.Phontol.com

图  16-31link.Phontol.com

有了这些文件后,就可以测试本地化的工作情况,如下一节所述。www.Phontol.comlink.Phontol.com

16.8.4  测试结果

在应用程序中创建一个页面,在页面上放置一个TreeView服务器控件。www.Phontol.com除了TreeView控件之外,还要放置一个SiteMapDataSource控件,以处理前面创建的Web.sitemap文件。www.Phontol.com设置TreeView控件的属性DataSourceID=“SiteMapDataSource1”,如本章前面所示,将这两个控件关联起来。www.Phontol.comlink.Phontol.com

如果在Microsoft的Internet Explorer中把语言设置为en-us(美国英语),结果就如图16-32所示。www.Phontol.comlink.Phontol.com

link.Phontol.com

图  16-32link.Phontol.com

在浏览器中打开这个页面,就会检查请求的文化。www.Phontol.com由于本例定义的设置是用于使用en-us文化的用户,所以使用默认的Web.sitemap.resx。www.Phontol.com于是,使用Web.sitemap.resx文件填充TreeView控件的值,如图16-32所示。www.Phontol.com如果请求者把文化设置为fi(芬兰语),就会得到完全不同的结果。www.Phontol.comlink.Phontol.com

要测试它,可以修改IE中使用的首选语言。www.Phontol.com首先,在IE中选择Tools | Internet Options,在第一个选项卡上(General),单击对话框底部的Languages按钮,打开Language Preferences对话框。www.Phontol.com单击Add按钮,在选项列表中添加Finnish语言设置。www.Phontol.com最后,使用Move Up按钮把Finnish选项移动到列表的最上面。www.Phontol.com结果如图16-33所示。www.Phontol.comlink.Phontol.com

link.Phontol.com

图  16-33link.Phontol.com

有了这个设置,运行页面,TreeView控件就会显示如图16-34所示的结果。www.Phontol.comlink.Phontol.com

link.Phontol.com

图  16-34link.Phontol.com

现在,请求页面时,文化设置为fi,对应于Web.sitemap.fi.resx文件,而不是默认的Web.sitemap.resx文件。www.Phontol.comlink.Phontol.com

原创粉丝点击