Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > การทำ Multi Language บน ASP.NET MVC สร้างเว็บหลายภาษาบน ASP.Net MVC



 
Clound SSD Virtual Server

การทำ Multi Language บน ASP.NET MVC สร้างเว็บหลายภาษาบน ASP.Net MVC

การทำ Multi Language บน ASP.NET MVC การทำ Multi Language บน ASP.NET MVC ให้สามารถแสดงผลได้หลายภาษา โดย Config ผ่าน Resource file

ASP.Net MVC


ขั้นตอนการทำ

1. create class CultureManager
2. create basecontroller
3. inherited controller ที่ต้องการ มายัง basecontroller
4. add resource file ตาม ภาษา ที่ต้องการ
5. add ไปยัง cshtml

Step 1: create class CultureManager

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Threading;
using System.Globalization;

public static partial class Config
    {
        public partial class CultureManager
        {

            protected HttpSessionState session;

            public CultureManager(HttpSessionState httpSessionState)
            {
                session = httpSessionState;
            }

            public static int CurrentCulture
            {
                /*en-US     th-TH */
                get
                {
                    if (Thread.CurrentThread.CurrentUICulture.Name == "en-US") return 0;        /*english*/
                    else if (Thread.CurrentThread.CurrentUICulture.Name == "th-TH") return 1;   /*thai*/
                    else return 0;
                }
                set
                {
                    //
                    // Set the thread's CurrentUICulture.
                    //
                    if (value == 0)
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                    else if (value == 1)
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("th-TH");
                    else
                        Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

                    //
                    // Set the thread's CurrentCulture the same as CurrentUICulture.
                    //
                    Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
                }
            }

        }
    }



2. create basecontroller

Code (C#)
    public abstract class BaseController : Controller
    {

        #region OnActionExecuting

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

        }

        #endregion

        /// <summary>
        /// Manage the internationalization before to invokes the action in the current controller context.
        /// </summary>
        protected override void ExecuteCore()
        {
            int culture = 0;
            if (this.Session == null || this.Session["CurrentCulture"] == null)
            {
                int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Culture"], out culture);
                this.Session["CurrentCulture"] = culture;
            }
            else
            {
                culture = (int)this.Session["CurrentCulture"];
            }
            //
            Common.Helpers.Config.CultureManager.CurrentCulture = culture;
            //
            // Invokes the action in the current controller context.
            //
            base.ExecuteCore();
        }

        protected override bool DisableAsyncSupport
        {
            get { return true; }
        }

        #region "JsonResult"
        protected override JsonResult Json(object data, string contentType,
            Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new Api.JsonNetResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };            
        }


        #endregion

    }


3. inherited controller ที่ต้องการ มายัง BaseController แทน Controller

Code (C#)
    public class HomeController : BaseController


4. add resource file ตาม ภาษา ที่ต้องการ

ใน folder App_GlobalResources ทำการ เพิ่ม file
1. Resource.en.resx
2. Resource.resx

และทำการ เพิ่มค่า parameter ตามต้องการ

5. add switch ภาษา ไปยัง cshtml

Code
   <a class="active" href="/Home/ChangeCurrentCulture/0" title="THAI">TH</a> 
   <a href="/Home/ChangeCurrentCulture/1" title="ENGLISH">EN</a>


** การเรียกใช้ ก็ Resources.Resource.ParameterName

หวังว่าจะเป็นประโยชน์ สำหรับ การทำ Application ให้รองรับ หลายๆ ภาษา ครับ

ตัวอย่างเพิ่มเติม ดูได้จาก www.impleplus.com


Reference : http://impleplus.com





   
Share
Bookmark.   

  By : momoplus
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2014-05-28
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่