مرجع تخصصی برنامه نویسان

بزرگترین انجمن برنامه نویسان فارسی زبان

برای دوزبانه کردن وب سایت چه کار دیگه ای باید انجام بدم؟

چهارشنبه, 26 شهریور 1399 18:16

برای دوزبانه کردن وب سایت چه کار دیگه ای باید انجام بدم؟

لنگوییج کنترلر
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace testMVC.web.Controllers
{
رفرنسبه هومکنترلر
    public class LanguageController : Controller
    {
        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            string lang = null;
           /*HttpCookie langCookie = Request.Cookies["culture"];
            if (langCookie != null)
            {
                lang = langCookie.Value;
            }*/
          if (Session["culture"] != null)
                lang = Session["culture"].ToString();
            else
            {
                var userLanguage = Request.UserLanguages;
                var userLang = userLanguage != null ? userLanguage[0] : "";
                if (userLang != "")
                {
                    lang = userLang;
                }
                else
                {
                    lang = LanguageManager.GetDefaultLanguage();
                }
            }
            new LanguageManager().SetLanguage(lang);
            return base.BeginExecuteCore(callback, state);
        }
    }
}
هومکنترلر

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using testMVC.web.Models;

namespace testMVC.web.Controllers
{

[AllowAnonymous]
    public class HomeController : LanguageController
{

#region action

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

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

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

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

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

#endregion

        public ActionResult ChangeLanguage(string lang)
        {
            new LanguageManager().SetLanguage(lang);
            return RedirectToAction("Index", "Home");
        }
       /* public ActionResult NewsPart()
        {
            CampoDeiFioriEntities db = new CampoDeiFioriEntities();
            var dbNews = db.MagNovita.Last<MagNovita>();
            var item = from b in db.MagNovita
                       where b.PrgNovita == 4
                       select b;
            return View(dbNews);
        }*/
        
        
}
}


یک کلاس بسازیم به اسم لنگوییج منیجر 

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;

namespace testMVC.web
{
    public class LanguageManager
    {
        public static List<Languages> AvailableLanguages = new List<Languages> {   
            new Languages {  
                LanguageFullName = "Italian", LanguageCultureName = "it-IT"  
            }, 
            new Languages {  
                LanguageFullName = "English", LanguageCultureName = "en-US"  
            },   
        };  
        public static bool IsLanguageAvailable(string lang) {  
            return AvailableLanguages.Where(a => a.LanguageCultureName.Equals(lang)).FirstOrDefault() != null ? true : false;  
        }  
        public static string GetDefaultLanguage() {  
            return AvailableLanguages[0].LanguageCultureName;  
        }  
        public void SetLanguage(string lang) {  
           // try {  
                if (!IsLanguageAvailable(lang)) lang = GetDefaultLanguage();
                var cultureInfo = new CultureInfo(lang);  
                Thread.CurrentThread.CurrentUICulture = cultureInfo;  
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
                HttpContext.Current.Session["culture"] = lang;
                /*HttpCookie langCookie = new HttpCookie("culture", lang);  
                langCookie.Expires = DateTime.Now.AddYears(1);
                HttpContext.Current.Response.SetCookie(langCookie); 
                HttpContext.Current.Response.Cookies.Add(langCookie);*/
           // } catch (Exception) {}  
        }  
    }  
    public class Languages {  
        public string LanguageFullName {  
            get;  
            set;  
        }  
        public string LanguageCultureName {  
            get;  
            set;  
        }  
    } 
}

توی پوشه لی آوت

<!DOCTYPE html>

<html>
<head>
<title>@ViewBag.Title - Campo dei Fiori</title>
<meta charset="utf-8" />

<meta name="viewport" content="width=device-width,  initial-scale=1.0, minimum-scale=1.0">
<link href="~/Content/css/parametri.css" rel="stylesheet" />
<link href="~/Content/css/style.css" rel="stylesheet" />
<link href="~/Content/css/Fonts.css" rel="stylesheet" />


<link rel="shortcut icon" type="image/png" href="~/Content/img/Favicon.png" />

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/js/General.js"></script>
@*<script src="~/Scripts/js/jquery.localisation.js"></script>*@
@RenderSection("Scripts", required: false)
</head>
<body>

@RenderSection("PopupVideo", required: false)
<div class="Container">

<div class="header">
<div class="H_Iocon">

                @{
                    var isSelected_IT = !string.IsNullOrEmpty(Session["culture"].ToString()) && Session["culture"].ToString() == "it-IT" ? "Sel" : "";
                    var isSelected_EN = string.IsNullOrEmpty(Session["culture"].ToString()) || Session["culture"].ToString() == "en-US" ? "Sel" : "";
                }

<div class="Lingua"> <span class="@isSelected_IT"> 
                @Html.ActionLink("ITA", "ChangeLanguage", "Home", new { lang = "it-IT" }, null)</span>  
                | 
                    <span class="@isSelected_EN">@Html.ActionLink("ENG", "ChangeLanguage", "Home", new { lang = "en-US" }, null)</span></div>

<a target="_blank" href="https://www.facebook.com/CampoDeiFioriSrl/"><img class="H_FbIcon" src="~/Content/img/H_FbIcon.png" /></a>
<img class="MenuMobile" src="~/Content/img/MenuMobile.png" /><img style="display:none" class="MenuMobile" src="~/Content/img/CloseMenu.png" />
</div>
<a href="@Url.Action("Index", "Home")"> <img class="Logo_Header" src="~/Content/img/Logo.png" />   </a>

<div class="Menu">
<ul>
<li><a href="@Url.Action("ChiSiamo","Home")">@Resources.Global.Menu_Chisiamo</a></li>
<li><a href="#">@Resources.Global.Menu_Novita</a></li>
<li><a href="@Url.Action("Contatti","Home")">@Resources.Global.Menu_Contatti</a></li>
</ul>
<ul class="Lingua">
<li>ITA</li>
<li>ENG</li>
</ul>
</div>

</div>


@RenderBody()

<div class="Footer">
@Resources.Global.Menu_Company - <a href="@Url.Action("Contatti","Home")">@Resources.Global.Menu_Contattaci</a>   - <a href="@Url.Action("Privacy","Home")"> @Resources.Global.Menu_Privacy </a>

</div>
</div>
<div class="ScrollZone "> <span class="ScorriTxt">@Resources.Global.Menu_Scorri</span>    <span class="SaliTxt">@Resources.Global.Menu_Sali</span></div>

</body>
</html>

ارسال پاسخ برای این تاپیک