I have the following setup in my app:
MyResources.resx // english strings
MyResources.zh-CN.resx // chinese strings
Due to lag in the translation process, some keys have english values but no chinese values. In other cases, the entire zh-CN resx file does not exist. By default, ResourceManager
will fall back to the english value if the chinese value does not exist. This is acceptable for my use-case in most scenarios. However, I currently have a need to fetch a chinese resource string WITHOUT fallback to english.
My question is: what is the right way to do this?
I had initially thought that this could be done via the GetResourceSet method:
var manager = MyResources.ResourceManager;
var set = manager.GetResourceSet(CultureInfo.GetCultureInfo("zh-CN"), createIfNotExists: true, tryParents: false);
if (set == null || set.GetString("key") == null) { /* not translated! */ }
// however, this has issues because resource set lookup is cached:
// this will force the association of the zh-CN culture with the
// English resource set unde the hood
manager.GetString("key", CultureInfo.GetCultureInfo("zh-CN"));
// now this returns the English resource set, thus breaking my check
var set2 = manager.GetResourceSet(CultureInfo.GetCultureInfo("zh-CN"), createIfNotExists: true, tryParents: false);
if (set == null || set.GetString("key") == null) { /* checks whether key exists in english :-( */ }
ResourceNamespace.MyResourcesFileName.KeyofString
or by using ResourceManager with hardcoded key:ResourceManager.GetString("KeyofString")
?