Forked from jakubfijalkowski/gist:0771bfbd26ce68456d3e
Created
June 13, 2018 12:50
-
-
Save wmalgadey/c017a475d5b08bbda54896e43aa30c94 to your computer and use it in GitHub Desktop.
WPF l10n with bindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment