c#多语言实现

也许你会有多语言这样的需求,后期方便添加语言,基于这样的需求写了个辅助类;

先看代码:

  1 public class LanguageHelper
  2 {
  3     private static Dictionary<string, string> m_lstLanguage = new Dictionary<string, string>();
  4     private static Dictionary<string, FontFamily[]> m_lstLanguageFontFamily = new Dictionary<string, FontFamily[]>();
  5     /// <summary>
  6     /// 语言改变事件委托
  7     /// </summary>
  8     /// <param name="lang">语言</param>
  9     public delegate void LanguageChangedHandler(string lang);
 10     /// <summary>
 11     /// 语言改变事件
 12     /// </summary>
 13     public static event LanguageChangedHandler LanguageChanged;
 14     /// <summary>
 15     /// 当前语言
 16     /// </summary>
 17     public static string Language { get; private set; }
 18     /// <summary>
 19     /// 语言文件所在文件夹
 20     /// </summary>
 21     public static string LanguageDirectory { get; private set; } = "Language";
 22     /// <summary>
 23     /// 初始化
 24     /// </summary>
 25     /// <param name="langDirectory">语言文件所在文件夹</param>
 26     public static void InitLanguage(string langDirectory)
 27     {
 28         LanguageDirectory = langDirectory;
 29     }
 30     /// <summary>
 31     /// 初始化语言对应使用的字体样式(可多次调用设置)
 32     /// </summary>
 33     /// <param name="language">语言</param>
 34     /// <param name="fontFamily">字体样式列表,0:常规字体,1:加粗字体(可空)</param>
 35     public static void InitLanguageFont(string language, FontFamily[] fontFamily)
 36     {
 37         m_lstLanguageFontFamily[language] = fontFamily;
 38     }
 39     /// <summary>
 40     /// 加载语言文件
 41     /// </summary>
 42     /// <param name="lang">语言文件名称,如zh-CN,UTF-8编码</param>
 43     /// <returns>是否加载成功</returns>
 44     public static bool LoadLanguage(string lang = "zh-CN")
 45     {
 46         if (File.Exists(Path.Combine(LanguageDirectory, lang + ".json")))
 47         {
 48             Language = lang;
 49             m_lstLanguage = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(Path.Combine(LanguageDirectory, lang + ".json"), Encoding.UTF8));
 50             if (LanguageChanged != null)
 51             {
 52                 LanguageChanged(lang);
 53             }
 54             return true;
 55         }
 56         return false;
 57     }
 58 
 59     /// <summary>
 60     /// 使用关键词获取语言文本
 61     /// </summary>
 62     /// <param name="strKey">关键词</param>
 63     /// <returns>文本内容</returns>
 64     public static string GetLanguageText(string strKey)
 65     {
 66         if (m_lstLanguage.ContainsKey(strKey))
 67         {
 68             return m_lstLanguage[strKey];
 69         }
 70         return strKey;
 71     }
 72 
 73     /// <summary>
 74     /// 根据语言获取对应的字体
 75     /// </summary>
 76     /// <param name="lang">语言</param>
 77     /// <param name="blnBold">是否粗体</param>
 78     /// <returns></returns>
 79     public static FontFamily GetFontFamilyByLanguage(string lang, bool blnBold = false)
 80     {
 81         if (m_lstLanguageFontFamily.ContainsKey(lang))
 82         {
 83             var fs = m_lstLanguageFontFamily[lang];
 84             if (blnBold)
 85             {
 86                 if (fs.Length > 1)
 87                 {
 88                     return fs[1];
 89                 }
 90             }
 91             return fs[0];
 92         }
 93         return new FontFamily("微软雅黑");
 94     }
 95     /// <summary>
 96     /// 根据语言获取对应的字体,以1080为基准进行计算
 97     /// </summary>
 98     /// <param name="size"></param>
 99     /// <param name="lang"></param>
