C# 处理多语言的基础类
1 using Newtonsoft.Json; 2 using Newtonsoft.Json.Linq; 3 using System.IO; 4 using System.Text; 5 6 namespace TestDemo 7 { 8 /// <summary> 9 /// 处理多语言的基础类 10 /// </summary> 11 public class Lang 12 { 13 /// <summary> 14 /// 语言文件 15 /// </summary> 16 private readonly JObject lang; 17 18 /// <summary> 19 /// 构造函数 20 /// </summary> 21 /// <param name="fileName"></param> 22 public Lang(string filePath) 23 { 24 // 读取文件 25 string str = string.Empty; 26 using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8)) 27 { 28 str = sr.ReadToEnd(); 29 } 30 lang = JObject.Parse(str); 31 } 32 33 /// <summary> 34 /// 通过key取得文本 35 /// </summary> 36 /// <param name="key"></param> 37 /// <returns></returns> 38 public string this[string key] 39 { 40 get 41 { 42 string value = string.Empty; 43 if (lang.ContainsKey(key)) 44 { 45 value = lang[key].ToString(); 46 } 47 return value; 48 } 49 } 50 51 /// <summary> 52 /// 取得多语言文件 53 /// </summary> 54 /// <returns></returns> 55 public string GetLanguageFile() 56 { 57 return JsonConvert.SerializeObject(lang); 58 } 59 60 } 61 }