• 社区网站功能实现系列(一):多国语言的实现


                      社区网站功能实现系列(一):多国语言的实现

    相信大家也见过很多网站有多国语言版本,点击一个链接就能轻松的切换。下面我就来简单讲一下,在我们所做的项目中,这种功能的实现方式。

    首先要明白,这只是在网站加载的时候, 这些以文字方式显示的内容,根据从配置文件中(或者缓存中)获取到的条件(要显示那种语言),来对应显示的。也就是将原本固定死的文字用类似于<%= GetTextString(SNET.Network.Entities.LanguageKey.Edit_Video) %>来替代。

    下面我来讲下GetTextString方法的实现方式。

    1.     因为GetTextString几乎在每个页面上都要使用到,所以,将它放入Page的base方法中。

    public string GetTextString(int key)

    {

        return SNET.Network.Controllers.NetworkBaseController.GetTextString(key, NetworkLanguageID);

    }

    NetworkLanguageID是base中的一个属性,就是当前站点的语言种类,在Cache中获得。

     

    2.     GetTextString(int, int)方法的实现:

    /// <summary>

            /// 根据语言类别和该类语言下的KEY来获取字符串信息

            /// </summary>

            /// <param name="intLanguageID">当前语言的ID</param>

            /// <param name="key">语言KEY</param>

            /// <returns></returns>

            public static string GetTextString(int intLanguageID, int key)

            {

                switch (intLanguageID)

                {

                    case LanguageIDsIndentifier.CHINESE_SIMPLIFIED:

                        if (LanguageCache_zh_CN == null)

                        {

                            lock (objLanguageKey)

                            {

                                if (LanguageCache_zh_CN == null)

                                {

                                    LanguageCache_zh_CN = new Dictionary<int, string>();

                                    AddCacheZhCN();

                                }

                            }

                        }

                        return LanguageCache_zh_CN[key];

                    case LanguageIDsIndentifier.ENGLISH_US:

                        if (LanguageCache_en_US == null)

                        {

                            lock (objLanguageKey)

                            {

                                if (LanguageCache_en_US == null)

                                {

                                    LanguageCache_en_US = new Dictionary<int, string>();

                                    AddCacheEnUS();

                                }

                            }

                        }

                        return LanguageCache_en_US[key];

                    case LanguageIDsIndentifier.VIETNAMESE:

                        if (LanguageCache_vi_VN == null)

                        {

                            lock (objLanguageKey)

                            {

                                if (LanguageCache_vi_VN == null)

                                {

                                    LanguageCache_vi_VN = new Dictionary<int, string>();

                                    AddCacheViVN();

                                }

                            }

                        }

                        return LanguageCache_vi_VN[key];

     

                    case LanguageIDsIndentifier.CHINESE_TRADITIONAL:

                        if (LanguageCache_zh_CN_HK == null)

                        {

                            lock (objLanguageKey)

                            {

                                if (LanguageCache_zh_CN_HK == null)

                                {

                                    LanguageCache_zh_CN_HK = new Dictionary<int, string>();

                                    AddCacheZhCNHK();

                                }

                            }

                        }

                        return LanguageCache_zh_CN_HK[key];

     

                    case LanguageIDsIndentifier.JAPANESE:

                        if (LanguageCache_jp_JP == null)

                        {

                            lock (objLanguageKey)

                            {

                                if (LanguageCache_jp_JP == null)

                                {

                                    LanguageCache_jp_JP = new Dictionary<int, string>();

                                    AddCacheJP();

                                }

                            }

                        }

                        return LanguageCache_jp_JP[key];

                    default:

                        if (LanguageCache_zh_CN == null)

                        {

                            lock (objLanguageKey)

                            {

                                if (LanguageCache_zh_CN == null)

                                {

                                    LanguageCache_zh_CN = new Dictionary<int, string>();

                                    AddCacheZhCN();

                                }

                            }

                        }

                        return LanguageCache_zh_CN[key];

                }

            }

    如果Cache中没有这个语言包下的文字内容,则通过方法AddCacheZhCN (AddCacheEnUS,AddCacheViVN,AddCacheZhCNHK,AddCacheJP)将所有该语言包下的文字内容添加到Cache中,再获取当前给定key对应的值并返回。

     

    3.     AddCacheZhCN方法的实现

          public static void AddCacheZhCN()

            {

                string strZhCN = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath(LanguageResourceIdentifier.PATH_ZH_CN));

                if (LanguageCache_zh_CN == null)

                {

                    LanguageCache_zh_CN = new Dictionary<int,string> ();

                }

                LanguageCache_zh_CN = GetLanguageCache(strZhCN);

     }

    strZhCN是存放该语言(简体中文)的内容文件的路径,该文件(Resources/zh-CN.json)的格式为:

    [

    {Key:1,Value:"帐号无法使用,请联系管理员。"},

    {Key:2,Value:"关于"},

    {Key:3,Value:"关于活动"},

    {Key:4,Value:"关于圈子"},

    ……,

    ……,

    {Key:1729,Value:"详情请点击以下的链接:"}

    ]

     

    4.     GetLanguageCache(string)方法的实现

     public static Dictionary<int, string> GetLanguageCache(string strJson)

              {

                Dictionary<int, string> languageCache = new Dictionary<int, string>();

                SNET.MultilLanguage.Entity.MultilLanguage objlanguage = new SNET.MultilLanguage.Entity.MultilLanguage();

                List<object> listLanguage = SNET.Common.DataAccessHelper.JSONHelper.GetJSONList(objlanguage.GetType(), strJson);

                for (int i = 0; i < listLanguage.Count; i++)

                {

                    objlanguage = (SNET.MultilLanguage.Entity.MultilLanguage)listLanguage[i];

                    languageCache.Add(objlanguage.Key, objlanguage.Value);

                }

                objlanguage = null;

                return languageCache;

       }

    SNET.Common.DataAccessHelper.JSONHelper.GetJSONList(Type, string)是一个将json字符串反序列化的方法。具体实现略。

     

    5.     这样就将文件形式的语言包以键值对的形式(dictionary)缓存了起来,传入key,再配合以当前环境的LanguageID,确定是哪一个语言包文件,从相应的缓存中获取该key对应的value(也就是要显示的内容),这样就实现了在同一个站点下,多国语言的自由切换。

  • 相关阅读:
    C#static
    Sql中CHARINDEX用法
    分分钟用上C#中的委托和事件
    为什么使用抽象类?有什么好处?
    【NOIP】普及组2009 细胞分裂
    【VIJOS】P1512 SuperBrother打鼹鼠
    【NOIP】提高组2014
    @NOIP2018
    @NOIP2018
    @NOIP2018
  • 原文地址:https://www.cnblogs.com/myself/p/1672849.html
Copyright © 2020-2023  润新知