阅读量:0
下面展示如何给C#项目配置本地化的步骤
- 给项目创建一个文件夹,LocalizationResources
- 文件夹内创建两个资源文件Strings.rex,Strings.en.resx
- 资源文件内部创建两个资源
Strings.rex:
Name:姓名
Age:年龄
Strings.en.resx
Name:Name
Age:Age
接下来创建一个测试类
public class Student { public string Name { get; set; } public int Age { get; set; } public Student(string name,int age) { Name = name; Age = age; } public override string ToString() { //ToString方法中不要写死,用本地化中的字段来替换 return LocalizationResources.Strings.Name+":"+Name+"\t"+ LocalizationResources.Strings.Age+":"+Age; }
尝试用两种语言输出
static void Main(string[] args) { var chineseCulture = CultureInfo.CurrentCulture; Thread.CurrentThread.CurrentUICulture = chineseCulture; Thread.CurrentThread.CurrentCulture = chineseCulture; Print(); //切成英文 var englishCulture = CultureInfo.GetCultureInfo("en"); Thread.CurrentThread.CurrentCulture = englishCulture; Thread.CurrentThread.CurrentUICulture = englishCulture; Print(); } private static void Print() { var stu = new Student("xiaoming", 19); Console.WriteLine(stu.ToString()); }
输出结果:
姓名:xiaoming 年龄:19
Name:xiaoming Age:19