MvcLanguageUrls helps you to apply permanent language switch in all of your MVC application Urls.

By using this module you'll have Urls like this: www.example.com/en/sample or www.example.com/fr/sample

  • NuGet Package

PM> Install-Package MvcLanguageUrls

  • Easy to use

Usage of this module is pretty straightforward. In RegisterRoutes method of your application, just replace the default Route map with this line of code:

MvcUrlExtension.MapLocalizedRoute(routes, "en", "fr", "de", "uk");

This line indicates that application will be support these languages.

The same goes for Areas with MapLocalizedAreaRoute method:

MvcUrlExtension.MapLocalizedAreaRoute(context, this, new[] { "en", "fr", "de", "uk" });

If you wish to transfer any non-localized url to use this route, insert this line of code after previous one:

MvcUrlExtension.RedirectToLocalizedRoute(routes, true);

With this code any request to "www.example.com/sample" will redirect to "www.example.com/en/sample"

Finally your RegisterRoutes will look like this:

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

    MvcUrlExtension.MapLocalizedRoute(routes, "en", "fr", "de", "uk");
    MvcUrlExtension.RedirectToLocalizedRoute(routes, true);
}

And the area registration method will look like this:

public override void RegisterArea(AreaRegistrationContext context)
{
MvcUrlExtension.MapLocalizedAreaRoute(context, thisnew[] { "en""fr""de""uk" }); }

And that's it, you're done.

Please note that the only specified languages in route registrations will work in Urls, otherwise the user will see the default 404 mvc error.

  • Additional Methods

If you wish to switch the language by generating different language Urls, there are two additional extension methods.

Html.ActionLinkLang which accepts language parameter, and of course if you don't mention it will use current culture language. And Url.ActionLang which again accepts additional language parameter.

Last edited Aug 10, 2013 at 4:58 PM by salar2k, version 12