Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WPF l10n with bindings
using System.ComponentModel;
using System.Globalization;
using System.Resources;
using System.Windows.Data;
public class TranslationSource
: INotifyPropertyChanged
{
private static readonly TranslationSource instance = new TranslationSource();
public static TranslationSource Instance
{
get { return instance; }
}
private readonly ResourceManager resManager = Properties.Resources.ResourceManager;
private CultureInfo currentCulture = null;
public string this[string key]
{
get { return this.resManager.GetString(key, this.currentCulture); }
}
public CultureInfo CurrentCulture
{
get { return this.currentCulture; }
set
{
if (this.currentCulture != value)
{
this.currentCulture = value;
var @event = this.PropertyChanged;
if (@event != null)
{
@event.Invoke(this, new PropertyChangedEventArgs(string.Empty));
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class LocExtension
: Binding
{
public LocExtension(string name)
: base("[" + name + "]")
{
this.Mode = BindingMode.OneWay;
this.Source = TranslationSource.Instance;
}
}
@meterien

This comment has been minimized.

Copy link

meterien commented Feb 9, 2017

Thanks!!! This is very useful and work out of the box.

@Logistoner

This comment has been minimized.

Copy link

Logistoner commented Aug 11, 2017

Very nice work indeed. This is in my opinion the most elegant and straightforward approach for multilanguage WPF applications.
I've used this a lot in the recent months and the only thing I was missing was the ability to easily concatenate multiple localized resources into a single string. I was looking into the WPF multibinding feature, but eventually realized this is easily implemented with a slight change in your code.

Usage:
<Label Content="{l:Loc Add_Customer}"/>

...this gives localized resources for keys "Add" and "Customer" separated by a space => "Add Customer" for the default locale.

Code:

private const char KeySeparator = '_';

public string this[string key]
        {
            get
            {
                if (key.IndexOf(KeySeparator)==-1)
                {
                    var res = ResourceManager?.GetString(key, _currentCulture);
                    return !string.IsNullOrWhiteSpace(res) ? res : $"[{key}]";
                }
                else
                {
                    var keys = key.Split(KeySeparator);
                    var results = new List<string>();
                    foreach (var k in keys)
                    {
                        var res = ResourceManager?.GetString(k, _currentCulture);
                        results.Add(!string.IsNullOrWhiteSpace(res) ? res : $"[{k}]");
                    }
                    return string.Join(" ", results);
                }
            }
        }
@ModernRonin

This comment has been minimized.

Copy link

ModernRonin commented May 30, 2018

works perfectly - super helpful! thanks a lot!

@lpnam0201

This comment has been minimized.

Copy link

lpnam0201 commented Jun 11, 2018

Brilliant solution. Thanks a lot.

@eisenreich

This comment has been minimized.

Copy link

eisenreich commented Aug 23, 2018

Very, very, very interesting! This showed me a complete different angle of view how to work with bindings! Thanks! 👍

@ghostelet

This comment has been minimized.

Copy link

ghostelet commented Sep 30, 2018

That looks nice. Do you have any copyright/licensing on this code?

@jakubfijalkowski

This comment has been minimized.

Copy link
Owner Author

jakubfijalkowski commented Dec 13, 2018

Wow, I didn't expect that much feedback here (and GitHub decided not to notify me - damn you, settings!). It's awesome that you find this useful. Thanks a lot!

@ghostelet - feel free to use it in whatever form you like (w/o copyright notice). You may treat it as public domain.

@Jinjinov

This comment has been minimized.

Copy link

Jinjinov commented Dec 28, 2018

This is an excellent solution!

Thank you for this and for all your help making a version that works with multiple resource managers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.