Example #1
0
        public static Dictionary <string, string> Strings(string viewVirtualPath, CultureInfo cultureInfo)
        {
            #region Precompiled

            var resourceManager = CompiledResources.GetResourceManager(viewVirtualPath);
            try
            {
                var resourceSet = resourceManager.GetResourceSet(cultureInfo, false, true);

                if (resourceSet == null)
                {
                    return(GetDefaultCultureResourceSet(resourceManager, cultureInfo));
                }

                var result = new Dictionary <string, string>();

                var enumerator = resourceSet.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    result.Add(enumerator.Key.ToString(), (string)enumerator.Value);
                }

                return(result);
            }
            catch (NullReferenceException ex)
            {
                throw new NullReferenceException(
                          "NullReferenceException was met with 'viewVirtualPath' = '{0}', 'CultureInfo' = '{1}'.".FormatWith(
                              viewVirtualPath, cultureInfo), ex);
            }
            #endregion

            #region Non precompiled
            var dic        = new Dictionary <string, string>();
            var resXReader = new ResXResourceReader(GetResourceFileFullName(viewVirtualPath, cultureInfo));

            foreach (DictionaryEntry entry in resXReader)
            {
                var value = entry.Value.ToString();
                //value = value.Replace("\"", "\\\"").Replace("'", "\\'").Replace("\r", "")
                //    .Replace("\n", "");
                dic.Add(entry.Key.ToString(), value);
            }
            resXReader.Close();

            return(dic);

            #endregion
        }
        public static string Localize(this WebPageExecutingBase page, string resourceKey, string defaultValue = null)
        {
            #region Precompiled
            return(CompiledResources.GetResourceManager(page.VirtualPath).GetString(resourceKey));

            #endregion

            #region Not precompiled
            var localResourceObject = HttpContext.GetLocalResourceObject(page.VirtualPath, resourceKey);

            if (localResourceObject != null)
            {
                return(localResourceObject.ToString());
            }
            else
            {
                return(defaultValue ?? resourceKey);
            }
            #endregion
        }