首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 開發多語言版本程式:資源檔與 CultureInfo 實戰

     編輯:關於C語言

專案目標

建立一個支援中英雙語切換的 WinForms 應用,包含單一表單、三個控制項,根據用戶設定動態切換介面語言,設定持久化至 INI 檔。

開發環境

  • OS: Windows 2000 Professional
  • IDE: Microsoft Visual Studio .NET 2002
  • 專案路徑: F:\Csharp\MyApp

實施步驟

1. 新建專案與佈局

  • 專案名:MyApp
  • 表單 Form1 放置:MainMenu、Label1、Button1
  • 選單結構:語言 → 中文 / English(RadioCheck = true)

2. 建立設定檔

MyAppConfig.ini 單行儲存當前語言:English 或 Chinese。

3. 建立資源檔

.NET 三種資源格式:

  • .txt:純文字 Name=Value,僅支援字串
  • .resx:XML 格式,支援字串、圖片等可序列化物件
  • .resources:二進位格式,可嵌入組件或編譯為衛星組件

本例僅需字串,採用 .txt + Resgen.exe 轉換:

Resource_en.txt (預設後備資源)
menuItem1_Name=Language
button1_Name=Exit
label1_Text=This is English version!

Resource_zh.txt (UTF-8 編碼儲存)
menuItem1_Name=語言
button1_Name=退出
label1_Text=這是中文版!

4. 編譯資源檔

F:\Csharp\MyApp> resgen Resource_en.txt,MyApp.resources
F:\Csharp\MyApp> resgen Resource_zh.txt,MyApp.zh-CN.resources
  • MyApp.resources:預設(英文)後備資源
  • MyApp.zh-CN.resources:中文資源,zh-CN 為文化特性代碼

5. 核心代碼實現

using System.Globalization;
using System.Resources;
using System.Threading;
using System.IO;

public class Form1 : Form
{
    CultureInfo ci = new CultureInfo("");
    ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("MyApp", ".", null);

    private void ReadResources()
    {
        this.menuItem1.Text = rm.GetString("menuItem1_Name", ci);
        this.button1.Text = rm.GetString("button1_Name", ci);
        this.label1.Text = rm.GetString("label1_Text", ci);
        this.Refresh();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (StreamReader sr = File.OpenText("MyAppConfig.ini"))
        {
            string lang = sr.ReadLine();
            if (lang == "Chinese")
            {
                ci = new CultureInfo("zh-CN");
                menuItem2.Checked = true;
            }
            else
            {
                ci = new CultureInfo("en");
                menuItem3.Checked = true;
            }
        }
        this.ReadResources();
    }

    private void SaveConfig(string lang)
    {
        using (StreamWriter sw = File.CreateText("MyAppConfig.ini"))
            sw.WriteLine(lang);
    }

    private void menuItem2_Click(object sender, EventArgs e) // 中文
    {
        SaveConfig("Chinese");
        ci = new CultureInfo("zh-CN");
        menuItem2.Checked = true; menuItem3.Checked = false;
        ReadResources();
    }

    private void menuItem3_Click(object sender, EventArgs e) // 英文
    {
        SaveConfig("English");
        ci = new CultureInfo("en");
        menuItem2.Checked = false; menuItem3.Checked = true;
        ReadResources();
    }

    private void button1_Click(object sender, EventArgs e) => this.Close();
}

6. 編譯與部署

csc /out:MyApp.exe form1.cs

VS.NET 執行時,需將 MyAppConfig.ini、MyApp.resources、MyApp.zh-CN.resources 複製至 bin\Debug\ 目錄。

現代化改進建議

  • 使用 .resx + Visual Studio 內建資源設計器,支援強類型存取(Resources.String1)
  • 採用衛星組件模式:主組件含預設資源,各語言獨立 MyApp.resources.dll 部署於 zh-CN\、en\ 子目錄
  • 利用 Thread.CurrentThread.CurrentUICulture 自動匹配系統語言
  • 設定檔改用 application.settings 或 JSON,支援型別安全與複雜結構
  • ASP.NET Core 環境下使用 IStringLocalizer + 資源檔,支援 DI 與中介軟體文化特性判斷

總結

本文展示了 .NET 1.x 時代「檔案型資源管理器」的經典做法:手工編寫 .txt → Resgen.exe 編譯 → ResourceManager 運行時載入 → CultureInfo 切換。雖工具鏈已現代化,但核心概念——資源與代碼分離、文化特性驅動載入、後備機制——至今仍是 .NET 國際化的基石。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved