2

I have this function and it works fine to get a translated value from this specific resource file called OkayMessages.

public static string GetResourceString(string resourceKey){
  ResourceManager resourceManager = Resources.OkayMessages.ResourceManager;
  return resourceManager.GetString(resourceKey);
}

But i have more than 1 resource file and i want this function to get values from those files as well.. Only, i'm having trouble with dynamically/programmatically selecting the right resource(manager). I have tried to use the code below, and some variants to that, but i always get an error.

public static string GetResourceString(string resourceFile, string resourceKey){
  ResourceManager resourceManager = new System.Resources.ResourceManager("Resources." + resourceFile, Assembly.GetExecutingAssembly());
  return resourceManager.GetString(resourceKey);
}

The error i got most of the times was: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resources.OkayMessages.resources" was correctly embedded or linked into assembly..

Update: I'm using the \App_GlobalResources\ folder for my resource files, and it seems that this is the problem. When i place a resource file in the root of my project, i can initialize a ResourceManager without problems.

Tys
  • 3,592
  • 9
  • 49
  • 71
  • 1
    So the underlying problem here is that resources in the App_GlobalResources folder get compiled into their own assembly (App_GlobalResources). The GetGlobalResourceObject method knows to use the correct assembly. – Kenneth Ito Jul 08 '12 at 18:07

2 Answers2

5

After searching in the wrong direction for a while, I just found the most simple answer to this problem. It turns out that there is a method called GetGlobalResourceObject.

So in my case I'm now using this line of code which does all:

GetGlobalResourceObject("OkayMessages", "PasswordChanged").ToString();
Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
Tys
  • 3,592
  • 9
  • 49
  • 71
  • Were you able to do this using files named Resources.OkayMessages (like your original example implies), or did you name it just "OkayMessages"? To get it working I had to have no periods in the file name because it kept thinking that the period was the speration point for the localization. – Peter Jul 13 '12 at 16:54
1

Read carefully this article and you'll find that you need to specify correct namespace of the resource. That's your problem. Here is working example if OkayResources.resx resides in project root folder:

using System.Reflection;
using System.Resources;
using System.Web.UI;

namespace WebApplication1
{
    public partial class _Default : Page
    {

        public _Default()
        {
            var result = GetResourceString("OkayResources", "SomeKey");
        }

        private static string GetResourceString(string resourceFileName, string key)
        {
            var resourceName = "WebApplication1." + resourceFileName;
            var resourceManager = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
            return resourceManager.GetString(key);
        }
    }
}

If you put your resource file into Resources folder you'll have to update resource namespace:

var resourceName = "WebApplication1.Resources." + resourceFileName;
Sergei B.
  • 3,227
  • 19
  • 18
  • I'm still getting that same error. I'm using the \App_GlobalResources\ folder and because of that, my resource files reside in the namespace Resources. Would that make a difference? – Tys Jul 08 '12 at 14:04
  • In other parts of my code, i normally call a resource string like this Resources.OkayMessages.UserLoggedIn – Tys Jul 08 '12 at 14:05
  • I have updated my question with some findings after playing around with your solution. The problem seems to be the use of the \App_GlobalResources\ folder. – Tys Jul 08 '12 at 14:16
  • App_GlobalResources is not an issue itself. You have to update namespace. In your case it will be `YourDefaultNamespace.App_GlobalResources.FileName`. So, I believe my answer is correct. – Sergei B. Jul 08 '12 at 15:02
  • I just tried this, and some variations, but still the same error. The good thing is, that it did make me search in some different directions, and i found the solution. I'll write an answer for it. – Tys Jul 08 '12 at 15:55