|
using System;
using System.Globalization;
using System.Windows.Markup;
namespace CBR.Core.Helpers.Localization
{
/// <summary>
/// Defines the handling method for the <see cref="ResxExtension.GetResource"/> event
/// </summary>
/// <param name="resxName">The name of the resx file</param>
/// <param name="key">The resource key within the file</param>
/// <param name="culture">The culture to get the resource for</param>
/// <returns>The resource</returns>
public delegate object GetResourceHandler(string resxName, string key, CultureInfo culture);
/// <summary>
/// A markup extension to allow resources for WPF Windows and controls to be retrieved
/// from a resource file associated with the window or control
/// </summary>
[MarkupExtensionReturnType(typeof(object))]
public class LocalizationExtension : ManagedMarkupExtension
{
#region ----------------PROPERTIES----------------
/// <summary>
/// The type name that the resource is associated with
/// </summary>
private string _resModul;
/// <summary>
/// The fully qualified name of the embedded resx (without .resources) to get the resource from
/// </summary>
public string ResModul
{
get { return _resModul; }
set { _resModul = value; }
}
/// <summary>
/// The key used to retrieve the resource
/// </summary>
private string _key;
/// <summary>
/// The name of the resource key
/// </summary>
public string Key
{
get { return _key; }
set { _key = value; }
}
/// <summary>
/// The default value for the property
/// </summary>
private string _defaultValue;
/// <summary>
/// The default value to use if the resource can't be found
/// </summary>
/// <remarks>
/// This particularly useful for properties which require non-null
/// values because it allows the page to be displayed even if the resource can't be loaded
/// </remarks>
public string DefaultValue
{
get { return _defaultValue; }
set { _defaultValue = value; }
}
#endregion
#region ----------------EVENTS----------------
/// <summary>
/// This event allows a designer or preview application (such as Globalizer.NET) to
/// intercept calls to get resources and provide the values instead dynamically
/// </summary>
public static event GetResourceHandler GetResource;
#endregion
#region ----------------CONSTRUCTORS----------------
/// <summary>
/// Create a new instance of the markup extension
/// </summary>
public LocalizationExtension() : base()
{
}
/// <summary>
/// Create a new instance of the markup extension
/// </summary>
/// <param name="resxName">The fully qualified name of the embedded resx (without .resources)</param>
/// <param name="key">The key used to get the value from the resources</param>
/// <param name="defaultValue">
/// The default value for the property (can be null). This is useful for non-string
/// that properties that may otherwise cause page load errors if the resource is not
/// present for some reason (eg at design time before the assembly has been compiled)
/// </param>
public LocalizationExtension(string key, string defaultValue)
: base()
{
this._key = key;
if (!string.IsNullOrEmpty(defaultValue))
{
this._defaultValue = defaultValue;
}
}
#endregion
#region ----------------MARKUP EXTENSION OVERRIDE----------------
/// <summary>
/// Return the value associated with the key from the resource manager
/// </summary>
/// <returns>The value from the resources if possible otherwise the default value</returns>
protected override object GetValue()
{
if (string.IsNullOrEmpty(Key))
throw new ArgumentException("Key cannot be null");
object result = null;
IResourceProvider provider = null;
try
{
object resource = null;
//allow resource trapping by calling the handler
if (GetResource != null)
resource = GetResource(ResModul, Key, CultureManager.Instance.UICulture);
if (resource == null)
{
//get the provider
if (provider == null)
provider = CultureManager.Instance.Provider;
//get the localized resource
if (provider != null)
resource = provider.GetObject(this, CultureManager.Instance.UICulture);
}
//and then convert it to desired type
result = provider.ConvertValue(this, resource);
}
catch (Exception err)
{
}
try
{
// if it does not work, we ask the default value
if (result == null)
result = provider.GetDefaultValue(this, CultureManager.Instance.UICulture);
}
catch (Exception err)
{
}
return result;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside
https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/