3

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
murtazat
  • 399
  • 3
  • 12

1 Answers1

1

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

Community
  • 1
  • 1
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
  • Thanks. I am trying to understand option 1. I think it looks a decent approach but problem is how this can be done in designer.cs file which is auto generated. Does it provide any extension points? – murtazat May 14 '15 at 23:05
  • I mean how to trick the localization framework to believe that fake strings are existing in some fake class (created by the approach you mentioned) at runtime. How to load such class that contains fake strings? – murtazat May 14 '15 at 23:07
  • I know I am asking for too much but please provide any code sample of option 1 in context of designer.cs file that is generated by visual studio. Thank you very much. – murtazat May 14 '15 at 23:13
  • Hey @murtazat, I suggest you familiarize yourself with the code behind and read articles about the resource manager. The manager itself is a class that comes with the .NET framework while resource files are technically generated access classes - this is why there is an auto-generated code behind. If you have strings in your resource only then everything sits in the generated source code. Now think one step further: If you need a class (type) to satisfy the resource manager then you will have to provide it one way or the other. There is no "tricking" in .NET. Option 1 explains one way to do so. – Quality Catalyst May 14 '15 at 23:32