資源與本地化 System.Resources

1、建立資源文件

能夠將字符串、圖像或對象數據等資源包含在資源文件中,方便應用程序使用。建立資源文件的方法: html

一、手動或使用IDE工具自動生成XML 資源 (.resx) 文件。

      建立一個包含字符串資源的文本文件。或建立一個包含字符串、圖像或對象數據的 XML 資源 (.resx) 文件。web

使用 Visual Studio 建立一個資源文件並將其包含在項目中。 Visual Studio 提供一個資源編輯器,藉助該編輯器,可添加、刪除和修改資源。 編譯時,資源文件會自動轉換成二進制 .resources 文件,並嵌入應用程序程序集或附屬程序集中。 編程

  注意:內部使用資源文件生成器 (Resgen.exe) 將文本文件轉換成二進制資源 (.resources) 文件。 而後使用語言編譯器將這個二進制資源文件嵌入可執行應用程序或應用程序庫,或者使用程序集連接器 (Al.exe) 將這個二進制資源文件嵌入附屬程序集。c#

二、以編程方式建立一個 XML 資源 (.resx) 文件。 使用ResXResourceWriter類

      能夠建立一個 .resx 文件、枚舉其資源並按名稱檢索特定資源。 瀏覽器

ResXResourceWriter rw = new ResXResourceWriter("Demo.rex");
rw.AddResource("Logo", Image.FromFile("logo.jpg");
rw.AddResource("Title", "Proce c#");
rw.Generate();
rw.Close();

三、以編程方式建立一個二進制資源 (.resources) 文件,使用ResourceWriter類。 不推薦

      而後使用語言編譯器將該文件嵌入可執行應用程序或應用程序庫,或者使用程序集連接器 (Al.exe) 將這個二進制資源文件嵌入附屬程序集。方法同上。 cookie

2、Winform可使用ResourceManager讀取資源文件,不推薦

Winform使用資源文件(Demo.resx;Demo.en-us.rex)編輯器

//多語言
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-us");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");

Assembly asm = Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("Demo.Properties.Resources", asm);
//Demo是你程序的命名空間,Demo.Properties 是資源類Resources的命名空間, 資源文件名稱不帶擴展名
this.logo.Image = (Image)rm.GetObject("Logo");//Get**方法具備重載方法,第二個參數爲CultureInfo對象
this.Title.Text = rm.GetString("Title");    //資源文件名稱不帶擴展名

使用強類型可直接使用類,不須要ResourceManageride

logo.Image = Demo.Logo;
 Tilte.Text = Demo.Title

3、Winform程序界面多語言切換,ComponentResourceManager類

ComponentResourceManager類繼承自ResourceManager類。工具

Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");

ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
resources.ApplyResources(this, "$this");
AppLang(form, resources);

#region AppLang for control
/// <summary>
/// 遍歷窗體全部控件,針對其設置當前界面語言
/// </summary>
/// <param name="control"></param>
/// <param name="resources"></param>
private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
{
    if (control is MenuStrip)
    {
        resources.ApplyResources(control, control.Name);
        MenuStrip ms = (MenuStrip)control;
        if (ms.Items.Count > 0)
        {
            foreach (ToolStripMenuItem c in ms.Items)
            {
                AppLang(c, resources);
            }
        }
    }

    foreach (Control c in control.Controls)
    {
        resources.ApplyResources(c, c.Name);
        AppLang(c, resources);
    }
}
#endregion

#region AppLang for menuitem
/// <summary>
/// 遍歷菜單
/// </summary>
/// <param name="item"></param>
/// <param name="resources"></param>
private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
{
    if (item is ToolStripMenuItem)
    {
        resources.ApplyResources(item, item.Name);
        ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
        if (tsmi.DropDownItems.Count > 0)
        {
            foreach (ToolStripMenuItem c in tsmi.DropDownItems)
            {
                AppLang(c, resources);
            }
        }
    }
}
   #endregion

4、使用GetManifestResourceStream讀取資源文件

一、將本地要加入的資源文本(視頻,圖片,文本或其它)加入項目。好比咱們如今加入一個up.bmp的圖片到項目中,且放在文件夾Resources下面,
二、將up.bmp右鍵選擇「屬性」的「生成操做」設置爲"嵌入的資源"。網站

注意:訪問資源的名稱規則爲:項目命名空間.資源文件所在文件夾名.資源文件名

Assembly assm = Assembly.getGetExecutingAssembly();
Stream stream = assm.GetManifestResourceStream("CreateDatabase.Resources.aa.txt");
//文本
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string str = sr.ReadToEnd();
//XML文件
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
//圖片
Image img = Image.FromStream(stream);

5、ASP.NET Web網站本地化

一、重寫InitializeCulture方法。

      在Global.asax中的Application_BeginRequest獲取或設置語言,每次頁面的請求都首先運行這個方法,而後再運行具體頁面的InitializeCulture重載方法。

附上一張圖,看看頁面方法和事件的執行順序

protected override void InitializeCulture()
{
    String curCulture = Request.QueryString["currentculture"];
    if (!String.IsNullOrEmpty(curCulture))
    {
        HttpCookie cookie = new HttpCookie("preferCulture", curCulture);
        cookie.Expires = DateTime.Today.AddDays(7);
        Response.SetCookie(cookie);//設置Cookie
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(s);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(s);
    }
    else
    {
        HttpCookie cookie = new Request.Cookies["preferCulture"];//取得Cookie
        if (cookie != null)
        {
            curCulture = cookie.Value.ToString();

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(curCulture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(curCulture);
        }
    }
}

除了經過IE瀏覽器和線程獲取語言設置,其實咱們能夠本身設置使用哪一種語言。
1)經過在每一個頁面裏的Page節指定

<%@ Page Culture="en-us" UICulture="en-us" %>

如上所設,該頁將使用en-us的語言設置。
注意:這只是個概要式寫法,實際的頁面中的Page通常都包含更多的屬性。
2)經過在Web.Config裏的globalization節指定

<system.web>
<globalization Culture="en-us" UICulture="en-us" />
</system.web>

二、添加資源語言文件

      在項目App_GlobalResource文件夾添加Demo.resx和Demo.en-us.rex兩個文件

在首頁中添加:

<a href="?currentculture=zh-cn">中文(中國)</a>
&nbsp;
<a href="?currentculture=en-us">English(USA)</a>

三、獲取全局資源(GlobalResources)的方式:

      你能從 ~\App_GlobalResources\MyMessages.resx 獲得的資源經過:

一、產生的封裝代碼 :

string message = Resources.MyMessages.Hello;

二、資源表達式

<asp:Label Text="<%$ Resources: MyMessages, Hello %>" />

三、GetGlobalResourceObject方法

string message = GetGlobalResourceObject("MyMessages", "Hello");

注意:本地資源的獲取方法:

能從 ~\App_LocalResources\default.aspx.resx 獲得的資源經過:

一、資源表達式:

<asp:Label Text="<%$ Resources: Hello %>" />

二、meta:resourceKey :

<asp:Label meta:resourceKey="labelResourceKey" />

三、GetLocalResourceObject方法:

string message = GetLocalResourceObject("Hello"); "

頁面級別的本地化:https://www.cnblogs.com/zhaohwi/archive/2008/08/25/1276134.html

相關文章
相關標籤/搜索