我想用西类牙语显示模型错误消息,我已经在资源文件中定义了这些字符串。我已经使用 Razor 语法对页面上的其他字符串做了同样的事情,但是没有选择来自 ViewModel 注释的字符串。
它实际上是在选择默认值——英语。所以我的猜测是可能未检测到语言/文化,但页面上的其他字符串以西类牙语显示
//Spanish: El campo {0} se requiere
//English: The {0} field is required <--- this comes out always irrespective of set language
[Required(ErrorMessageResourceName = "ErrorMessage_Required",
ErrorMessageResourceType = typeof(GlobalResources.Resources))]
[Display(Name = "CardNumber", ResourceType = typeof(GlobalResources.Resources) )]
public string CardNumber { get; set; }
我在 Controller 中设置了语言
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
HttpCookie cookie = Request.Cookies["lang"];
string lang = cookie != null ? cookie.Value : "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}
如何将文化设置扩展到 ViewModel?
更新 类似的帖子:MVC3 globalization: need global filter before model binding
更新
在我的浏览器设置中更改首选语言使其工作。这意味着模型属性正在使用不受 System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
影响的设置。 有办法做到这一点吗? - 还在寻找....
更新:将代码移至Application_AcquireRequestState
似乎可以解决问题。
protected void Application_AcquireRequestState()
{
HttpCookie cookie = Request.Cookies["lang"];
string lang = cookie != null ? cookie.Value : "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}
The explanation I got ,也在此问题中发布的链接中发现,模型使用 Controller 重写方法中设置的区域性为时已晚,因为绑定(bind)已经在调用方法之前发生。 This link was helpful
最佳答案
尝试使用下面的类
public sealed class LanguageManager
{
/// <summary>
/// Default CultureInfo
/// </summary>
public static readonly CultureInfo DefaultCulture = new CultureInfo("en-US");
/// <summary>
/// Available CultureInfo that according resources can be found
/// </summary>
public static readonly CultureInfo[] AvailableCultures;
static LanguageManager()
{
List<string> availableResources = new List<string>();
string resourcespath = Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "App_GlobalResources");
DirectoryInfo dirInfo = new DirectoryInfo(resourcespath);
foreach (FileInfo fi in dirInfo.GetFiles("*.*.resx", SearchOption.AllDirectories))
{
//Take the cultureName from resx filename, will be smt like en-US
string cultureName = Path.GetFileNameWithoutExtension(fi.Name); //get rid of .resx
if (cultureName.LastIndexOf(".") == cultureName.Length - 1)
continue; //doesnt accept format FileName..resx
cultureName = cultureName.Substring(cultureName.LastIndexOf(".") + 1);
availableResources.Add(cultureName);
}
List<CultureInfo> result = new List<CultureInfo>();
foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
//If language file can be found
if (availableResources.Contains(culture.ToString()))
{
result.Add(culture);
}
}
AvailableCultures = result.ToArray();
CurrentCulture = DefaultCulture;
if (!result.Contains(DefaultCulture) && result.Count > 0)
{
CurrentCulture = result[0];
}
}
/// <summary>
/// Current selected culture
/// </summary>
public static CultureInfo CurrentCulture
{
get { return Thread.CurrentThread.CurrentCulture; }
set
{
Thread.CurrentThread.CurrentUICulture = value;
Thread.CurrentThread.CurrentCulture = value;
}
}
}
最后像下面这样设置文化。
LanguageManager.CurrentCulture = new CultureInfo("Your culture");
现在覆盖文化
关于c# - 错误消息 : Model not picking language-specific resource file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16200309/