Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / WPF

Advanced WPF Localization

Rate me:
Please Sign up or sign in to vote.
4.85/5 (27 votes)
1 Oct 2017Public Domain6 min read 266.1K   7.1K   74  
Provides a solution for localization of WPF application both in XAML and in code-behind
using System;
using System.Collections.Generic;

using System.Text;
using System.Globalization;

namespace WpfLocalization
{
    /// <summary>
    /// Invaokes a method to obtain a localized value.
    /// </summary>
    public class MethodLocalizedValue : LocalizedValue
    {
        LocalizationCallback _method;

        object _parameter;

        /// <summary>
        /// Initializes a new instance of the <see cref="MethodLocalizedValue"/> class.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="method">The method.</param>
        /// <param name="parameter">The parameter to pass to the method.</param>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="method"/> is null.</exception>
        public MethodLocalizedValue(LocalizedProperty property, LocalizationCallback method, object parameter)
            : base(property)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            _method = method;

            _parameter = parameter;
        }

        /// <summary>
        /// Retrieves the localized value from resources or by other means.
        /// </summary>
        /// <returns>
        /// The localized value.
        /// </returns>
        protected override object GetLocalizedValue()
        {
            var culture = Property.GetCulture();

            var uiCulture = Property.GetUICulture();

            return _method(culture, uiCulture, _parameter);
        }
    }
}

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.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior)
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions