我想以西班牙语显示模型错误消息,我已在资源文件中定义了这些字符串。我使用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; }
我在控制器中设置了语言
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);
}
如何将文化设置扩展到ViewModels?
更新 类似的帖子: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,也发现在此问题中发布的链接中,模型使用在控制器重写方法中设置的文化为时已晚,因为在调用方法之前绑定已经发生。 This link was helpful
答案 0 :(得分:3)
尝试使用以下课程
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");
现在覆盖文化
答案 1 :(得分:1)
确保您设置 CurrentCulture
和CurrentUICulture
。我怀疑CurrentCulture
实际上是资源文件正在查看的内容:
//I've taken off System.Threading, add a using to it (aids readability)
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);