zoukankan      html  css  js  c++  java
  • 基于资源文件的MVC的国际化处理

    这几天在研究MVC的国际化,MVC的国际化方案有很多,这次主要看的是基于Resources文件的国际化。

    在进行国际化的过程中主要是对Thread.CurrentThread.CurrentUICulture的一个封装处理;

    一般的处理方案: Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(value);

    第一步:

    建立CultureHelper辅助类:

        public class CultureHelper
        {
            protected HttpSessionState session;
            public CultureHelper(HttpSessionState session)
            {
                this.session = session;
            }
    
            public static string CurrentCulture
            {
                get
                {
                    return Thread.CurrentThread.CurrentUICulture.Name;
                }
                set
                {
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(value);
                }
            }
        }
    

    CultureHelper主要通过线程来完成语言的切换功能;

    建立基本的BaseController:

     public class BaseController : Controller
        {
            protected override void ExecuteCore()
            {
                string culture = string.Empty;
                if (this.Session["Culture"] == null)
                {
                    // 从配置文件获取语种
                    culture = System.Configuration.ConfigurationManager.AppSettings["Culture"];
                    // 设置默认语种
                    this.Session["Culture"] = System.Configuration.ConfigurationManager.AppSettings["Culture"];
                }
                else
                {
                    culture = this.Session["Culture"].ToString();
                }
                Helpers.CultureHelper.CurrentCulture = culture;
                base.ExecuteCore();
            }
    
            protected override bool DisableAsyncSupport
            {
                get { return true; }
            }
        }
    

    所有的Controller都基于BaseController,在进行设置的时候,只需要对Session["Culture"]进行更改就可以了。

    至于Resources文件的配置,网络上一大堆的介绍,那我就不重复处理了。

    但是需要注意几个问题:

    1. 关于Resources文件的配置;

    2. 怎么进行强制的修改

  • 相关阅读:
    Eclipse Alt + / 无提示
    洛谷 P1101 单词方阵
    力扣题解 7th 整数反转
    力扣题解 344th 反转字符串
    力扣题解 48th 旋转图像
    力扣题解 36th 有效的数独
    力扣题解 1th 两数之和
    力扣题解 283th 移动零
    力扣题解 66th 加一
    力扣题解 350th 两个数组的交集 II
  • 原文地址:https://www.cnblogs.com/ArrowTip/p/MvcMultiLanguage.html
Copyright © 2011-2022 走看看