0

I want to make Multi Language website in mvc. Here is what I have done until know

  1. Make two folder in Views as En and Ar
  2. Inside these folders I have a 2 views both with the same name as "Home"

Now lets suppose if I am in English version the url will be something like this localhost/En/Home, In the Home view I have html Button Now what I want is that when the user press the Button it should redirect to localhost/Ar/WhateverPage and when the user is in arabic version vice versa should Happen.

So to summarize, all I want to do is change the Url from en/Home page to ar/Home.

Note I will add this html button to Layout.cshtml so I just want this to Program Once

Abdul Hannan
  • 424
  • 1
  • 6
  • 20
  • For the routing/thread part, see [ASP.NET MVC 5 culture in route and url](http://stackoverflow.com/a/32839796/181087). After doing that your current thread will have the current culture, you just need to [customize the view engine](http://stackoverflow.com/a/16517536/181087) to select the right view based on the current thread. You might consider using LTR and RTL views with resources, but if these are the only 2 languages you will have that might not be worth the extra effort. – NightOwl888 Jul 16 '16 at 16:57

1 Answers1

0

You can navigate between pages by knowing the current culture of the browser and also keep the user choice to redirect him to the target languages regardless the current culture of the browser

this can simply done by the code below

 if (!string.IsNullOrEmpty(Request.QueryString["Lang"]))
    {
        HttpCookie myCookie = (HttpCookie)Request.Cookies["cLang"];
        if (myCookie != null)
        {
            if (myCookie["Lang"] != Request.QueryString["Lang"].ToString())
            {
                myCookie["Lang"] = Request.QueryString["Lang"].ToString();
                myCookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(myCookie);

                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Request.QueryString["Lang"]);
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(Request.QueryString["Lang"]);
                Response.Redirect(Request.Url.AbsoluteUri);
            }
        }
        else
        {
            string strLang = Request.QueryString["Lang"];
            myCookie = new HttpCookie("cLang");
            myCookie.Values.Add("Lang", strLang);
            myCookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(myCookie);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(strLang);
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(strLang);
        }

    }

But what if you want to add another language to your site ?! will you copy the same pages and translate it to the other language ?!

Not the best choice, so i strongly recommend reading the following article (http://afana.me/post/aspnet-mvc-internationalization.aspx)