Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/Localize.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ the extensions support to add the Binding as Constructor. Because of WPF restric
{lex:Loc {Binding Path=Foo}}
```

or via content

```xaml
<lex:Loc>
<Binding Path="Foo" "/>
</lex:Loc>
```

Multibinding only possible via content please use the following syntax:

```xaml
<lex:Loc>
<MultiBinding Converter="FooAndBarConverter">
<Binding Path="Foo" "/>
<Binding Path="Bar" "/>
</MultiBinding>
</lex:Loc>
```

This gives you the full flexibility because the Markupextensions is the pure Binding and this supports Converters, StringFormat, ...

### Enums
Expand Down Expand Up @@ -89,6 +108,6 @@ For translation of complex texts there some value should be in the text like thi

>You have selected 10 car(s).

TWe have introduces a StringFormatConverter see [ValueConverters](ValueConverters.md) with automatic using of [smartFormat](https://github.com/axuno/SmartFormat) if this is available.
We have introduced a StringFormatConverter see [ValueConverters](ValueConverters.md) with automatic using of [smartFormat](https://github.com/axuno/SmartFormat) if this is available.
A detailed explanation how to use can be found [here](GapText.md).

4 changes: 2 additions & 2 deletions src/Deprecated/Engine/LocBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public object Source
#endregion

#region Target LocExtension
private LocExtension _target;
private LocBaseExtension _target;
/// <summary>
/// The target extension.
/// </summary>
public LocExtension Target
public LocBaseExtension Target
{
get => _target;
set
Expand Down
6 changes: 3 additions & 3 deletions src/Deprecated/Engine/LocProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace WPFLocalizeExtension.Deprecated.Engine
public class LocProxy : FrameworkElement
{
/// <summary>
/// Our own <see cref="LocExtension"/> instance.
/// Our own <see cref="LocBaseExtension"/> instance.
/// </summary>
private LocExtension _ext;
private LocBaseExtension _ext;

#region Source property
/// <summary>
Expand Down Expand Up @@ -133,7 +133,7 @@ private static void PropertiesChanged(DependencyObject d, DependencyPropertyChan

if (proxy._ext == null)
{
proxy._ext = new LocExtension { Key = key };
proxy._ext = new LocBaseExtension { Key = key };
proxy._ext.SetBinding(proxy, proxy.GetType().GetProperty("Result"));
}
else
Expand Down
12 changes: 6 additions & 6 deletions src/Deprecated/Extensions/Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace WPFLocalizeExtension.Deprecated.Extensions
/// <inheritdoc/>
[Obsolete("LocBrushExtension is deprecated and will be removed in version 4.0, please use lex:Loc instead and see documentation", false)]
[MarkupExtensionReturnType(typeof(System.Windows.Media.Brush))]
public class LocBrushExtension : LocExtension
public class LocBrushExtension : LocBaseExtension
{
/// <inheritdoc/>
public LocBrushExtension()
Expand All @@ -33,7 +33,7 @@ public LocBrushExtension(string key) : base(key) { }
/// <inheritdoc/>
[Obsolete("LocDoubleExtension is deprecated and will be removed in version 4.0, please use lex:Loc instead and see documentation", false)]
[MarkupExtensionReturnType(typeof(double))]
public class LocDoubleExtension : LocExtension
public class LocDoubleExtension : LocBaseExtension
{
/// <inheritdoc/>
public LocDoubleExtension()
Expand All @@ -46,7 +46,7 @@ public LocDoubleExtension(string key) : base(key) { }
/// <inheritdoc/>
[MarkupExtensionReturnType(typeof(System.Windows.FlowDirection))]
[Obsolete("LocFlowDirectionExtension is deprecated and will be removed in version 4.0, please use lex:Loc instead and see documentation", false)]
public class LocFlowDirectionExtension : LocExtension
public class LocFlowDirectionExtension : LocBaseExtension
{
/// <inheritdoc/>
public LocFlowDirectionExtension()
Expand All @@ -59,7 +59,7 @@ public LocFlowDirectionExtension(string key) : base(key) { }
/// <inheritdoc/>
[MarkupExtensionReturnType(typeof(System.Windows.Media.Imaging.BitmapSource))]
[Obsolete("LocImageExtension is deprecated and will be removed in version 4.0, please use lex:Loc instead and see documentation", false)]
public class LocImageExtension : LocExtension
public class LocImageExtension : LocBaseExtension
{
/// <inheritdoc/>
public LocImageExtension()
Expand All @@ -72,7 +72,7 @@ public LocImageExtension(string key) : base(key) { }
/// <inheritdoc/>
[MarkupExtensionReturnType(typeof(string))]
[Obsolete("LocTextExtension is deprecated and will be removed in version 4.0, please use lex:Loc instead and see documentation", false)]
public class LocTextExtension : LocExtension
public class LocTextExtension : LocBaseExtension
{
#region Constructors
/// <inheritdoc/>
Expand Down Expand Up @@ -323,7 +323,7 @@ protected override string FormatText(string target)
/// <inheritdoc/>
[MarkupExtensionReturnType(typeof(System.Windows.Thickness))]
[Obsolete("LocThicknessExtension is deprecated and will be removed in version 4.0, please use lex:Loc instead and see documentation", false)]
public class LocThicknessExtension : LocExtension
public class LocThicknessExtension : LocBaseExtension
{
/// <inheritdoc/>
public LocThicknessExtension()
Expand Down
23 changes: 23 additions & 0 deletions src/Deprecated/Extensions/LocExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#region Copyright information
// <copyright file="LocExtension.cs">
// Licensed under Microsoft Public License (Ms-PL)
// https://github.com/XAMLMarkupExtensions/WPFLocalizationExtension/blob/master/LICENSE
// </copyright>
// <author>Konrad Mattheis</author>
#endregion

namespace WPFLocalizeExtension.Deprecated.Extensions
{
using System.Windows.Markup;

/// <inheritdoc/>>
[ContentProperty("ResourceIdentifierKey")]
public class LocExtension : WPFLocalizeExtension.Extensions.LocBaseExtension
{
/// <inheritdoc/>>
public LocExtension() : base() { }

/// <inheritdoc/>>
public LocExtension(object key) : base(key) { }
}
}
6 changes: 3 additions & 3 deletions src/Engine/EnumRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace WPFLocalizeExtension.Engine
public class EnumRun : Run
{
/// <summary>
/// Our own <see cref="LocExtension"/> instance.
/// Our own <see cref="LocBaseExtension"/> instance.
/// </summary>
private LocExtension _ext;
private LocBaseExtension _ext;

#region EnumValue property
/// <summary>
Expand Down Expand Up @@ -116,7 +116,7 @@ private static void PropertiesChanged(DependencyObject d, DependencyPropertyChan

if (run._ext == null)
{
run._ext = new LocExtension { Key = key };
run._ext = new LocBaseExtension { Key = key };
run._ext.SetBinding(run, run.GetType().GetProperty("Text"));
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/LocalizeDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ private void AvailableCulturesCollectionChanged(object sender, NotifyCollectionC
/// </summary>
~LocalizeDictionary()
{
LocExtension.ClearResourceBuffer();
LocBaseExtension.ClearResourceBuffer();
FELoc.ClearResourceBuffer();
BLoc.ClearResourceBuffer();
}
Expand Down
52 changes: 35 additions & 17 deletions src/Extensions/LocExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace WPFLocalizeExtension.Extensions
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using WPFLocalizeExtension.Deprecated.Engine;
using WPFLocalizeExtension.Engine;
using WPFLocalizeExtension.Providers;
using WPFLocalizeExtension.TypeConverters;
Expand All @@ -33,8 +34,7 @@ namespace WPFLocalizeExtension.Extensions
/// <summary>
/// A generic localization extension.
/// </summary>
[ContentProperty("ResourceIdentifierKey")]
public class LocExtension : NestedMarkupExtension, INotifyPropertyChanged, IDictionaryEventListener, IDisposable
public abstract class LocBaseExtension : NestedMarkupExtension, INotifyPropertyChanged, IDictionaryEventListener, IDisposable
{
#region PropertyChanged Logic
/// <summary>
Expand Down Expand Up @@ -68,7 +68,7 @@ internal void OnNotifyPropertyChanged(string property)
/// <summary>
/// Holds the Binding to get the key
/// </summary>
private Binding _binding;
private BindingBase _binding;

/// <summary>
/// the Name of the cached dynamic generated DependencyProperties
Expand Down Expand Up @@ -150,9 +150,9 @@ internal static void SafeRemoveItemFromResourceBuffer(string key)
/// <param name="property">The target property name.</param>
/// <param name="propertyIndex">The index in the property (if applicable).</param>
/// <returns>The bound extension or null, if not available.</returns>
public static LocExtension GetBoundExtension(object target, string property, int propertyIndex = -1)
public static LocBaseExtension GetBoundExtension(object target, string property, int propertyIndex = -1)
{
foreach (var ext in LocalizeDictionary.DictionaryEvent.EnumerateListeners<LocExtension>())
foreach (var ext in LocalizeDictionary.DictionaryEvent.EnumerateListeners<LocBaseExtension>())
{
var ep = ext._lastEndpoint;

Expand Down Expand Up @@ -200,7 +200,7 @@ private static string GetPropertyName(object property)

#region Properties
/// <summary>
/// Gets or sets the Key to a .resx object
/// Gets or sets the Key that identifies a resource (Assembly:Dictionary:Key)
/// </summary>
public string Key
{
Expand All @@ -217,6 +217,24 @@ public string Key
}
}

/// <summary>
/// Gets or sets the Binding that delivers the Key that identifies a resource (Assembly:Dictionary:Key)
/// </summary>
public BindingBase KeyBinding
{
get => _binding;
set
{
if (_binding != value)
{
_binding = value;
UpdateNewValue();

OnNotifyPropertyChanged(nameof(Key));
}
}
}

/// <summary>
/// Gets or sets the custom value converter.
/// </summary>
Expand Down Expand Up @@ -268,9 +286,9 @@ public object ResourceIdentifierKey

#region Constructors & Dispose
/// <summary>
/// Initializes a new instance of the <see cref="LocExtension"/> class.
/// Initializes a new instance of the <see cref="LocBaseExtension"/> class.
/// </summary>
public LocExtension()
public LocBaseExtension()
{
// Register this extension as an event listener on the first target.
OnFirstTarget = () =>
Expand All @@ -280,10 +298,10 @@ public LocExtension()
}

/// <summary>
/// Initializes a new instance of the <see cref="LocExtension"/> class.
/// Initializes a new instance of the <see cref="LocBaseExtension"/> class.
/// </summary>
/// <param name="key">The resource identifier.</param>
public LocExtension(object key)
public LocBaseExtension(object key)
: this()
{
if (key is TemplateBindingExpression tbe)
Expand All @@ -298,7 +316,7 @@ public LocExtension(object key)
key = newBinding;
}

if (key is Binding binding)
if (key is BindingBase binding)
_binding = binding;
else
Key = key?.ToString();
Expand All @@ -317,7 +335,7 @@ public LocExtension(object key)
/// <summary>
/// The finalizer.
/// </summary>
~LocExtension()
~LocBaseExtension()
{
Dispose();
}
Expand Down Expand Up @@ -476,13 +494,13 @@ public override object FormatOutput(TargetInfo endPoint, TargetInfo info)
{
MethodInfo mi = typeof(DependencyProperty).GetMethod("FromName", BindingFlags.Static | BindingFlags.NonPublic);

cacheDPThis = mi.Invoke(null, new object[] { name, typeof(LocExtension) }) as DependencyProperty
?? DependencyProperty.RegisterAttached(name, typeof(NestedMarkupExtension), typeof(LocExtension),
cacheDPThis = mi.Invoke(null, new object[] { name, typeof(LocBaseExtension) }) as DependencyProperty
?? DependencyProperty.RegisterAttached(name, typeof(NestedMarkupExtension), typeof(LocBaseExtension),
new PropertyMetadata(null));

cacheDPKey = mi.Invoke(null, new object[] { name + ".Key", typeof(LocExtension) }) as DependencyProperty
?? DependencyProperty.RegisterAttached(name + ".Key", typeof(string), typeof(LocExtension),
new PropertyMetadata("", (d, e) => { (d?.GetValue(cacheDPThis) as LocExtension)?.UpdateNewValue(); }));
cacheDPKey = mi.Invoke(null, new object[] { name + ".Key", typeof(LocBaseExtension) }) as DependencyProperty
?? DependencyProperty.RegisterAttached(name + ".Key", typeof(string), typeof(LocBaseExtension),
new PropertyMetadata("", (d, e) => { (d?.GetValue(cacheDPThis) as LocBaseExtension)?.UpdateNewValue(); }));
cacheDPName = name;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ValueConverters/TranslateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
culture = LocalizeDictionary.Instance.SpecificCulture;
var _key = value.ToString();

var result = LocExtension.GetLocalizedValue(targetType, _key, culture, null);
var result = LocBaseExtension.GetLocalizedValue(targetType, _key, culture, null);

if (result == null)
{
Expand Down
13 changes: 13 additions & 0 deletions tests/HelloWorldWPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:local="clr-namespace:HalloWeltWPF"
d:DataContext="{d:DesignInstance Type=local:TestVM, IsDesignTimeCreatable=True}"
mc:Ignorable="d"

lex:LocalizeDictionary.DesignCulture="en"
lex:LocalizeDictionary.IncludeInvariantCulture="False"
lex:ResxLocalizationProvider.DefaultAssembly="HelloWorldWPF"
Expand Down Expand Up @@ -74,6 +75,18 @@
</Button.Template>
</Button>
<TextBlock Text="{lex:Loc {Binding tenum, StringFormat=TestEnum_{0}}}"></TextBlock>
<TextBlock>
<TextBlock.Text>
<lex:Loc>
en
<!--<lex:Loc.KeyBinding>-->
<!--<MultiBinding StringFormat="{}{0}">
<Binding Path="tenum" Converter="{lex:PrependTypeConverter}" ConverterParameter="__"/>
</MultiBinding>-->
<!--</lex:Loc.KeyBinding>-->
</lex:Loc>
</TextBlock.Text>
</TextBlock>
<TextBlock Text="{lex:Loc {Binding tenum, Converter={lex:PrependTypeConverter}, ConverterParameter=__}}"></TextBlock>
<Button Name="BindeTestButton" Content="Press to toggle binded property" Margin="5" Click="BindeTestButton_Click" ></Button>
<TextBlock Name="MyLabel2" FontSize="20" Text="{lex:Loc PresentationCore:ExceptionStringTable:DeleteText}" HorizontalAlignment="Center" />
Expand Down