Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:17:49+00:00 2026-06-06T12:17:49+00:00

I want to switch my views in MVC 3 between two languages – PL

  • 0

I want to switch my views in MVC 3 between two languages – PL and EN. I’ve created two folders in Views- EN and PL. So after clicking appropriate language link at any site I want my route change from:

routes.MapRoute(
                "pl", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "PL", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

to:

routes.MapRoute(
                "en", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "EN", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

When I click appropriate link (language switcher) it changes CultureInfo which is persistent to all threads.
_Layout View with switcher:

<ul>
    <li>@Html.ActionLink("En", "ChangeCulture", null, new { lang = "en"}, null)</li>
    <li>@Html.ActionLink("Pl", "ChangeCulture", null, new { lang = "pl"}, null)</li>
</ul>

and controller (which sets also static variable lang that can be seen in every controller’s method and be persistent between requests):

public ActionResult ChangeCulture(string lang)
    {

        PLController.lang = lang;
        CultureSettings setCulture = new CultureSettings();
        setCulture.InitializeCulture(lang);
        cookie.Value = CultureInfo.CurrentCulture.Name;
        this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
        return View("Index");
    }

InitializeCulture method is overriden from Page class as follows:

public class CultureSettings : Page{

    public void InitializeCulture(string culture)
    {
            String selectedLanguage;
            if(culture == null)
            {
                selectedLanguage = "pl";
            }
            else
            {
                selectedLanguage = culture;
            }

            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);

        base.InitializeCulture();
    }
}

It sets CultureInfo properly. Now I want (according to current CultureInfo) switch routes for every navigation links and change route pattern from mysite.com/PL/{controller}/{action} to mysite.com/EN/{controller}/{action}.

Does anyone has any ideas or maybe better approach for this problem? But condition is that address must be looking like this mysite.com/EN or mysite.com/PL – not different (i.e. en.mysite.com)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:17:50+00:00Added an answer on June 6, 2026 at 12:17 pm

    The first thing that you must decide is where to store the current user language. There are different possibilities:

    • part of every url
    • cookie
    • session

    IMHO for SEO purposes it’s best to have it as part of the url.

    So I would recommend writing a custom route which will parse the language from the url and set the current thread culture:

    public class LocalizedRoute : Route
    {
        public LocalizedRoute()
            : base(
                "{lang}/{controller}/{action}/{id}",
                new RouteValueDictionary(new
                {
                    lang = "en-US",
                    controller = "home",
                    action = "index",
                    id = UrlParameter.Optional
                }),
                new RouteValueDictionary(new
                {
                    lang = @"[a-z]{2}-[a-z]{2}"
                }),
                new MvcRouteHandler()
            )
        {
        }
    
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var rd = base.GetRouteData(httpContext);
            if (rd == null)
            {
                return null;
            }
    
            var lang = rd.Values["lang"] as string;
            if (string.IsNullOrEmpty(lang))
            {
                // pick a default culture
                lang = "en-US";
            }
    
            var culture = new CultureInfo(lang);
    
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
    
            return rd;
        }
    }
    

    We could now register this custom route in Global.asax:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.Add("Default", new LocalizedRoute());
    }
    

    Alright, now let’s have a model:

    public class MyViewModel
    {
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime Date { get; set; }
    }
    

    A controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel
            {
                Date = DateTime.Now
            });
        }
    
        public ActionResult Test()
        {
            return Content(DateTime.Now.ToLongDateString());
        }
    }
    

    And a view:

    @model MyViewModel
    
    @Html.DisplayFor(x => x.Date)
    
    <ul>
        <li>@Html.ActionLink("switch to fr-FR", "index", new { lang = "fr-FR" })</li>
        <li>@Html.ActionLink("switch to de-DE", "index", new { lang = "de-DE" })</li>
        <li>@Html.ActionLink("switch to en-US", "index", new { lang = "en-US" })</li>
    </ul>
    
    @Html.ActionLink("Test culture", "test")
    

    Now when you click on the links we are changing the language and this language is now part of the routes. Notice how once you have chosen the language this language is being persisted in the routes for the test link.

    Scott Hanselman also wrote a nice blog post on localization and globalization in ASP.NET that’s worth checking out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short: I want to have two fullscreen views, where I can switch between
I want to switch between a couple views with a flick gesture using two
In order to switch between views in my iOS app, I have been using
I have a MVC project. The views are aspx. I created a view split
I want to switch views but with no animation such as a cross dissolve,
Is there something like onLeftSwipeListener and onRightSwipeListener in Android? I want to switch views
I am trying to switch views, but I want the animation with the view
I'm working on an app in which I want the viewstack to switch views
I am just exploring asp.net mvc as I want to switch from Webforms. I
I have a ViewPagerActivity with two views. I want to register a context menu,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.