0

I started to develop Multi Language site and By viewing Youtube video i coded that.In my Index Page it works very nice.But i have no idea about how to use that to About Us page(my second view).Could you please give me a help to solve this issue. / This is MVC site.

My Site Can access like this
http://localhost:10228 <-- For English
http://localhost:10228/Home/ChangeLanguage?lang=hi <-- For Hindi

Problem is When i go to another view like AboutUs It doesn't detect the language.(always show in English )

Global.asax

  protected void Application_AquireRequestState(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context != null && context.Request.Cookies != null)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_lang"];
            if (cookie != null && cookie.Value != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Value);
                Thread.CurrentThread.CurrentCulture = new CultureInfo(cookie.Value);
            }
        }
    }

_Layout.cshtml

@using InNews.App_GlobalResources


@Html.ActionLink(multilanguage.English, "ChangeLanguage", "Home", new { lang = "en" }, null)
@Html.ActionLink(multilanguage.Hindi, "ChangeLanguage", "Home", new { lang = "hi" }, null)
@Html.ActionLink(multilanguage.Japanese, "ChangeLanguage", "Home", new { lang = "jp" }, null)

_MenuPartial.cshtml(This is rendered to the layout page)

 @using InNews.App_GlobalResources
 <li>@Html.ActionLink(multilanguage.Home, "Index", "Home")</li>
 <li>@Html.ActionLink(multilanguage.AboutUs, "AboutUs", "Home")</li>

HomeController

  public ActionResult Index()
    {
        return View();
    }

   public ActionResult ChangeLanguage(string lang)
    {
        if (lang != null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
        }

        HttpCookie cookie = new HttpCookie("_lang");
        cookie.Value = lang;
        Response.Cookies.Add(cookie);

        return View("Index");
    }



public ActionResult AboutUs()
        {
            return View();
        }

Could you please help me to solve this issue.

TechGuy
  • 4,298
  • 15
  • 56
  • 87
  • @NightOwl888 I read your answer.Yes it's very good and lengthly explanation.Is there any way to fix my problem in my scenario ? because in here i use another approach... Any idea ? – TechGuy Feb 04 '17 at 18:37
  • The problem *is* your approach. If you want the language to be "sticky", the simplest way is to use Routing rather than a cookie. In fact, the cookie is redundant and unnecessary if you put the language in the URL. And get rid of that `ChangeLanguage` action - it is not necessary. Localization is *content*, not *personalization*, there should be no settings for an individual user - let your users pick the right URL for their culture of choice. Then you only need to build a simple set of links to change culture (that do a GET, not a POST). – NightOwl888 Feb 04 '17 at 19:34

0 Answers0