100     /// <returns></returns>
101     public static Font GetFontByLanguage(float size, string lang)
102     {
103         return GetFontByLanguage(size, lang, 1080);
104     }
105     /// <summary>
106     /// 根据语言和显示宽度获取对应的字体
107     /// </summary>
108     /// <param name="size">原始文字大小</param>
109     /// <param name="lang">语言</param>
110     /// <param name="width">当前缩放后界面显示宽度,根据原始宽度来进行比例缩放</param>
111     /// <param name="oldWidth">原始宽度</param>
112     /// <param name="blnBold">是否加粗</param>
113     /// <returns>字体</returns>
114     public static Font GetFontByLanguage(float size, string lang, int width, float oldWidth = 1080.0f, bool blnBold = false)
115     {
116         var ff = GetFontFamilyByLanguage(lang, blnBold);
117         return new Font(ff, size * width / oldWidth, blnBold ? FontStyle.Bold : FontStyle.Regular);
118     }
119     /// <summary>
120     /// 根据字体和显示宽度获取对应的字体
121     /// </summary>
122     /// <param name="font">原始文字字体</param>
123     /// <param name="width">当前缩放后界面显示宽度,根据原始宽度来进行比例缩放</param>
124     /// <param name="oldWidth">原始宽度</param>
125     /// <returns>字体</returns>
126     public static Font GetFontByFont(Font font, int width, float oldWidth = 1080.0f)
127     {
128         return new Font(font.FontFamily, font.Size * width / oldWidth, font.Style);
129     }
130 }
View Code

调用代码

//多语言测试
LanguageHelper.InitLanguage("Language");
//设置语言对应字体的时候,也可以使用FontHelper类加载的文件字体
LanguageHelper.InitLanguageFont("zh-CN", new System.Drawing.FontFamily[] { new System.Drawing.FontFamily("微软雅黑"), new System.Drawing.FontFamily("微软雅黑") })
LanguageHelper.InitLanguageFont("zh-TW", new System.Drawing.FontFamily[] { new System.Drawing.FontFamily("微软雅黑"), new System.Drawing.FontFamily("微软雅黑") })
LanguageHelper.LanguageChanged += (lang) =>
{
    Console.WriteLine("语言改变:" + lang);
    Console.WriteLine("-------------------");
    //重新加载语言
    Console.WriteLine(LanguageHelper.GetLanguageText("欢迎"));
    Console.WriteLine(LanguageHelper.GetLanguageText("无操作时间"));
    Console.WriteLine(LanguageHelper.GetLanguageText("等待时间"));
    Console.WriteLine(LanguageHelper.GetLanguageText("退出系统"));
    Console.WriteLine(LanguageHelper.GetLanguageText("错误提示"));
    //根据语言设置字体,一般用于语言切换,节目大小改变时,重新设置字体
    Font f1 = LanguageHelper.GetFontByLanguage(15, "zh-CN", 800, 1080, true);//原始15号字体,中文,界面宽度800,设计时原始宽度1080,是否加粗
    Console.WriteLine("缩放后字体:" + f1);
};
Console.WriteLine();
LanguageHelper.LoadLanguage("zh-CN");
Console.WriteLine();
LanguageHelper.LoadLanguage("zh-TW");
Language.LanguageHelper.LoadLanguage("zh-TW");
View Code

多语言文件格式

 语言配置文件为json格式,如zh-CN.json,UTF-8编码;
 格式为:{"key1":"value1","key2":"value2"};
{
  "欢迎": "欢迎",
  "无操作时间": "无操作时间/秒",
  "等待时间": "等待时间/秒",
  "退出系统": "退出系统",
  "错误提示": "错误提示"
}

你可以在界面上注册事件LanguageChanged,在事件中修改界面的文字,配合GetFontByLanguage函数可以进行方便设置字体,效果更好

posted @ 2024-12-20 16:39  冰封一夏  阅读(10)  评论(0编辑  收藏  举报
HZHControls控件库官网:https://www.hzhcontrols.cn