Solved

.NET MVC (5) Localization based on URL

Posted on 2014-12-22
3
517 Views
Last Modified: 2014-12-22
I have an application in .NET MVC5 with 2 supported languages, English as default. I currently use cookies to switch between languages but would like to change the application to make use of URL's for specifying the language. I have found a solution that works, but the default language, English, must currently appear in the URL  ".../en/...".

How can I make use of URL for this purpose, while having the default (English) work without a culture (language) specified in the URL? English would be the default, when no culture (language) is specified in the URL and the second language would be accessible by including the culture (language) in the URL.
0
Comment
Question by:lynnwalker
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40514115
How are you implementing the URL based language selection currently? If you are using RouteConfig have a look at this tutorial

http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API
0
 

Author Comment

by:lynnwalker
ID: 40514237
I am using RouteConfig, but in an MVC application, not Web API. Here is the current RouteConfig:

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

            routes.MapRoute(name: "DefaultWithCulture"
                           , url: "{culture}/{controller}/{action}/{id}"
                           , defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                           , constraints: new { culture = "[a-z]{2}" }
            );

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

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40514250
In your default MapRoute set the culture you want as the default

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

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question