Search code examples
c#asp.netenumswebformsresx

Link and enum to get value from resx file


i want to use enum as a key to get value from an resource file in my project i use and method to get value from resx file:

using System;
    using System.Reflection;
    using System.Resources;
    using System.Web;
    using Utility.Tools.ExtensionMethods;
    using Utility.Tools.Infrastructure;
    using Utility.Tools.Utilities.Storage;

    namespace MultiLanguage
    {
        public enum LocaleConfig_Type : byte
        {
            FA,

            AR,

            DARI
        }

        public class MultiLangToolkit
        {
            public static string GetTranslate(MainKeys key)
            {
                var lang = LanguegesToolkit.GetCurentLang();

                return GetTranslate(key, lang);
            }

            public static string GetTranslate(MainKeys key, LocaleConfig_Type locale)
            {
                try
                {
                    string value;
                    switch (locale)
                    {                        case LocaleConfig_Type.FA:
                            {
                                var fa_resx = new ResourceManager("MultiLanguage.FA", Assembly.GetExecutingAssembly());
                                value = fa_resx.GetString(key.ToString());
                            }
                            break;
                        case LocaleConfig_Type.AR:
                            {
                                var ar_resx = new ResourceManager("MultiLanguage.AR", Assembly.GetExecutingAssembly());
                                value = ar_resx.GetString(key.ToString());
                            }
                            break;
                        case LocaleConfig_Type.DARI:
                            {
                                var dari_resx = new ResourceManager("MultiLanguage.DARI", Assembly.GetExecutingAssembly());
                                value = dari_resx.GetString(key.ToString());
                            }
                            break;

                        default:
                            value = "No Language Selected!";
                            break;
                    }

                    if (!string.IsNullOrEmpty(value))
                        return value;

                    return "Key Or Value NotFound!";
                }
                catch (Exception)
                {
                    return "Exception when translate";
                }
            }
        }

        public class LanguegesToolkit
        {
            public static LocaleConfig_Type GetCurentLang()
            {
                try
                {
                    object sessionDefalutLocale = null;

                    if (HttpContext.Current.Session != null)
                    {
                        sessionDefalutLocale = HttpContext.Current.Session[SessionManager.SESSION_USER_DEFAULT_LOCAL];
                    }
                    if (sessionDefalutLocale != null)
                    {
                        return sessionDefalutLocale.ToString().ToEnum<LocaleConfig_Type>();
                    }
                    else
                    {
                        var defaultLocale = RequestGlobalProperties.CurrentUserRequest.DefaultLocale;

                        if (defaultLocale.IsNullOrEmpty())
                            return Constants.ServiceDefaultLocale.ToEnum<LocaleConfig_Type>();

                        return defaultLocale.ToEnum<LocaleConfig_Type>();
                    }
                }
                catch (Exception)
                {
                    return LocaleConfig_Type.FA;
                }
            }
        }
    }

and i have a enum class to store enums:

namespace MultiLanguage
{
    public enum MainKeys
    {
        /// <summary>
        /// login to account
        /// </summary>
        Sign_in,

        /// <summary>
        /// password
        /// </summary>
        Password,

        /// <summary>
        /// user name
        /// </summary>
        Username
}
}

and i use this code to get value from my resx files

 MultiLanguage.MultiLangToolkit.GetTranslate(MultiLanguage.MainKeys.Username)

but this type of code is too many for getting a value from my resx file i just want to write this code instants the code above:

 MultiLanguage.MainKeys.Username 

simply just call the enum and get translated value from resx file i use this code in a dll file to use that in my other projects and use and enum as a key instants of string because strings is not a good choice and my cause and runtime issue and it's hard to debug that. and using summery on the enums to see that is the value in resx file(yes i copy the value frome my rex file into summery)
how can i do that? is that possible ?


Solution

  • If you really want to stick with your enumerations then you could just create an extension method to simplify the usage. Something like

    public static class EnumExtensions
    {
      public static string Translate(this MainKeys key)
      {
        return MultiLanguage.MultiLangToolkit.GetTranslate(key);  
      }
    }
    

    then you can just use

    MultiLanguage.MainKeys.Username.Translate();