C# 项目本地化

avatar
作者
筋斗云
阅读量:0

下面展示如何给C#项目配置本地化的步骤

  1. 给项目创建一个文件夹,LocalizationResources
  2. 文件夹内创建两个资源文件Strings.rex,Strings.en.resx
  3. 资源文件内部创建两个资源
    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

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!