Click here to Skip to main content
15,896,201 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 275.3K   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.
    /// </summary>
    public class FormattedLocalizedValue : LocalizedValue
    {
        string _formatString;

        object[] _args;

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

            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            _formatString = formatString;

            _args = args;
        }

        /// <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();

            return string.Format(culture, _formatString, _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