⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resources.cs

📁 ASP.NET简洁论坛源代码 这是一个简单的论坛
💻 CS
字号:
using System.Collections;
using System.Collections.Specialized;
using System.Web.Caching;
using System.Xml;
using NetFocus.Web.Core;

namespace NetFocus.Web.Applications.Forum
{
    public class Resources
    {
        public static NameValueCollection GetSupportedLanguages()
        {
            WebContext context = WebContext.Current;

            string cacheKey = "Forums-SupportedLanguages";

            NameValueCollection supportedLanguages = Caches.Get(cacheKey) as NameValueCollection;
            if (supportedLanguages == null)
            {
                string filePath = context.MapPath("~/Languages/languages.xml");
                CacheDependency dp = new CacheDependency(filePath);
                supportedLanguages = new NameValueCollection();

                XmlDocument d = new XmlDocument();
                d.Load(filePath);

                foreach (XmlNode n in d.SelectSingleNode("root").ChildNodes)
                {
                    if (n.NodeType != XmlNodeType.Comment)
                    {
                        supportedLanguages.Add(n.Attributes["key"].Value, n.Attributes["name"].Value);
                    }
                }

                Caches.Max(cacheKey, supportedLanguages, dp);
            }

            return supportedLanguages;
        }

        public static NameValueCollection GetLanguageCharsets()
        {
            WebContext context = WebContext.Current;

            string cacheKey = "LanguageCharsets";

            NameValueCollection languageCharsets = Caches.Get(cacheKey) as NameValueCollection;
            if (languageCharsets == null)
            {
                string filePath = context.MapPath("~/Languages/languages.xml");
                CacheDependency dp = new CacheDependency(filePath);
                languageCharsets = new NameValueCollection();

                XmlDocument d = new XmlDocument();
                d.Load(filePath);

                foreach (XmlNode n in d.SelectSingleNode("root").ChildNodes)
                {
                    if (n.NodeType != XmlNodeType.Comment)
                    {
                        if (n.Attributes["emailCharset"] == null)
                            continue;
                        string codepage = n.Attributes["emailCharset"].Value;
                        if (n.Attributes["emailSubjectCharset"] != null)
                            codepage += "," + n.Attributes["emailSubjectCharset"].Value;

                        languageCharsets.Add(n.Attributes["key"].Value, codepage);
                    }
                }

                Caches.Max(cacheKey, languageCharsets, dp);
            }

            return languageCharsets;
        }

        public static string GetSupportedLanguage(string language)
        {
            return GetSupportedLanguage(language, Configuration.Instance.DefaultLanguage);
        }
        public static string GetSupportedLanguage(string language, string languageDefault)
        {
            NameValueCollection supportedLanguages = GetSupportedLanguages();
            string supportedLanguage = supportedLanguages[language];
            if (!string.IsNullOrEmpty(supportedLanguage))
                return language;
            else
                return languageDefault;
        }

        public static string GetString(string name)
        {

            return GetString(name, false);
        }
        public static string GetString(string name, bool defaultOnly)
        {
            return GetString(name, "Resources.xml", defaultOnly);
        }
        public static string GetString(string name, string fileName)
        {
            return GetString(name, fileName, false);
        }
        public static string GetString(string name, string fileName, bool defaultOnly)
        {

            Hashtable resources = null;
            WebContext cSContext = WebContext.Current;
            string userLanguage = "zh-CN";

            if (fileName != null && fileName != "")
                resources = GetResource(ResourceType.String, userLanguage, fileName, defaultOnly);
            else
                resources = GetResource(ResourceType.String, userLanguage, "Resources.xml", defaultOnly);

            string text = resources[name] as string;

            //try the standard file if we passed a file that didnt have the key we were looking for
            if (text == null && fileName != null && fileName != "")
            {
                resources = GetResource(ResourceType.String, userLanguage, "Resources.xml", false);

                text = resources[name] as string;
            }

            if (text == null)
            {
                text = string.Empty;
            }
            return text;
        }

        //public static Message GetMessage(CSExceptionType exceptionType)
        //{

        //    Hashtable resources = GetResource(ResourceManagerType.ErrorMessage, WebContext.Current.User.Profile.Language, "Messages.xml", false);

        //    if (resources[(int)exceptionType] == null)
        //    {
        //        throw new CSException(CSExceptionType.ResourceNotFound, "Value not found in Messages.xml for: " + exceptionType);
        //    }

        //    return (Message)resources[(int)exceptionType];
        //}
        
        private static Hashtable GetResource(ResourceType resourceType, string userLanguage, string fileName, bool defaultOnly)
        {


            string defaultLanguage = Configuration.Instance.DefaultLanguage;
            string cacheKey = resourceType.ToString() + defaultLanguage + userLanguage + fileName;

            // Ensure the user has a language set
            //
            if (string.IsNullOrEmpty(userLanguage) || defaultOnly)
                userLanguage = defaultLanguage;

            // Attempt to get the resources from the Cache
            //
            Hashtable resources = Caches.Get(cacheKey) as Hashtable;

            if (resources == null)
            {
                resources = new Hashtable();

                // First load the Chinese resouce, changed from loading the default language
                // since the userLanguage is set to the defaultLanguage if the userLanguage
                // is unassigned. We load the english language always just to ensure we have
                // a resource loaded just incase the userLanguage doesn't have a translated
                // string for this English resource.
                //
                resources = LoadResource(resourceType, resources, "zh-CN", cacheKey, fileName);

                // If the user language is different load it
                //
                if ("zh-CN" != userLanguage)
                    resources = LoadResource(resourceType, resources, userLanguage, cacheKey, fileName);

            }

            return resources;
        }
        private static Hashtable LoadResource(ResourceType resourceType, Hashtable target, string language, string cacheKey, string fileName)
        {
            string filePath = WebContext.Current.PhysicalPath("Languages\\" + language + "\\" + fileName);

            //			switch (resourceType) {
            //				case ResourceType.ErrorMessage:
            //					filePath = string.Format(filePath, "Messages.xml");
            //					break;
            //
            //				default:
            //					filePath = string.Format(filePath, "Resources.xml");
            //					break;
            //			}

            CacheDependency dp = new CacheDependency(filePath);

            XmlDocument d = new XmlDocument();
            try
            {
                d.Load(filePath);
            }
            catch
            {
                return target;
            }

            foreach (XmlNode n in d.SelectSingleNode("root").ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment)
                {
                    switch (resourceType)
                    {
                        case ResourceType.ErrorMessage:
                            //Message m = new Message(n);
                            //target[m.MessageId] = m;
                            //break;

                        case ResourceType.String:
                            if (target[n.Attributes["name"].Value] == null)
                                target.Add(n.Attributes["name"].Value, n.InnerText);
                            else
                                target[n.Attributes["name"].Value] = n.InnerText;
                            break;
                    }
                }
            }

            if (language == Configuration.Instance.DefaultLanguage)
            {
                Caches.Max(cacheKey, target, dp);
            }
            else
            {
                Caches.Insert(cacheKey, target, dp, Caches.MinuteFactor * 5);
            }

            return target;

        }

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -