[RESOLVED]Multilanguage routing and language from a browser.

How can I add default culture to the RouteValueDictionary(): r.Defaults.Add("culture", "sv") in this code:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

            foreach(var r in from Route r in routes where !(r.RouteHandler is SingleCultureRouteHandler) select r)
            {
                r.RouteHandler = new MultiCultureRouteHandler();
                
                r.Url = "{culture}/" + r.Url;

                // Adding default culture
                if(r.Defaults == null)
                {
                    r.Defaults = new RouteValueDictionary();
                }
                r.Defaults.Add("culture", "sv");

                // Adding constraint for culture param
                if(r.Constraints == null)
                {
                    r.Constraints = new RouteValueDictionary();
                }
                r.Constraints.Add("culture", new UserRouteConstraint("en", "sv"));
            }

This way: r.Defaults.Add("culture", HttpContext.Current.Request.UserLanguages[0].Substring(0, 2)) get this error message: "Request
is not available in this
context
".

Thanks in advance!

Zurx

can I add default culture to the RouteValueDictionary():

With a actionFilter. See http://haacked.com/archive/2010/02/21/manipulating-action-method-parameters.aspx/

Hi Zurx,

Thanks for your post.

The best thing to do is to add our localized route first and keep the default second. The way the routing works, it will use the first route that matches.

routes.MapRoute(
                name: "Localization",
                url: "{lang}/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

 You can  use an Attribute, making it easy to use. Under the Filter folder add a new class which inherits from ActionFilterAttribute. This method will check if "lang" is available from RouteData. If lang is present it will change the currentUICulture of
the current thread. If "lang" is not part of the URL, then it will set it to the default.

More information,you can refer to the following link:

#Localization from url with Asp.Net MVC 4

http://www.frankysnotes.com/2013/11/localization-from-url-with-aspnet-mvc-4.html

Hope this can be helpful.

Best Regards,

Eileen

Thank you! This is a great solution!

I wrote

public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var culture = filterContext.RouteData.Values["culture"] as string;
            var userLanguage = HttpContext.Current.Request.UserLanguages;
            
            CultureInfo cultureInfo = new CultureInfo(culture ?? (userLanguage != null ? userLanguage[0].Substring(0, 2) : "sv"));

            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
            base.OnActionExecuting(filterContext);
        }

Leave a Reply