Created
May 10, 2015 15:22
-
-
Save jakubfijalkowski/0771bfbd26ce68456d3e to your computer and use it in GitHub Desktop.
WPF l10n with bindings
This file contains 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; | |
} | |
} |
nevaran
commented
Nov 23, 2020
via email
I *just* figure it out, but thanks anyway; going to update the comment to
help others
…On Mon, Nov 23, 2020 at 11:17 AM Jakub Fijałkowski ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@nevaran <https://github.com/nevaran> - you can check the blog post
<https://www.codinginfinity.me/posts/localization-of-a-wpf-app-the-simple-approach/>
that I wrote about this. It explains how it works a little bit better
(although it might not be fully explanatory).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/0771bfbd26ce68456d3e#gistcomment-3537134>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAG6OGGFXLLIISHBXY6BMQ3SRISEPANCNFSM4IZTM5WQ>
.
@jakubfijalkowski, thanks for your code. Is there a way to allow something like this?
{ns:Loc {Binding ViewModelProperty}}
@lszczygielek If my memory is not mistaken, that is possible with some changes to the code (i.e. you need to handle the binding somewhat yourself). Unfortunately, I haven't touched it in years and won't be able to help what needs to be changed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment