turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

No redirect according i18n.langtag using DefaultLanguage other than "en" #250

Closed ohager closed 8 years ago

ohager commented 8 years ago

In my MVC 4 application the user can change the language inside the WebApp using the i18n.langtag. When the default language is set to "en" the redirects according the user's preference occur normally and everything is working fine, but when setting another default language than "en" (i.e. "de") on Application_Start the user is always redirected to the default language without respecting the set preference.

// default setting
protected void Application_Start(){
     i18n.LocalizedApplication.Current.DefaultLanguage = "de";
    //...more config
}
// set language cookie
 private void SetLanguage(string languageTag)
        {
            LanguageTag lt = LanguageTag.GetCachedInstance(languageTag);
            if (lt.IsValid())
            {
                Response.Cookies.Add(new HttpCookie("i18n.langtag")
                {
                    Value = lt.ToString(),
                    HttpOnly = true,
                    Expires = DateTime.UtcNow.AddYears(1)
                });
            }
            else
            {
                var cookie = Response.Cookies["i18n.langtag"];
                if (cookie != null)
                {
                    cookie.Value = null;
                    cookie.Expires = DateTime.UtcNow.AddMonths(-1);
                }
            }
            HttpContext.SetPrincipalAppLanguageForRequest(lt);
        }
turquoiseowl commented 8 years ago

Have you tried the SetLanguage impl. here?:

https://github.com/turquoiseowl/i18n#explicit-user-language-selection

There's some extra code at the end...

turquoiseowl commented 8 years ago

No response so closing. Feel free to re-open with further comment.