Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.NET MVC (5) Localization based on URL

Posted on 2014-12-22
3
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
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:
Michael Fowler earned 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

604 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