public string GetLocalizedName() { string key = this.Name; if (string.IsNullOrEmpty(key)) { return(string.Empty); } return(LocalizationUtility.GetString(this.scope, key, this.culture.Name)); }
/// <summary> /// get the localized format message string. /// </summary> /// <param name="name"></param> /// <returns></returns> public override string FormatErrorMessage(string name) { //Contract.Assert(lclStrMng != null); Contract.Assert(this.cultureName != null); /* the ErrorMessageString usually equals ErrorMessage property * which you can specify when using this Attribute. */ /* Both errorMessage and name will be localized. * The errorMsaage usually use ValidationAttribute.ErrorMessageString property, * it usually equals ValidationAttribute.ErrorMessage property * which you can specify when using this Attribute. The parameter name usually * come from DisplayAttribute or the name of property to be validated by default. */ string localizedMessage = LocalizationUtility.GetString(this.scope, this.ErrorMessage, this.cultureName); string localizedName = LocalizationUtility.GetString(this.scope, name, this.cultureName); return(string.Format(this.cultureName, localizedMessage, localizedName)); }
static void Main(string[] args) { Initialize(); string scope = typeof(Program).Assembly.GetName().Name; string hello = LocalizationUtility.GetString("hello", scope, "zh-CN"); Console.WriteLine("zh-CN: {0}", hello); Console.WriteLine(); hello = LocalizationUtility.GetString("hello", scope, "en-US"); Console.WriteLine("en-US: {0}", hello); Console.WriteLine(); // 未指定cultureName,自动使用默认UI设置 hello = LocalizationUtility.GetString("hello", scope); Console.WriteLine("default UI culture: {0}", hello); Console.WriteLine(); Console.Write("按Enter键退出"); Console.ReadLine(); }
private string GetString(string key) { return(LocalizationUtility.GetString(key, this.GetType().Assembly.GetName().Name)); }