Is it possible to create a pseudo resource on the fly without using a .resx file using the ResourceManager?

I have a resource file strings.resx in my project that stores the default English strings. For testing purposes, I created another line of the .zh-CN.resx file, but the lines stored there are not real Chinese. These are just test strings that were generated after adding some characters to the strings stored in the strings.resx file (the main resource file). When a user logs into the zh-CN culture, this file helps catch hardcoded data and untranslated strings. Is it possible that I can get rid of the strings.zh-CN.resx file and generate a fake resource on the fly when the current thread culture is zh-CN?

Basically - are there any ways the ResourceManager can exchange fake strings for certain cultures without a physical .resx file?

I am using Visual Studio auto generated designer.cs file to fetch resource. I can see in the designer.cs file that there is one static property for each line in the .resx file. The way the ResourceManager is initialized in designer.cs is as below.

/// <summary>
///   Returns the cached ResourceManager instance used by this class.
/// </summary>

    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Resources.ResourceManager ResourceManager {
        get {
            if (object.ReferenceEquals(resourceMan, null)) {
                global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("xxx.xxx.Strings", typeof(Strings).Assembly);
                resourceMan = temp;
            }
            return resourceMan;
        }
    }

      

This is how the static property is added by the automatically generated code.

/// <summary>
///   Looks up a localized string similar to Absolute.
/// </summary>
public static string Absolute {
    get {
        return ResourceManager.GetString("Absolute", resourceCulture);
    }
}

      

I am looking for extension points in this search. if possible i want to hook my own ResourceManager (or whatever I'm not sure about) and return a fake pseudo string when the culture is zh-CN. The main goal is that I don't want to generate a fake physical resource file just for testing purposes.

+3


source to share


1 answer


Of course you can. Do you understand how the ResourceManager works ? There are several ways to achieve what you want. Here are a couple:

ONE OPTION . Take a look at the code behind the resource file. It tells you what you need to create, for example, ResourceManager

at some stage. One way to do this is to specify the resource type name (aka base name) and the assembly in which it can be found. Do the same! This will make you realize that you also need a resource type (class) on the fly, so do the following: create one resource type before instantiating ResourceManager

. The type must have a public property named for the fake string you want to retrieve. An example of how to do this is shown below: fooobar.com/questions/40915 / ... .



ANOTHER OPTION : Check out how a mock framework like Moq works . You can apply the same idea to spoof a resource type. So instead of creating a resource type on the fly, you can have a resource mock that can be taught to return your _fake stri

+1


source







All Articles