Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET

Creating a Bilingual ASP.NET MVC 3 Application – Part 2

Rate me:
Please Sign up or sign in to vote.
4.82/5 (29 votes)
30 Jan 2012CPOL16 min read 149K   5.8K   65  
This artcle extends Part 1 to allow overriding of culture through the URL in a lightweight fashion.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;

namespace MvcGlobalisationSupport
{
    public class GlobalisedRoute : Route
    {
        
        public const string CultureKey = "culture";

        static string CreateCultureRoute(string unGlobalisedUrl)
        {
            return string.Format("{{" + CultureKey + "}}/{0}", unGlobalisedUrl);
        }

        /// <summary>
        ///    Initializes a new instance of the System.Web.Routing.Route class, by using
        ///    the specified URL pattern, default parameter values, and handler class.
        /// </summary>
        /// <param name="unGlobalisedUrl">The URL pattern for the route, without the culture</param>
        /// <param name="defaults"The values to use for any parameters that are missing in the URL.></param>
        public GlobalisedRoute(string unGlobalisedUrl, RouteValueDictionary defaults) :
            base(CreateCultureRoute(unGlobalisedUrl),
                    defaults,
                    new RouteValueDictionary(new { culture = new CultureRouteConstraint() }),
                    new GlobalisationRouteHandler())
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I Graduated in Natural Sciences (Chemistry & Physics) from Durham University, where I did not cover myself with glory, but did fill, and often cover, myself with beer.

I qualified as a secondary school teacher, but I hated teaching. I worked as an IT techhie for a few years but kept being promoted to IT Trainer, thanks to my teaching experience, which I also disliked. I spent some time working out what else I could do instead and reduced beer intake.

I realised that I should work as a programmer, having enjoyed it a hobby since I was a nipper in the halcyon days of the Sinclair Spectrum (48k, Rubber Keyboard). Spent two weeks working out why I didn't think of this to start with, instead of starting my dull-as-ditch-water Chemistry degree 8 years earlier. Had a beer to celebrate.

I Graduated in 2001 with an MSc from Newcastle Uni in Comp Sci. Did cover myself with glory, and drank some beer.

.Netting ever since, and loving it. Though I have largely given up beer due to not being able to hack the pace like I used to.

I was born, brought up, and have lived most of my life near Newcastle. In a fit of temporary insanity I moved to Amman, in my wife's homeland of Jordan, but made it back safely to the UK without any extra holes being made in my person by bullets. To be fair it was pretty safe at the time, if you ignored the roads.

Visit Jordan if you can by the way, the landscape is beautiful and varied, the food excellent and the people the friendliest on earth, after Geordies naturally Smile | :) .

Comments and Discussions