2

I develop multilanguages asp.net mvc site. The language stores in url. The default one will be English. I have BaseController class

protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string culture = filterContext.RouteData.Values[MvcApplication.CultureParamerName] as string;
            if (string.IsNullOrWhiteSpace(culture))
                culture = Facade.Common.GetLocale(BECulture.Cultures.English);//==en-us
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            base.OnActionExecuting(filterContext);
        }

and some routes

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Photos_Route",
            "{culture}/photos/{action}/{id}",
            new { controller = "photos", action = "show", culture = "en-us" }
            );
        routes.MapRoute(
            "PhotosDownload_Route",
            "{culture}/photos/{action}/{id}/resolution/{measure1}x{measure2}",
            new { controller = "photos", action = "download", culture = "en-us" }
            );

        routes.MapRoute(
            "Pages_Router",
            "{culture}/page/{id}",
            new { controller = "home", action = "page", id = UrlParameter.Optional, culture = "en-us" }
            );

        routes.MapRoute(
            "Tags_Route", // Route name
            "{culture}/tag/{tag}", // URL with parameters
            new { controller = "photos", action = "ShowPhotosByTag", culture = "en-us" } // Parameter defaults
            );



routes.MapRoute(
            "Home_Route",
            "{culture}/{action}",
            new { controller = "home", culture = "en-us" }
            );

        routes.MapRoute(
            "Default_Route",
            "{culture}/{controller}/{action}/{id}",
            new { controller = "home", action = "index", id = 1, culture = "en-us" }
            );

    }

I want to do following: if the current language is English then the url must not contain "en-us" value, because the English is default language. And vise-versa: if there's no parameter "culture" in url (that is in RouteData.Values["culture"] ) then the default language is English.

How can I do it?

UPDATE: the clarification about what I want. The master page (_Layout.cshtml) contains the link to the page "About site"

 @Html.ActionLink(Resources.Strings.About_LinkText, "about", "home")

If the site in English it points to /en-us/about. But I want (if the site in English) it should point to /about (without en-us).

2
  • You're basically making culture required in every request so it will have to be included in every request. By default the first parameter will always be culture. So if it's /home then "home" becomes the culture. This isn't the easiest thing to do. Are the routes set or can you change them? Commented Oct 19, 2011 at 19:54
  • You'll probably want to move the culture to the last item and make it {*culture} however it's still not easy. You might need to create a route constraint and duplicate all your routes without the culture parameter Commented Oct 19, 2011 at 20:42

1 Answer 1

1

The problem with this route setup is to decide whether the first token of the route is a valid culture name. For example you will have to deal with routes like:

  • /fr-FR/home/index
  • /home/index

In the first case culture="fr-FR" and in the second culture="home". So to deal with this you could try something along the lines of:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string culture = filterContext.RouteData.Values["culture"] as string;
    var ci = CultureInfo
        .GetCultures(CultureTypes.AllCultures)
        .FirstOrDefault(x => x.Name == culture);
    if (ci == null)
    {
        // we couldn't find a suitable culture => fallback to default
        ci = new CultureInfo("en-US");
    }

    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;
    base.OnActionExecuting(filterContext);
}
Sign up to request clarification or add additional context in comments.

7 Comments

@AlexMaslakov, what's your issue?
<<if the current language is English then the url must not contain "en-us" value, because the English is default language. And vise-versa: if there's no parameter "culture" in url (that is in RouteData.Values["culture"] ) then the default language sets to English.>> In the other words: how to remove extra values "en-us" from url if the default culture is English?
@AlexMaslakov, your routes are fine and should work for your requirements along with my suggestion about the OnActionExecuting method. Did you try it?
I want to remove "en-us" from url. For example, /home/index and /en-us/home/index displays the same content in English.
@AlexMaslakov, that's exactly what will happen with this setup (your routes and my OnActionExecuting). I repeat my question once again: did you try it? Did you encounter some problems? If you had you would have seen that both /home/index and /en-us/home/index will display the index action on the home controller with culture=en-us.
|

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.