azza idz
azza idz

Reputation: 663

Can't change application language all the way

I have three resx files that i use in application. By default it's english, but when i want to change application language only UI is changed. Controllers seems to stay fixed on english, did i overlook something?

Resource file properties (set to public)

enter image description here

I change language on _ViewStart.cshtml like this (language is EN or DE)

Culture = UICulture = language;

I also tried to change language in controller upon login but it doesn't have any effect (if i comment out ViewStart code following lines do nothing, application is still on english)

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");

I don't have anything language related in web.config atm. But if i put globalization attribute on German application language is changed but i need to change it from within application so i can't use globalization attribute. Any ideas?

As an example, if i use this in my view @Resource.Language i will always get correctly selected user language, but if i use same line in controller and sent string to my view it's always on english (ignores user choice)

Upvotes: 1

Views: 1210

Answers (1)

Aaron Newton
Aaron Newton

Reputation: 2316

I suspect the problem is that you need to implement this per request, rather than just in the login controller. Have a look at the solution here: https://stackoverflow.com/a/1561583/201648 (you might also like to have a read of http://www.ryadel.com/en/setup-a-multi-language-website-using-asp-net-mvc/#Resource_Files_inMVC). You'll need to create an attribute and decorate your class or actions with this. If you don't want to modify the route values as they have done here, you could write the culture-info value to a cookie on login and then get this value out of the cookie in the DataAnnotation, e.g. in the login controller:

HttpCookie iln8Cookie= new HttpCookie("iln8Cookie");
iln8Cookie["Language"] = "de"; //Replace with the user's language
iln8Cookie["Culture"] = "DE"; //Replace with the user's culture
iln8Cookie.Expires = DateTime.Now.AddDays(90);
HttpContext.Response.SetCookie(iln8Cookie);

Then in the data annotation, read the cookie value, e.g.

public class InternationalizationAttribute : ActionFilterAttribute {

    public override void OnActionExecuting(ActionExecutingContext filterContext) {

        var cookie = filterContext.HttpContext.Request.Cookies["iln8Cookie"];

        string language = cookie != null ? cookie.Values["Language"] : "en";
        string culture = cookie != null ? cookie.Values["Culture"] : "AU";

        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));

    }
}

Then decorate the class or action:

[Internationalization]
public class HomeController : Controller {

Also, depending on how the login works for this application, the language and culture information may already be available to you from the user object, i.e.

filterContext.HttpContext.Request.UserLanguages[0]

I've confirmed this works using the following test project which you can clone from BitBucket:

https://bitbucket.org/mrblurgle/test-internationalisation

Upvotes: 1

Related Questions