I have this function and it works fine to get a translated value from this specific resource file called OkayMessages.
public static string GetReso
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();
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;