175 lines
4.4 KiB
C#
175 lines
4.4 KiB
C#
using System;
|
|
using System.Resources;
|
|
using System.Reflection;
|
|
using System.Globalization;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace Resources
|
|
{
|
|
|
|
public delegate void ApplyValueDelegate(object @object, string resourceName, object value);
|
|
|
|
public struct ResourceItem : IEquatable<ResourceItem>
|
|
{
|
|
private object @object;
|
|
private string resourceName;
|
|
private ApplyValueDelegate @delegate;
|
|
|
|
public ResourceItem(object @object, string resourceName, ApplyValueDelegate @delegate)
|
|
{
|
|
this.@object = @object;
|
|
this.resourceName = resourceName;
|
|
this.@delegate = @delegate;
|
|
}
|
|
|
|
public object Object
|
|
{
|
|
get
|
|
{
|
|
return @object;
|
|
}
|
|
}
|
|
|
|
public string ResourceName
|
|
{
|
|
get
|
|
{
|
|
return resourceName;
|
|
}
|
|
}
|
|
|
|
public ApplyValueDelegate Delegate
|
|
{
|
|
get
|
|
{
|
|
return @delegate;
|
|
}
|
|
}
|
|
|
|
public bool Equals(ResourceItem item)
|
|
{
|
|
return object.ReferenceEquals(@object, item.@object) && resourceName.Equals(item.resourceName);
|
|
}
|
|
}
|
|
|
|
public static class ResourceManagerEx
|
|
{
|
|
|
|
public class ResourceItemCollection : Collection<ResourceItem>
|
|
{
|
|
|
|
protected override void InsertItem(int index, ResourceItem item)
|
|
{
|
|
base.InsertItem(index, item);
|
|
|
|
ResourceManagerEx.ApplyResource(item);
|
|
}
|
|
|
|
protected override void SetItem(int index, ResourceItem item)
|
|
{
|
|
base.SetItem(index, item);
|
|
|
|
ResourceManagerEx.ApplyResource(item);
|
|
}
|
|
}
|
|
|
|
private static ResourceManager resourceManager;
|
|
private static string baseName;
|
|
private static CultureInfo resourceCulture;
|
|
private static ResourceItemCollection collection;
|
|
|
|
static ResourceManagerEx()
|
|
{
|
|
resourceManager = null;
|
|
baseName = String.Empty;
|
|
resourceCulture = null;
|
|
collection = new ResourceItemCollection();
|
|
}
|
|
|
|
public static ResourceManager ResourceManager
|
|
{
|
|
get
|
|
{
|
|
if (resourceManager == null)
|
|
{
|
|
resourceManager = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
|
|
}
|
|
|
|
return resourceManager;
|
|
}
|
|
}
|
|
|
|
public static string BaseName
|
|
{
|
|
get
|
|
{
|
|
return baseName;
|
|
}
|
|
|
|
set
|
|
{
|
|
baseName = value;
|
|
}
|
|
}
|
|
|
|
public static CultureInfo ResourceCulture
|
|
{
|
|
get
|
|
{
|
|
return resourceCulture;
|
|
}
|
|
|
|
set
|
|
{
|
|
resourceCulture = value;
|
|
ApplyResources();
|
|
}
|
|
}
|
|
|
|
public static ResourceItemCollection Items
|
|
{
|
|
get
|
|
{
|
|
return collection;
|
|
}
|
|
}
|
|
|
|
public static object GetResource(string resourceName)
|
|
{
|
|
return ResourceManager.GetObject(resourceName, resourceCulture);
|
|
}
|
|
|
|
public static void ApplyResources()
|
|
{
|
|
foreach (ResourceItem item in collection)
|
|
{
|
|
ApplyResource(item);
|
|
}
|
|
}
|
|
|
|
internal static void ApplyResource(ResourceItem item)
|
|
{
|
|
string resourceName = item.ResourceName;
|
|
object value = ResourceManager.GetObject(resourceName, resourceCulture);
|
|
|
|
#if DEBUG
|
|
if (value == null)
|
|
{
|
|
CultureInfo c = (resourceCulture == null) ? CultureInfo.CurrentUICulture : resourceCulture;
|
|
string message = String.Format("Could not load Resource '{0}'({1}).", resourceName, c.EnglishName);
|
|
|
|
throw new System.ComponentModel.WarningException(message);
|
|
}
|
|
#endif
|
|
item.Delegate(item.Object, resourceName, value);
|
|
}
|
|
|
|
public static void ApplyTextToControl(object @object, string resourceName, object value)
|
|
{
|
|
System.Windows.Forms.Control c = (System.Windows.Forms.Control)@object;
|
|
|
|
c.Text = value.ToString();
|
|
}
|
|
}
|
|
}
|