Esempio n. 1
0
 /// <summary>
 /// 添加资源缓存
 /// </summary>
 /// <param name="entry">资源</param>
 protected void AddResourceCache(ResourceEntry entry)
 {
     lock (lockResources)
     {
         if (languages == null)
         {
             languages = new Dictionary <string, ResourceEntry>();
         }
         else
         {
             languages.Add(entry.Name, entry);
         }
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 返回特定语言的编码
        /// </summary>
        /// <param name="name">资源名称</param>
        /// <param name="culture">语言编码</param>
        /// <returns>资源</returns>
        public string GetResource(string name, string culture)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Resource name cannot be null or empty.");
            }

            if (string.IsNullOrWhiteSpace(culture))
            {
                throw new ArgumentException("Culture name cannot be null or empty.");
            }

            // 本地化
            culture = culture.ToLowerInvariant();

            if (Cache && languages == null)
            {
                //一次性将所有资源添加到缓存
                lock (lockResources)
                {
                    if (languages == null)
                    {
                        languages = ReadResources().ToDictionary(r => r.Name);
                    }
                }
            }

            ResourceEntry entry = null;

            if (Cache)
            {
                if (languages != null)
                {
                    entry = languages[name];
                }
            }
            if (entry == null)
            {
                entry = ReadResource(name, culture);
            }
            return(entry == null ? "" : entry[culture]);
        }