App_GlobalResources和LocalResources文件夹区别和用途

来源:互联网 发布:php面向对象设计模式 编辑:程序博客网 时间:2024/06/04 00:28

Asp.net多语言中的App_GlobalResources和LocalResources文件夹区别与用途

App_GlobalResources的文件是全局文件资源,可以在任何页面通过Resources直接使用这里面的资源文件。示例如下:

1、在App_GlobalResources中增加一个文件叫做ResourceTest.resx

2、在ResourceTest.resx中增加两个项 PageTitle 值为“中国加油!” LabelText 值为“奥运顺利!”

3、打开aspx页面的,然后在Page_Load方法中可以直接这样使用

protected void Page_Load(object sender, EventArgs e)
       {
           this.Title = Resources.ResourceTest.PageTitle;
           Label1.Text = Resources.ResourceTest.LabelText;
       }

这里的ResouceTest就是那个资源文件的文件名,在VS中,可以自动感知出ResourceTest。

直接在页面的控件中的使用方法是:

<asp:Label ID="Label1" runat="server" Text="<%$Resources:ResourceTest,LabelText %>" ></asp:Label>

如果使用这种方式绑定了App_GlobalResources,那么就不能再绑定App_LocalResources中的资源文件了

如果要支持英文,就在App_GlobalResources中增加一个文件,文件命名格式是:ResourcesTest.en-us.resx,在页面的使用方式不变。当你用浏览器访问时,系统会自动侦测出你的浏览器设置的默认语言,然后自动调用不同的资源包来呈现出来。

除了上面所谈到的方式可以直接使用资源包,还可以通过HttpContext.GetGlobalResourcesObject来获取资源包的内容。

HttpContext.GetGlobalResourceObject(resxFile, MyResName)

HttpContext.GetGlobalResourceObject(resxFile, MyResName,CurrentCulture)这个方法第一个参数是资源文件名,第二个参数是要检索的键值。调用例子为:

string GetGlobalResources(string resxFile,string resxKey)

{

string resxValue=(string)HttpContext.GetGlobalResourceObject(resxFile, resxKey)

if(string.IsNullOrEmpty(resxValue)

{

return string.Empty;

}

return resxValue;

}

App_LocalResources文件夹,这个文件夹中放的是页面的资源文件,这些资源文件和每个Aspx页面对应。比如我在网站项目下添加了一个Default.aspx文件,在设计VS的模式下,选择工具“生成本地资源” 就会自动在App_LocalResources中生成一个名字为Default.aspx.resx的资源文件。

编程访问的方式是:

HttpContext.GetLocalResourceObject("resxFile","resxKey")

直接在控件中的访问方式:

<asp:Label ID="Label1" runat="server" meta.:resourcekey="LabelText"></asp:Label>

 

怎样读取App_LocalResources里某一资源文件的所有key,value值?   

public static void Main()
{
// Create a ResourceReader for the file items.resources.
ResourceReader rr = new ResourceReader("items.resources");


// Create an IDictionaryEnumerator to iterate through the resources.
IDictionaryEnumerator id = rr.GetEnumerator();

// Iterate through the resources and display the contents to the console.
while(id.MoveNext())
Console.WriteLine("\n[{0}] \t{1}", id.Key, id.Value);

rr.Close();

}  

 -------------------------------------------------分割线------------------------------------------------------------------

用途:

 

asp.net 2.0中的App_GlobalResources可以用来解决本地化的问题,程序会根据浏览器的语言首选项自动判断显示出本地化的界面。

首先在App_GlobalResources新建resx资源文件。如:

不同语言的resx中项目应该具有相同的名称:

中文资源项

英文资源项

完了以后就可以使用这些名值对了

<asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:lang,userinfo%>" />

或者

<%=Resources.lang.userinfo%>

至此你可以更换浏览器的语言首选项来看看效果了。

接下去的问题是怎么通过程序本身的方式来更换语言项。

1. 可以往web.config中增加<globalization culture="en-US" uiCulture="en-US"/>来改变程序默认使用的语言项。

2.可以往global全局应用程序中增加如下代码

void Application_BeginRequest(Object sender, EventArgs e)
{
try
{
if (Request.Cookies["lang"] != null)
{
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(Request.Cookies["lang"].Value.ToString());
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Request.Cookies["lang"].Value.ToString());
}
}
catch (Exception)
{ }
}

即可通过设置变换cookie值来达到即时切换语言的目的,轻松的实现了程序的多语言。

 

原创粉丝点击