1

I have the following resource files in my ASP.NET MVC application to stored form validation error message for both English and Spanish

ErrorMessages.resx -- English Error Messages
ErrorMessages.sp.resx -- Spanish Error Messages

In my models that require the System.ComponentModel.DataAnnotations Required Attributes they are marked like this:

[Required(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName)]
public string MyProperty { get; set; }

In my controller, I am reading a JavaScript cookie that contains the user's selected language on the site and programmatically set the 'CultureInfo' of the current thread and the HTTP session:

public ActionResult MyController() 
{
    var language = HttpContext.Request.Cookies["language"].Value;

    if(language.Equals("english")) 
    {
        HttpContext.Session["culture"] = "en-US";
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    }
    else 
    {
        HttpContext.Session["culture"] = "es-US";
        Thread.CurrentThread.CurrentCulture = new CultureInfo("es-US");
    }
}  

How do I set the appropriate resource file once the CultureInfo value has been modified?

2
  • 1
    spanish .resx file should *.es.resx, not *.sp.resx Commented Apr 25, 2019 at 1:59
  • I renamed my files to the appropriate CultureInfo value. Thanks for the suggestion Commented Apr 25, 2019 at 16:37

2 Answers 2

1

Spanish .resx file should be *.es.resx, not *.sp.resx.

In order to resolve translations from an appropriate .resx file you need to assign Thread.CurrentThread.CurrentUICulture, not Thread.CurrentThread.CurrentCulture

Sign up to request clarification or add additional context in comments.

5 Comments

When I switched to CurrentUICulture property, it is still using the english ErrorMessages.resx file. I need my app to know when CurrentUICulture = "es-US" use the ErrorMessages.sp.resx
I did get it to work. I modified my code to the following: Thread.CurrrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("es-US");
@MichaelKniskern why do not you post an answer, so others can benefit from it too ?
I just updated the question with what I did to get it to work.
@MichaelKniskern, you suppose to post it as an answer, not add an answer to the body of the question.
1

Using @Koryakinp suggestion, I modified the controller code to the following:

var language = HttpContext.Request.Cookies["language"].Value;

if(language.Equals("english"))
{
    HttpContext.Session["culture"] = "en-US";
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
}

Also, I had to restructure the .resx files. I created a .resx named "ErrorMessages" as the base .resx and renamed the existing .resx files:

ErrorMessages.resx
ErrorMessages.en-US.resx
ErrorMessages.es-US.resx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.