Skip to content

Commit 4e2bcbf

Browse files
committed
#354: Added FallbackBehavior support
1 parent 1fdaaad commit 4e2bcbf

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

src/Engine/FallbackBehavior.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace WPFLocalizeExtension.Engine
2+
{
3+
/// <summary>
4+
/// Behavior when key is not found at the localization provider.
5+
/// </summary>
6+
public enum FallbackBehavior
7+
{
8+
/// <summary>
9+
/// Display "Key: {key}" string.
10+
/// </summary>
11+
Default,
12+
13+
/// <summary>
14+
/// Display key string itself.
15+
/// </summary>
16+
Key,
17+
18+
/// <summary>
19+
/// Display an empty string.
20+
/// </summary>
21+
EmptyString
22+
}
23+
}

src/Extensions/LocExtension.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ public object ResourceIdentifierKey
264264
get => _key ?? "(null)";
265265
set => _key = value.ToString();
266266
}
267+
268+
/// <summary>
269+
/// Behavior when key is not found at the localization provider.
270+
/// </summary>
271+
public FallbackBehavior FallbackBehavior { get; set; }
272+
267273
#endregion
268274

269275
#region Constructors
@@ -606,7 +612,23 @@ public override object FormatOutput(TargetInfo endPoint, TargetInfo info)
606612
if (missingKeyEventResult.MissingKeyResult != null)
607613
result = missingKeyEventResult.MissingKeyResult;
608614
else
609-
result = "Key: " + _key;
615+
{
616+
switch (FallbackBehavior)
617+
{
618+
case FallbackBehavior.Key:
619+
result = _key;
620+
break;
621+
622+
case FallbackBehavior.EmptyString:
623+
result = string.Empty;
624+
break;
625+
626+
case FallbackBehavior.Default:
627+
default:
628+
result = "Key: " + _key;
629+
break;
630+
}
631+
}
610632
}
611633
}
612634
}

tests/HelloWorldWPF/MainWindow.xaml

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
<TextBlock Text="{lex:Loc {Binding tenum, Converter={lex:PrependTypeConverter}, ConverterParameter=__}}"></TextBlock>
7878
<Button Name="BindeTestButton" Content="Press to toggle binded property" Margin="5" Click="BindeTestButton_Click" ></Button>
7979
<TextBlock Name="MyLabel2" FontSize="20" Text="{lex:Loc PresentationCore:ExceptionStringTable:DeleteText}" HorizontalAlignment="Center" />
80+
<StackPanel Margin="20,0,0,0">
81+
<TextBlock FontSize="20" Text="{lex:Loc UndefinedKey, FallbackBehavior=Default}" />
82+
<TextBlock FontSize="20" Text="{lex:Loc UndefinedKey, FallbackBehavior=Key}" />
83+
<TextBlock FontSize="20" Text="{lex:Loc UndefinedKey, FallbackBehavior=EmptyString}" />
84+
</StackPanel>
8085
<StackPanel Orientation="Horizontal">
8186
<Button Content="{lex:Loc de}" Margin="5" CommandParameter="de" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=SetCultureCommand}" />
8287
<Button Content="{lex:Loc en}" Margin="5" CommandParameter="en" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=SetCultureCommand}" />

tests/HelloWorldWPF/MainWindow.xaml.cs

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public MainWindow()
4848

4949
private void Instance_MissingKeyEvent(object sender, MissingKeyEventArgs e)
5050
{
51+
// Test of FallbackBehavior.
52+
if (e.Key == "UndefinedKey")
53+
return;
54+
5155
e.MissingKeyResult = "Hello World";
5256
}
5357

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
namespace WPFLocalizeExtension.UnitTests.ValueConvertersTests
2+
{
3+
#region Usings
4+
using Xunit;
5+
using XAMLMarkupExtensions.Base;
6+
using WPFLocalizeExtension.Engine;
7+
using WPFLocalizeExtension.Extensions;
8+
#endregion
9+
10+
/// <summary>
11+
/// Tests for <see cref="LocExtension" />.
12+
/// </summary>
13+
public class LocExtensionTests
14+
{
15+
#region FallbackBehavior
16+
17+
private const string MISSING_KEY_RESULT = nameof(MISSING_KEY_RESULT);
18+
19+
/// <summary>
20+
/// Check different behaviors when key is not found at resource provider.
21+
/// </summary>
22+
[Theory]
23+
[InlineData(FallbackBehavior.Default, "Key: abacaba")]
24+
[InlineData(FallbackBehavior.Key, "abacaba")]
25+
[InlineData(FallbackBehavior.EmptyString, "")]
26+
public void FormatOutput_SpecifiedFallbackBehavior_SpecifiedOutput(FallbackBehavior fallbackBehavior, string expectedValue)
27+
{
28+
// ARRANGE.
29+
const string key = "abacaba";
30+
31+
var locExtension = new LocExtension(key);
32+
locExtension.FallbackBehavior = fallbackBehavior;
33+
var endPoint = new TargetInfo(null, null, typeof(string), -1);
34+
var info = new TargetInfo(null, null, typeof(string), -1);
35+
36+
// ACT.
37+
var resultValue = locExtension.FormatOutput(endPoint, info);
38+
39+
// ASSERT.
40+
Assert.Equal(expectedValue, resultValue);
41+
}
42+
43+
/// <summary>
44+
/// Check if <see cref="LocalizeDictionary.MissingKeyEvent" /> specifies missing value, then <see cref="FallbackBehavior" /> is not used.
45+
/// </summary>
46+
[Theory]
47+
[InlineData(FallbackBehavior.Default)]
48+
[InlineData(FallbackBehavior.Key)]
49+
[InlineData(FallbackBehavior.EmptyString)]
50+
public void FormatOutput_MissingKeyEventHandling_FallbackBehaviorNotUsed(FallbackBehavior fallbackBehavior)
51+
{
52+
// ARRANGE.
53+
const string key = "abacaba";
54+
55+
var locExtension = new LocExtension(key);
56+
locExtension.FallbackBehavior = fallbackBehavior;
57+
var endPoint = new TargetInfo(null, null, typeof(string), -1);
58+
var info = new TargetInfo(null, null, typeof(string), -1);
59+
60+
// ACT.
61+
object resultValue;
62+
LocalizeDictionary.Instance.MissingKeyEvent += OnMissingKeyEvent;
63+
64+
try
65+
{
66+
resultValue = locExtension.FormatOutput(endPoint, info);
67+
}
68+
finally
69+
{
70+
LocalizeDictionary.Instance.MissingKeyEvent -= OnMissingKeyEvent;
71+
}
72+
73+
// ASSERT.
74+
Assert.Equal(MISSING_KEY_RESULT, resultValue);
75+
}
76+
77+
/// <summary>
78+
/// Handle <see cref="LocalizeDictionary.MissingKeyEvent" />.
79+
/// </summary>
80+
private static void OnMissingKeyEvent(object sender, MissingKeyEventArgs e)
81+
{
82+
e.MissingKeyResult = MISSING_KEY_RESULT;
83+
}
84+
85+
#endregion
86+
}
87+
}

0 commit comments

Comments
 (0)