Click here to Skip to main content
15,888,325 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 269.2K   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;

namespace WpfLocalization
{
    /// <summary>
    /// Formats a list of objects to produce a string value. The formatting string is retrieved from
    /// resources.
    /// </summary>
    public class ResourceFormattedLocalizedValue : ResourceLocalizedValue
    {
        object[] _args;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormattedLocalizedValue"/> class.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="resourceKey">The resource key.</param>
        /// <param name="args">The args.</param>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="resourceKey"/> is null or empty.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="args"/> is null.</exception>
        public ResourceFormattedLocalizedValue(
            LocalizedProperty property,
            string resourceKey,
            params object[] args
            )
            : base(property, resourceKey)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            _args = args;
        }

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

            if (result == null || false == (result is string))
            {
                return null;
            }

            return string.Format(Property.GetCulture(), (string)result, _args);
        }
    }
}

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