3

I am having a problem using languages in asp.net MVC 5. I wrote a base control to change the culture setting according to a cookie. the code is working on views but does not work on either controllers or Model data annotations.

the base Controller is:

public class BaseController : Controller
    {
        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            if (Request.Cookies["Language"] == null || !Validate.IsLanguage(Cookies.CurrentLanguageCode))
            {
                HttpCookie cookie_new = new HttpCookie("Language", "en");
                cookie_new.Expires = DateTime.Now.AddYears(1);
                Response.SetCookie(cookie_new);

                Thread.CurrentThread.CurrentCulture =
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en");
            }
            else
            {
                Thread.CurrentThread.CurrentCulture =
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Cookies.CurrentLanguageCode);
            }
            return base.BeginExecuteCore(callback, state);
        }
    }

the controller where I am receiving the Error:

    ViewBag.ErrorTitle = Thread.CurrentThread.CurrentCulture + "1";
    ViewBag.Description = "";
    switch (id)
    {
        case 0:
            ViewBag.ErrorTitle = Resource.Lang.ErrorTitlePageNotFound;
            ViewBag.Description = Resource.Lang.ErrorDescriptionPageNotFound;
            break;

and the error is:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "App_GlobalResources.Lang.resources" was correctly embedded or linked into assembly at compile time, or that all the satellite assemblies required are loadable and fully signed.

The cookie does correctly exist, the language default set to Default which is en-US, and I have one resource file: Lang.resx

I am getting the same error on Data annotation. I wrote also a baseClass and let all the models inheriting from it but still getting the same problems.

halfer
  • 19,824
  • 17
  • 99
  • 186
Samy Sammour
  • 2,298
  • 2
  • 31
  • 66
  • Shouldn't cultureInfo be like "en-GB" and not just "en"? Check http://stackoverflow.com/questions/8226514/best-place-to-set-currentculture-for-multilingual-asp-net-mvc-web-applications – alexsuslin Nov 23 '16 at 10:00

0 Answers0