本文介绍了我的本地化网站不起作用,而不是在我点击时更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有本地化问题,我不知道发生了什么,当我点击按钮语言改变另一种语言时,没有任何改变。我调试并发现会话没有更改为其他值,请查看我的代码并帮助我查找,因为
i无法在基类中调试我覆盖InitializeCulture():

I have problem with localization, i do not know what happen, when i click button language to change another language, nothing change. i debugged and find out the session not changing to other value, please have a look to my code and help me findout, because i can not debug in base class that i override InitializeCulture() :

一些常数值:

public const string Lang = "Lang";
        public const string LangVn = "vi-vn"; //"vi-VN"
        public const string LangEn = "en-us"; //"en-Us"
        public const string LangJp = "ja-jp"; //"ja-JP"

        public const string EventTarget = "__EventTarget";



这里是检查语言按钮是否被点击的方法:


here is method to check whether language button is clicked :

public bool IsLangClickEvent(string request)
        {
            if (request == null)
            {
                return false;
            }
            else
            {
                request = request.Replace("_", "-");
            }

            if (request.EndsWith(GlobalResources.LangEn) || request.EndsWith(GlobalResources.LangJp) || request.EndsWith(GlobalResources.LangVn))
            {
                return true;
            }

            return false;
        }

这是我的基地要覆盖InitializeCulture()的类,我将它放在我的网站Root的Folder Helpers中:

here is my base class to override InitializeCulture (), i placed it in Folder Helpers at Root of my Website :

using System;
using System.Globalization;
using System.Threading;
using System.Web;
using UniqueDesign;
using UniqueDesign.Utilities;

namespace Kimono.Web
{
    public class Base : System.Web.UI.Page
    {
        #region initCulture

        protected override void InitializeCulture()
        {
            string culture = string.Empty;

            // get from client
            string select = Request.Form[GlobalResources.EventTarget];
            if (!string.IsNullOrEmpty(select))
            {
                if (select.EndsWith("vn"))
                {
                    culture = GlobalResources.LangVn;

                    // set lang to session
                    Session[GlobalResources.Lang] = GlobalResources.LangVn;
                }
                else
                {
                    if (select.EndsWith("us"))
                    {
                        culture = GlobalResources.LangEn;

                        // set lang to session
                        Session[GlobalResources.Lang] = GlobalResources.LangEn;
                    }
                    else
                    {
                        if (select.EndsWith("jp"))
                        {
                            culture = GlobalResources.LangJp;
                            // set lang to session
                            Session[GlobalResources.Lang] = GlobalResources.LangJp;
                        }
                    }
                }
            }
            else
            {
                // get from session
                if (Session[GlobalResources.Lang] != null)
                {
                    culture = Session[GlobalResources.Lang].ToString();
                    switch (culture)
                    {
                        case GlobalResources.LangVn:
                            culture = GlobalResources.LangVn;
                            break;
                        case GlobalResources.LangEn:
                            culture = GlobalResources.LangEn;
                            break;
                        case GlobalResources.LangJp:
                            culture = GlobalResources.LangJp;
                            break;
                        default:
                            break;
                    }
                }
            }

            if (string.IsNullOrEmpty(culture))
                culture = "Auto";

            //Use this
            UICulture = culture;
            Culture = culture;

            //OR This
            if (culture != "Auto")
            {
                var ci = new System.Globalization.CultureInfo(culture);
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            }

            base.InitializeCulture();
        }

        #endregion
    }
}

Global.asax:

Global.asax :

void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            //set default lang
            Session[GlobalResources.Lang] = GlobalResources.LangVn;

            Session.Timeout = 60;
        }

这里是代码实现本地化的页面:

And here is code of page that implement localization :

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack || Utilities.Instance.IsLangClickEvent(Request.Form[GlobalResources.EventTarget]))
            {
                SwitchLang();
            }
        }

        public void SwitchLang()
        {
            string lang = GlobalResources.LangVn;
            if (Session[GlobalResources.Lang] != null)
            {
                lang = Session[GlobalResources.Lang].ToString();
            }

            switch (lang)
            {
                case GlobalResources.LangVn:
                    Langvi_vn.CssClass = "active";
                    Langen_us.CssClass = "";
                    Langja_jp.CssClass = "";
                    break;
                case GlobalResources.LangEn:
                    Langvi_vn.CssClass = "";
                    Langen_us.CssClass = "active";
                    Langja_jp.CssClass = "";
                    break;
                case GlobalResources.LangJp:
                    Langvi_vn.CssClass = "";
                    Langen_us.CssClass = "";
                    Langja_jp.CssClass = "active";
                    break;
            }
        }

请看一下,问题是当我点击按钮语言,值会话不改变。我不知道如何,我已经检查方法覆盖InitializeCulture()。在此先感谢!

Please have a look, the problem is when i click button language, value of session do not change. And i dont know how, i already check in method override InitializeCulture() . Thanks in advance !

推荐答案


这篇关于我的本地化网站不起作用,而不是在我点击时更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:39