0

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Kyle
  • 17,317
  • 32
  • 140
  • 246

2 Answers2

1

Just to verify, try setting the language in a ActionFilterAttribute:

public class ChangeLanguageAttribute : ActionFilterAttribute
{

  public override void OnActionExecuting(
     ActionExecutingContext filterContext)
  {
     string languageCode = "fr";

     CultureInfo info =
       CultureInfo.CreateSpecificCulture(languageCode.ToString());

     Thread.CurrentThread.CurrentCulture = info;
     Thread.CurrentThread.CurrentUICulture = info;
  }
}

This is the exact code Im using, so I know this works.

Pbirkoff
  • 4,642
  • 2
  • 20
  • 18
  • Lucky, you can get the Request by using filterContext.HttpContext.Request – Pbirkoff Mar 07 '13 at 21:41
  • Problem found. It was not the above code, this works fine. The problem was that I forgot that it was in a filter so I had to apply the attribute to all my classes. – reaper_unique Aug 23 '13 at 09:24
0

this is the code i came up with, it should be on Application_OnBeginRequest :

public void Application_OnBeginRequest(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo NC = new System.Globalization.CultureInfo(1036, true);
            NC.NumberFormat.CurrencyDecimalDigits = 2;
            NC.NumberFormat.CurrencySymbol = "euro";
            NC.NumberFormat.CurrencyDecimalSeparator = ".";
            NC.NumberFormat.PercentDecimalSeparator = ".";
            NC.NumberFormat.NumberDecimalSeparator = ".";
            NC.NumberFormat.CurrencyGroupSeparator = "";
            NC.NumberFormat.PercentGroupSeparator = "";
            NC.NumberFormat.NumberGroupSeparator = "";
            System.Threading.Thread.CurrentThread.CurrentCulture = NC;
        }
Chtioui Malek
  • 11,197
  • 1
  • 72
  • 69