Get the value of a resource * without * using GetGlobalResourceObject

I have an MVC web application that uses a user folder and namespace for resource files. I need to be able to pull the latest values ​​from my resource file, i.e. use the resource type and string. I tried using:

HttpContext.GetGlobalResourceObject("ResourceClass", "SomeKey")

But this returns null. When I transfer a resource to App_GlobalResources, this works, but cascades to other problems. I tried to work with them, but they seem deeper and more significant than returning to my original plan, which is only that you can read from the resource file in a user folder.

In short, I am trying to localize the xVal check, both the error message and the RegEx authentication pattern. Everything works for me, except for one part where I try to localize a template. Since this is not built into DataAnnotations.RegularExpressionAttribute, I need to get it myself, based on the type of resource and the name specified in the attribute. Hence my dilemma.

Is it possible to get the value of a resource using another method? Or, should my resource file be in the App_GlobalResources folder? If the latter, then I will need to open another discussion for all my other problems - or implement some rougher form of localization for the material of regular expressions.

Thanks in advance.

Jerad

+3
source share
1 answer

, :

public static class ResourceHelper
{
    public static string GetString(Type resourceType, string resourceName)
    {
        return new ResourceManager(resourceType.FullName, resourceType.Assembly)
            .GetString(resourceName);
    }

    public static string GetString(Type resourceType, string resourceName, CultureInfo culture)
    {
        return new ResourceManager(resourceType.FullName, resourceType.Assembly)
            .GetString(resourceName, culture);
    }

    public static object GetObject(Type resourceType, string resourceName)
    {
        return new ResourceManager(resourceType.FullName, resourceType.Assembly)
            .GetObject(resourceName);
    }

    public static object GetObject(Type resourceType, string resourceName, CultureInfo culture)
    {
        return new ResourceManager(resourceType.FullName, resourceType.Assembly)
            .GetObject(resourceName, culture);
    }
}
+4

Source: https://habr.com/ru/post/1733464/


All Articles