I've been trying to follow the blog post found here. I've added a new assembly that hosts my resource files (I used a separate assembly as the resources may need to be shared between multiple projects). I have added the following to my web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="MyResources.Resources"/> <!-- New entry for resources -->
</namespaces>
</pages>
</system.web.webPages.razor>
And I have gone though and added resource strings to a few files for testing purposes. Now the problem that I seem to be running into, is that I cannot set the resource to be anything other than the default. So for example, in the generated resource file designer there is the following:
/// <summary>
/// Looks up a localized string similar to Log in was unsuccessful. Please correct the errors and try again..
/// </summary>
public static string Account_LoginUnsuccessful {
get {
return ResourceManager.GetString("Account_LoginUnsuccessful", resourceCulture);
}
}
If I set a breakpoint in this method, resourceCulture
is NEVER anything but null. Even though I have tried the following:
In Global.asax.ca:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
var culture = new System.Globalization.CultureInfo("fr");
// Modify current thread's cultures
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture.Name);
} // End of Application_AcquireRequestState
In a base mvc controller that all of my other controller inherit:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
SetCulture(requestContext.HttpContext.Request);
base.Initialize(requestContext);
}
protected override void ExecuteCore()
{
SetCulture(Request);
base.ExecuteCore();
}
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
SetCulture(requestContext.HttpContext.Request);
base.Execute(requestContext);
}
protected override IAsyncResult BeginExecute(System.Web.Routing.RequestContext requestContext, AsyncCallback callback, object state)
{
SetCulture(requestContext.HttpContext.Request);
metrics = Metrics.BeginTimer();
return base.BeginExecute(requestContext, callback, state);
}
private void SetCulture(HttpRequestBase Request)
{
string cultureName = "fr";
// Validate culture name
cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe
// Modify current thread's cultures
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
Now, to my understanding setting the threading current culture should be causing my resource files culture to change. I can't seem to get this working of the life of me (hence why I have tried setting the cultire in about ten different locations).
Any suggestions on what I am doing wrong here?