Reputation: 5970
Given I have two resourceManagers:
var mgr1 = new ResourceManager("NS1.StringResources", Assembly.GetExecutingAssembly());
var mgr2 = new ResourceManager("NS2.OtherStringResources", _otherAssembly);
Is there a way that i can merge them? Or Their resourcesets. such that i can have one manager or resourceset.
Upvotes: 0
Views: 274
Reputation: 5970
Ended up with something similar as it seems dictionary is only way to go.
public Dictionary<string, string> GetTotalResourceSet(CultureInfo culture)
{
Dictionary<string, string> set;
if (_resources.TryGetValue(culture.Name, out set))
return set;
var wl = getWhitelabelResourceSet(culture);
var loc = getLocalResourceSet(culture);
var dict = loc.Cast<DictionaryEntry>()
.ToDictionary(x => { return x.Key.ToString(); }, x =>
{
if (x.Value.GetType() == typeof(string))
return x.Value.ToString();
return "";
});
if (wl != null)
{
var wlDict = wl.Cast<DictionaryEntry>()
.ToDictionary(x => { return x.Key.ToString(); }, x =>
{
if (x.Value.GetType() == typeof(string))
return x.Value.ToString();
return "";
});
set = wlDict.Concat(dict.Where(kvp => !wlDict.ContainsKey(kvp.Key))).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
else
{
set = dict;
}
if (set != null)
{
_resources.TryAdd(culture.Name, set);
}
return set;
}
Upvotes: 1
Reputation: 7456
Not pretty but you will end up in a Merged dictionary:
var mgr1 = new ResourceManager("NS1.StringResources", Assembly.GetExecutingAssembly());
var mgr2 = new ResourceManager("NS2.OtherStringResources", _otherAssembly);
var combined = new Dictionary<string, object>();
ResourceSet resourceSet = mgr1.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet) {
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
combined.Add(resourceKey, resource);
}
resourceSet = mgr2.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet) {
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
combined.Add(resourceKey, resource);
}
Upvotes: 1