-
-
Save jakubfijalkowski/0771bfbd26ce68456d3e to your computer and use it in GitHub Desktop.
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; | |
} | |
} |
@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.
How come, no one ever posts the actual source code?
I realize that some people are able to read the class, and implement it. But some still require assistance, as clearly demonstrated in every comment section, on this topic.
How come, no one ever posts the actual source code? I realize that some people are able to read the class, and implement it. But some still require assistance, as clearly demonstrated in every comment section, on this topic.
Years ago I desperately needed this feature and as a result I implemented the most complete version of it in the legendary HandyControls.
and docs:
https://ghost1372.github.io/handycontrol/persianToolkit/dynamicLanguage/
Since I had a bit of trouble figuring out how to actually use the class, heres examples to help others:
XAML:
xmlns:local="clr-namespace:MyNamespace.LocalizationNamespace"
The namespace for xaml is whatever you set it in the class.
Usage:
"{local:Loc <PropertyName>}"
C#:
Properties.Resources.ResourceManager.GetString(nameof(Properties.Resources.<PROPERTYNAME>), Localization.TranslationSource.Instance.CurrentCulture);
And for switching to the actual culture:
C#:
Localization.TranslationSource.Instance.CurrentCulture = CultureInfo.GetCultureInfo("en-example");