日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#中英文切换实例

發布時間:2023/12/18 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#中英文切换实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.創建兩個資源文件

Resource.en-US.resx? ? ??Resource.zh-CN.resx? ? 注意中間部分每種語言文件名固定,后綴名是.resx,首部分是自定義的名字

2.創建讀寫工具類

上面兩個資源文件都是在Utility項目集中

當另一個含有UI的項目調用次Utility里的資源時需要引用此項目,然后編譯后在UI的項目debug中會自動生成嵌入的文件如:

debug\en-US\Utility.resources.dll? ?和debug\zh-CN\Utility.resources.dll?

注意:

ResourceManager rm = new ResourceManager("Utility.Resource", Assembly.GetExecutingAssembly());的調用就要以命名空間+資源文件首部名的形式。

?

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;

namespace WindowsFormsApplication1
{
? ? public class Utility
? ? {

? ? ? ? /// <summary>
? ? ? ? /// Set current culture by name
? ? ? ? /// </summary>
? ? ? ? /// <param name="name">name</param>
? ? ? ? public static void SetCurrentCulture(string name)
? ? ? ? {
? ? ? ? ? ? if (string.IsNullOrEmpty(name))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? name = "CN";
? ? ? ? ? ? }

? ? ? ? ? ? Thread.CurrentThread.CurrentCulture = new CultureInfo(name);
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// Get string by id
? ? ? ? /// </summary>
? ? ? ? /// <param name="id">id</param>
? ? ? ? /// <returns>current language string</returns>
? ? ? ? public static string GetString(string id)
? ? ? ? {
? ? ? ? ? ? string strCurLanguage = "";

? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ResourceManager rm = new ResourceManager("Utility.Resource", Assembly.GetExecutingAssembly());
? ? ? ? ? ? ? ? CultureInfo ci = Thread.CurrentThread.CurrentCulture;
? ? ? ? ? ? ? ? strCurLanguage = rm.GetString(id, ci);
? ? ? ? ? ? }
? ? ? ? ? ? catch(Exception ee)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? strCurLanguage = "No id:" + id + ", please add.";
? ? ? ? ? ? }

? ? ? ? ? ? return strCurLanguage;
? ? ? ? }

? ? }
}
3.界面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }

? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Utility.SetCurrentCulture("zh-CN");
? ? ? ? ? ? this.textBox1.Text = Utility.GetString("a1");
? ? ? ? }

? ? ? ? private void button2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Utility.SetCurrentCulture("en-US");
? ? ? ? ? ? this.textBox1.Text = Utility.GetString("a1");
? ? ? ? }
? ? }
}
?

總結

以上是生活随笔為你收集整理的c#中英文切换实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。