Search code examples
c#asp.net-mvcembedded-resource

How to create separate resource files for debugging and releasing?


Is it possible to create two files, for example Text.Debug.resx and Text.Release.resx, where the appropriate resource file is automatically loaded during debugging and releasing of the program?


Solution

  • I'd wrap the ResourceManager:

    public class Resources
    {
        private readonly ResourceManager _resourceManager;
    
        public Resources()
        {
    #if DEBUG
            const string configuration = "Debug";
    #else
            const string configuration = "Release";
    #endif
    
            _resourceManager = new ResourceManager($"StackOverflow.Text.{configuration}", typeof(Resources).Assembly);
        }
    
        public string GetString(string resourceKey)
        {
            return _resourceManager.GetString(resourceKey);
        }
    }
    

    Obviously, amend the namespace appropriately when newing up the manager.

    Edit

    You could also implement it as a static class to avoid having to new up an instance of the wrapper:

    public static class Resources
    {
        private static ResourceManager _resourceManager;
    
        public static string GetString(string resourceKey)
        {
            if (_resourceManager != null)
            {
                return _resourceManager.GetString(resourceKey);
            }
    
    #if DEBUG
            const string configuration = "Debug";
    #else
            const string configuration = "Release";
    #endif
    
            _resourceManager = new ResourceManager($"StackOverflow.Text.{configuration}", typeof(Resources).Assembly);
    
            return _resourceManager.GetString(resourceKey);
        }
    }