Reputation: 419
I have a resource file strings.resx in my project that stores default English strings. For testing purpose, I created one more file strings.zh-CN.resx but the strings stored there are not real Chinese. They are just test strings that have been generated after appending some characters to the strings stored in strings.resx (main resource file). When user logs into zh-CN culture, this file helps to catch hard coded data and untranslated strings. Is it possible that I can get rid of strings.zh-CN.resx file and generate a fake resource on the fly when current thread culture is zh-CN?
Basically - are there any ways we can trick ResourceManager to return fake strings for particular cultures without having a physical .resx file?
I am using Visual Studio auto generated designer.cs file to fetch resource. I can see in designer.cs file that there is one static property for each string in .resx file. The way ResourceManager is initialized in designer.cs is as under.
/// <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 the way static property is added by auto 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 retrieval. if possible I want to hook in my own custom ResourceManager (or something else I am not sure) and return a fake pseudo string when culture is zh-CN. Main purpose is I do not want to be generating a fake physical resource file just for testing purpose.
Upvotes: 3
Views: 1104
Reputation: 6795
Of course you can. Do you understand how the ResourceManager works? There are multiple ways to achieve what you want. A couple are these:
ONE OPTION: Look at the code behind of a resource file. It tells you that you need to create in instance of the ResourceManager
at some stage. One way to do so is to provide the name of a resource type (aka base name) and the assembly in which it can be found. Do the same! This will make you realize that you will also need a resource type (class) on the fly - so do that: create one resource type before you create an instance of the ResourceManager
. The type needs to have a public property with the name of the fake string which you want to retrieve. An example how to do that is given here: Stackoverflow: Dynamically create a class in C#.
ANOTHER OPTION: Familiarize yourself with how a mock framework works such as Moq. You can apply the same idea for faking a resource type. So rather than creating a resource type on the fly you can have a resource mock which can be taught to return your _fake stri
Upvotes: 1