OS: WP8
I'm trying to format a string which is the result of a converter taking a binding. All of it works except for the localization of the string format data, which I haven't the foggiest idea how to incorporate. Microsoft's documentation hasn't been all that clear on this and I'm wondering if someone could point me in the right direction.
<TextBlock Text="{Binding Date, StringFormat='Received On: {0}', ConverterParameter=shortdatewithyear, Converter={StaticResource DateTimeToTimeConvert}}"/>
It doesn't see like a totally off the wall thing to want to do.
Thanks!
-Cord
In your particular case I'd pull the string out of the resource file in the converter, then the .Net provided localisation can work. This is probably more important where you are building strings and the order you build it might change in different languages.
You create a resource file in the standard way - "MyResource.resx" to store the strings for your default language and then you can create a localised version of that called "MyResource.Fr-fr.resx" (if you were doing French). This will be automatically loaded and searched in the first instance for a string. If it doesn't find one the code will pull out the string from the default resource file. This way you don't have to translate everything - useful for US/GB spelling differences.
In general once you have this you can have localised strings in your XAML
Add a Localize class:
public class Localize : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChange(String name)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
#region 'Public Properties'
//Declarations
private static Resources.MyResources _myResources = new Resources.MyResources();
public Resources.MyResources myResources
{
get { return _myResources; }
set { NotifyChange("MyResources"); }
}
#endregion
}
Then in your XAML add this to your user control's resources:
<local:Localize x:Key="myResource"
xmlns:local="clr-namespace:MyProject" />
Then you can use it:
<TextBlock Text="{Binding myResource.MyString, Source={StaticResource myResource}}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With