پیاده سازی چند زبانه در ASP.Net

جمعه 16 مهر 1395

در این مقاله قصد داریم چگونگی پیاده سازی چند زبانه را در یک ASP.NET website بررسی کنیم، که در زیر با مثال توضیح میدهیم.

پیاده سازی چند زبانه در ASP.Net

مامیخواهیم شما را در چگونگی پیاده سازی چند زبانی را در یک ASP.NET website با استفاده از #C و فایل های App_GlobalResources راهنمایی کنیم.

مرحله 1 :

ساختن یک new website project

مرحله 2 :

هنگامی که پروژه شما ساخته شد ، شما نیاز به اضافه کردن App_GlobalResources  به پروژه خود دارید

این یک کار آسان است. برای ساخت فولدر، در  Solution Explorer بر روی نام web site خود کلیک راست کنید و Add Folder را انتخاب کنید . سپس بر روی فولدر App_GlobalResources کلیک کنید. فقط یکی از این پوشه ها میتواند در پروژه استفاده شود و این باید مسیرش در روت برنامه تعیین شود.

 

مرحله 3 :

بر روی پوشه App_GlobalRrsources کلیک راست کنید و Resources File را برای فایل جدید برای اضافه کردن زبان انتخاب کنید.

شما میتوانید برای فایل های زبان مانند الگو  “name.language-culture.resx” انتخاب کنید.

در این مقاله ما دو فایل برای فارسی و ویتنامی درست کرده ایم.

مرحله 4 :

resource file ها را باز میکنیم و برای آنها name و value  تعیین میکنیم.

 

 

 

مرحله 5 :

یک کلاس با نام BasePage میسازیم

در این فایل ، Culture ها را مقدار دهی میکنیم. در اینجا هنگامی که ما یک وب سایت جدید میسازیم، در code-behind شما به ارث بری از کلاس BasePage  به جای ارث بری از System.web.UI.Page نیازدارید.

مثال :

Original: public partial class Default : System.Web.UI.Page 
Now: public partial class Default : BasePage

 

مرحله 6 : 

یک webpage  جدید با نام Default.aspx ایجاد کنید، code-behind را باز کنید و ارث بری را از System.web.UI.Page به BasePage   تغییر دهید.

.

مرحله 7 :

برای گرفتن داده ها ار resource  از کنترل asp : Literal استفاده کنید.

مثال :

<asp:Literal ID="Literal3" runat="server" 
Text="<%$Resources:chienvh.language,mnuHome%>"/>

 


ASP.NET Website and C# with Multi-Language

ChienVH, 23 Sep 2016 CPOL
 
	   4.96 (26 votes)
Rate:	
vote 1vote 2vote 3vote 4vote 5
This article will guide you how to implement ASP.NET website with multi-language
 Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.
Download source code - 43.9 KB
Scenarios
In this article, I will guide you how to implement Multi-language into an ASP.NET website using C# and App_GlobalResources files. You can also find the video here Create Multi-language ASP.NET Website.

Step 1

Create a new website project (I’m using VS2012 in this demo).

Step 2

Once your project has been created, you need to add App_GlobalResources folder to your project.

This is an easy action. To create the folder, in Solution Explorer, just right-click the name of your Web site, click Add Folder, and then click App_GlobalResources folder. There can only be one of these folders in an application, and it must be located at the root of the application.



Step 3

After that, right-click on App_GlobalRrsources folder and select Resources File to create new files for additional languages. You can define the name for language files as the pattern “name.language-culture.resx”. In this demo, I will define two files name as chienvh.language.resx (English) and chienvh.language.vi-vn.resx (Vietnamese).

Step 4

Open two resource files to define the name and value (notice leaving the names as a keys as the same in those files).



Step 5

Create a new class file with the name as BasePage.

In this file, you will do the work to initialize the Culture, and then, hence, when creating a new webpage, then in the code-behind of that file, you need to inherit from BasePage instead of default option as System.Web.UI.Page.

Example:

Hide   Copy Code
Original: public partial class Default : System.Web.UI.Page 
Now: public partial class Default : BasePage
Step 6

Create a new webpage with the name as Default.aspx, then open the code behind of this file and change inheritance from System.Web.UI.Page to BasePage.

Step 7

Use asp:Literal control to get data from resource files.

Example:

Hide   Copy Code
<asp:Literal ID="Literal3" runat="server" 
Text="<%$Resources:chienvh.language,mnuHome%>"/>
Hide   Shrink    Copy Code
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System;
 
namespace ASPNetMultiLanguage
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (!string.IsNullOrEmpty(Request["lang"]))
            {
               
			Session["lang"] = Request["lang"];
            }
            string lang = Convert.ToString(Session["lang"]);
            string culture = string.Empty;
            /* // In case, if you want to set vietnamese as default language, then removing this comment
            if(lang.ToLower().CompareTo("vi") == 0 ||string.IsNullOrEmpty(culture))
            {               
				culture = "vi-VN";
            }
             */
            if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
            {
                culture = "en-US";
            }
            if (lang.ToLower().CompareTo("vi") == 0)
            {               
				culture = "vi-VN";
            }
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
 
            base.InitializeCulture();
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPNetMultiLanguage
{
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region--Show/hide language link
            if (!string.IsNullOrEmpty(Convert.ToString(Session["lang"])))
            {
                if (Convert.ToString(Session["lang"]) == "en")
                {
                    linkVietnameseLang.Visible = true;
                    linkEnglishLang.Visible = false;
                }
                else
                {
                    linkEnglishLang.Visible = true;
                    linkVietnameseLang.Visible = false;
                }
            }
            else
            {
                linkVietnameseLang.Visible = false;
                linkEnglishLang.Visible = true;
            }
            #endregion--
        }
    }
}

 

 

خروجی :

 

 

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید