Friday, September 20, 2013

Globalize WPF application using Localization

When you create a new product, it should be available to all people in worldwide and they could understand by his own language. When you limit your product's availability to only one language (Say in English), you limit your potential customer base. If you want your applications to reach a global audience, cost-effective localization of your product is one of the best and most economical ways to reach more customers.

Here I am going to apply globalization and localization in my Windows Presentation Foundation (WPF) application. Globalization is the design and development of applications that perform in multiple locations. Localization is the translation of application resources into localized versions for the specific cultures that the application supports.

A simple and effective way to localize application resources is to write a custom MarkupExtension that provides a localized value. The extension takes a parameter in the constructor that is the unique resource key. When the DepdendencyProperty asks for the value, the markup extension looks up the value from a generic resource provider. This gives you the flexibility to reuse the resource management and translation process that is already established within your company.

The localization module consists of the following parts.

Localization manager

The localization manager is a static class which manages the current language and notifies to all markup extensions which are specified in RESX file, to update their values when the language changes. It also provides access to localized resources. The resources itself are provided by a generic translation provider.

public class LocalizationManager
    {
        public static event EventHandler CultureChanged;
        public static ILocalizedResourceProvider LocalizationProvider { get; set; }
        public static IList<CultureInfo> SupportedCultures { get; private set; }

        public static CultureInfo CurrentCulture
        {
            get { return CultureInfo.CurrentUICulture; }
            set
            {
                Thread.CurrentThread.CurrentUICulture = value;

                if (CultureChanged != null)
                {
                    CultureChanged(null, EventArgs.Empty);
                }
            }
        }

        public static object GetValue(string key)
        {
            if (LocalizationProvider != null)
            {
                return LocalizationProvider.GetValue(key);
            }
            return null;
        }

        static LocalizationManager()
        {
            SupportedCultures = new List<CultureInfo>();
        }
    }

Localize markup extension

The localize markup extension knows the resource key and provides the localized value. It listens to the CultureChanged event of the localization manager and update its value. 

[MarkupExtensionReturnType(typeof(string)), Localizability(LocalizationCategory.NeverLocalize)]
    public class LocalizeExtension : MarkupExtension
    {
        private DependencyObject _targetObject;
        private DependencyProperty _targetProperty;
        private TypeConverter _typeConverter;

        public LocalizeExtension()
        {
            LocalizationManager.CultureChanged += LocalizationManager_CultureChanged;
        }

        ~LocalizeExtension()
        {
            LocalizationManager.CultureChanged -= LocalizationManager_CultureChanged;
        }

        private void LocalizationManager_CultureChanged(object sender, EventArgs e)
        {
            UpdateTarget();
        }

        public LocalizeExtension(string key) : this()
        {
            Key = key;
        }

        [ConstructorArgument("key")]
        public string Key { get; set; }

        [ConstructorArgument("format")]
        public string Format { get; set; }

        [ConstructorArgument("DefaultValue")]
        public object DefaultValue { get; set; }

        [ConstructorArgument("Converter")]
        public IValueConverter Converter { get; set; }

        internal void UpdateTarget()
        {
            if (_targetObject != null && _targetProperty != null)
            {
                _targetObject.SetValue(_targetProperty, ProvideValueInternal());
            }
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            // Resolve the depending object and property
            if (_targetObject == null)
            {
                var targetHelper = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
                _targetObject = targetHelper.TargetObject as DependencyObject;
                _targetProperty = targetHelper.TargetProperty as DependencyProperty;
                _typeConverter = TypeDescriptor.GetConverter(_targetProperty.PropertyType);
            }

            return ProvideValueInternal();
        }

        private object ProvideValueInternal()
        {
            // Get the localized value
            object value = LocalizationManager.GetValue(Key);

            // Automatically convert the type if a matching type converter is available
            if (value != null && _typeConverter != null && _typeConverter.CanConvertFrom(value.GetType()))
            {
                value = _typeConverter.ConvertFrom(value);
            }

            // If the value is null, use the fallback value if available
            if ((value == null && DefaultValue != null) || Convert.ToString(value) == "")
            {
                value = DefaultValue;
            }

            // If no fallback value is available, return the key
            if (value == null)
            {
                if (_targetProperty != null && _targetProperty.PropertyType == typeof(string))
                {
                    // Return the key surrounded by question marks for string type properties
                    value = string.Concat("?", Key, "?");
                }
                else
                {
                    // Return the UnsetValue for all other types of dependency properties
                    return DependencyProperty.UnsetValue;
                }
            }

            if (Converter != null)
            {
                value = Converter.Convert(value, _targetProperty.PropertyType, null, CultureInfo.CurrentCulture);
            }

            // Format the value if a format string is provided and the type implements IFormattable
            if (value is IFormattable && Format != null)
            {
                ((IFormattable)value).ToString(Format, CultureInfo.CurrentCulture);
            }
            return value;
        }
    }

Resource provider

The resource provider is a class that provides the localized resources. It has to implement the ILocalizedResourceProvider and can access any kind of resources you like RESX files.

public class ResourceFileProvider : ResourceManager, ILocalizedResourceProvider
    {
        private ResourceSet _resourceSet;

        public object GetValue(string key)
        {
            try
            {
                if (_resourceSet != null)
                {
                    return _resourceSet.GetObject(key);
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        private void LoadResources()
        {
            ReleaseAllResources();
            _resourceSet = GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        }

        public ResourceFileProvider(string baseName, Assembly assembly) : base(baseName, assembly)
        {
            LoadResources();
            LocalizationManager.CultureChanged += (sender, e) => { LoadResources(); };
        }
    }

How to use it

The usage of the markup extension is very simple. Just replace the string you want to localize by {Localize resourceKey}. Where resource key is the ID which is defined in .resx file.

In XAML :-

<TextBlock Text="{Localize Txt_Language, DefaultValue=Language}" />
<TextBlock Text="{Localize Txt_Welcome, DefaultValue='Welcome to WPF Applicaiton'}" />

CodeBehind :-

LocalizationManager.GetValue("Txt_Language")
LocalizationManager.GetValue("Txt_Welcome")

Sample Output

In English :-


In German :-



No comments: