我的項目中有一個資源文件strings.resx,用於存儲默認的英文字符串。爲了測試目的,我創建了另外一個文件strings.zh-CN.resx,但存儲的字符串不是真正的中文。它們只是將字符串附加到存儲在strings.resx(主資源文件)中的字符串後生成的測試字符串。當用戶登錄到zh-CN文化時,該文件有助於捕獲硬編碼數據和未翻譯的字符串。噹噹前線程文化是zh-CN時,是否有可能擺脫strings.zh-CN.resx文件並動態生成假資源?是否可以在不使用ResourceManager使用.resx文件的情況下即時創建僞資源?
基本上 - 有什麼辦法可以讓ResourceManager在沒有物理.resx文件的情況下爲特定文化返回假字符串?
我正在使用Visual Studio自動生成的designer.cs文件來獲取資源。我可以在designer.cs文件中看到,.resx文件中每個字符串都有一個靜態屬性。 ResourceManager在designer.cs中初始化的方式如下。
/// <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;
}
}
這是通過自動生成的代碼添加靜態屬性的方式。
/// <summary>
/// Looks up a localized string similar to Absolute.
/// </summary>
public static string Absolute {
get {
return ResourceManager.GetString("Absolute", resourceCulture);
}
}
我在查找擴展點。如果可能的話,我想鉤入自己的自定義ResourceManager(或其他我不確定的東西),並在文化爲zh-CN時返回假僞字符串。主要目的是爲了測試目的,我不想生成一個虛假的物理資源文件。
請通過[編輯]添加其他信息到您的問題,並從評論中刪除它們。 – Alex
順便說一句,'ResourceManager'是.NET的一部分,不屬於C# –