日期:2014-05-18  浏览次数:20497 次

多语言的用户界面问题
我第一次做国际化的项目,请大家多多支持啊!
我从 ASP.NET 网页生成两个局部资源文件,一个中文,一个英文。修改IE的语言后,页面内容会根据修改显示不同的语言,这个没有问题。但是如果我想在页面上添加两个按钮(中文、英文),用来切换显示语言,一般多语言网站都会有的,要怎么实现啊?


System.Globalization.CultureInfo.CurrentCulture 的属性是不能修改的,到底修改哪里呢?

我试过在按钮时间里添加
方法1:
  CultureInfo newCulture = (CultureInfo)System.Globalization.CultureInfo.CurrentCulture.Clone();
  newCulture.Name = "zh-CN";
  System.Globalization.CultureInfo.CurrentCulture = new CultureInfo("zh-CN");
编译器错误信息: CS0200: 无法对属性或索引器“System.Globalization.CultureInfo.Name”赋值 -- 它是只读的
副本也不能修改-_-!!!
方法2:
  System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(0x0804);
  System.Threading.Thread.CurrentThread.CurrentCulture = ci; 
没报错,但没起作用。一点反应也没有,还是英文的,唉!


路过的朋友帮个忙看一下

------解决方案--------------------
C# code

    /// <summary>
    /// 语言文化区域性控制
    /// </summary>
    public abstract class Culture : System.Web.UI.Page
    {
        /// <summary>
        /// 重写Page基类的InitializeCulture方法。
        /// 为页的当前线程设置 System.Web.UI.Page.Culture 和 System.Web.UI.Page.UICulture。
        /// </summary>
        protected override void InitializeCulture()
        {
            if (!string.IsNullOrEmpty(CultureCache))
            {
                SetCulture(CultureCache);
            }
            else
            {
                SetCulture(DefaltCulture);
            }
        }

        /// <summary>
        /// 设置指定文化
        /// </summary>
        /// <param name="culture">有效的区域性名称</param>
        public static void SetCulture(string culture)
        {
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            }
            catch
            {
                culture = DefaltCulture;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            }
            CultureCache = culture;
            UICultureCache = culture;
        }

        /// <summary>
        /// 获取当前应用程序默认文化
        /// </summary>
        public static string DefaltCulture
        {
            get
            {
                string culture = ConfigurationManager.AppSettings["Culture"].ToString();
                if (string.IsNullOrEmpty(culture) || culture == "auto")
                {
                    return Thread.CurrentThread.CurrentCulture.ToString();
                }
                return culture;
            }
        }

        /// <summary>
        /// 存储当前Culture Session
        /// </summary>
        public static string CultureCache
        {
            get
            {
                try
                {
                    return HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CultureCache"].ToString()].Value;
                }
                catch
                {
                    return string.Empty;
                }
            }
            set { HttpContext.Current.Response.Cookies[ConfigurationManager.AppSettings["CultureCache"].ToString()].Value = value; }
        }

        /// <summary>
        /// 存储当前UICulture Session
        /// </summary>
        public static string UICultureCache
        {
            get
            {
                try
                {
                    return HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["UICultureCache"].ToString()].Value;
                }
                catch
                {
                    return string.Empty;
                }
            }