Skip to content

Commit bb3f4ff

Browse files
committed
Minor refactoring
1 parent 825958a commit bb3f4ff

File tree

11 files changed

+90
-84
lines changed

11 files changed

+90
-84
lines changed

Gu.Wpf.Localization.Demo.Controls/Themes/Generic.xaml

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,10 @@
2626
<RowDefinition Height="*" />
2727
</Grid.RowDefinitions>
2828

29-
<!--
30-
<TextBlock Grid.Row="0"
31-
Grid.Column="0"
32-
Text="SwedishOnly" />
33-
<TextBlock Grid.Row="0"
34-
Grid.Column="1"
35-
Text="{l:Static p:Resources.SwedishOnly}" />
36-
37-
<TextBlock Grid.Row="1"
38-
Grid.Column="0"
39-
Text="TranslatedToAll" />
40-
<TextBlock Grid.Row="1"
41-
Grid.Column="1"
42-
Text="{l:Static p:Resources.TranslatedToAll}" />
43-
-->
44-
45-
<TextBlock Grid.Row="2"
29+
<TextBlock Grid.Row="0"
4630
Grid.Column="0"
4731
Text="KeyInControls" />
48-
<TextBlock Grid.Row="2"
32+
<TextBlock Grid.Row="0"
4933
Grid.Column="1"
5034
Text="{l:Static p:Resources.KeyInControls}" />
5135
</Grid>

Gu.Wpf.Localization.Demo.WithResources/App.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Setter Property="Margin" Value="10,0,10,0" />
1010
</Style>
1111

12-
<DataTemplate x:Key="DataTemplate">
12+
<DataTemplate x:Key="DemoTemplate">
1313
<Grid>
1414
<Grid.ColumnDefinitions>
1515
<ColumnDefinition Width="Auto" />

Gu.Wpf.Localization.Demo.WithResources/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
</GroupBox>
6868

6969
<GroupBox BorderBrush="BlueViolet" Header="DataTemplate">
70-
<ContentPresenter ContentTemplate="{StaticResource DataTemplate}" />
70+
<ContentPresenter ContentTemplate="{StaticResource DemoTemplate}" />
7171
</GroupBox>
7272

7373
<GroupBox BorderBrush="HotPink"

Gu.Wpf.Localization.Modern.Demo/Gu.Wpf.Localization.Modern.Demo.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<FileAlignment>512</FileAlignment>
1515
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1616
<RestorePackages>true</RestorePackages>
17+
<SccProjectName>SAK</SccProjectName>
18+
<SccLocalPath>SAK</SccLocalPath>
19+
<SccAuxPath>SAK</SccAuxPath>
20+
<SccProvider>SAK</SccProvider>
1721
</PropertyGroup>
1822
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1923
<DebugSymbols>true</DebugSymbols>

Gu.Wpf.Localization.Tests/Gu.Wpf.Localization.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
</ItemGroup>
6262
<ItemGroup>
6363
<Compile Include="ResourceManagerWrapperTests.cs" />
64-
<Compile Include="TranslateExtensionTests.cs" />
64+
<Compile Include="StaticExtensionTests.cs" />
6565
<Compile Include="Properties\AssemblyInfo.generated.cs" />
6666
<Compile Include="Properties\Resources.Designer.cs">
6767
<AutoGen>True</AutoGen>

Gu.Wpf.Localization.Tests/TranslateExtensionTests.cs renamed to Gu.Wpf.Localization.Tests/StaticExtensionTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
{
33
using System;
44
using System.Windows.Markup;
5-
6-
using Gu.Wpf.Localization;
7-
85
using Moq;
9-
106
using NUnit.Framework;
11-
using StaticExtension = Localization.StaticExtension;
127

13-
public class TranslateExtensionTests
8+
public class StaticExtensionTests
149
{
1510
[Test]
1611
public void ProvideValueSharedDp()
@@ -20,7 +15,7 @@ public void ProvideValueSharedDp()
2015
provideValueTargetMock.SetupGet(x => x.TargetObject).Returns(new SharedDp());
2116
serviceProviderMock.Setup(x => x.GetService(typeof(IProvideValueTarget)))
2217
.Returns(provideValueTargetMock.Object);
23-
var translateExtension = new StaticExtension("meh");
18+
var translateExtension = new Gu.Wpf.Localization.StaticExtension("meh");
2419
var actual = translateExtension.ProvideValue(serviceProviderMock.Object);
2520
Assert.AreEqual("#meh#", actual);
2621
}

Gu.Wpf.Localization.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ EndProject
2525
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gu.Wpf.Localization.Modern.Demo", "Gu.Wpf.Localization.Modern.Demo\Gu.Wpf.Localization.Modern.Demo.csproj", "{D77D4706-D218-4DC6-B117-611AF843FED9}"
2626
EndProject
2727
Global
28+
GlobalSection(TeamFoundationVersionControl) = preSolution
29+
SccNumberOfProjects = 7
30+
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
31+
SccTeamFoundationServer = http://tfshub.sandvik.com:8080/tfs/defaultcollection
32+
SccProjectUniqueName0 = Gu.Wpf.Localization\\Gu.Wpf.Localization.csproj
33+
SccProjectName0 = Gu.Wpf.Localization
34+
SccLocalPath0 = Gu.Wpf.Localization
35+
SccProjectUniqueName1 = Gu.Wpf.Localization.Demo.Controls\\Gu.Wpf.Localization.Demo.Controls.csproj
36+
SccProjectName1 = Gu.Wpf.Localization.Demo.Controls
37+
SccLocalPath1 = Gu.Wpf.Localization.Demo.Controls
38+
SccProjectUniqueName2 = Gu.Wpf.Localization.Tests\\Gu.Wpf.Localization.Tests.csproj
39+
SccProjectName2 = Gu.Wpf.Localization.Tests
40+
SccLocalPath2 = Gu.Wpf.Localization.Tests
41+
SccProjectUniqueName3 = Gu.Wpf.Localization.Demo.WithResources\\Gu.Wpf.Localization.Demo.WithResources.csproj
42+
SccProjectName3 = Gu.Wpf.Localization.Demo.WithResources
43+
SccLocalPath3 = Gu.Wpf.Localization.Demo.WithResources
44+
SccProjectUniqueName4 = Gu.Wpf.Localization.NoResources\\Gu.Wpf.Localization.NoResources.csproj
45+
SccProjectName4 = Gu.Wpf.Localization.NoResources
46+
SccLocalPath4 = Gu.Wpf.Localization.NoResources
47+
SccLocalPath5 = .
48+
SccProjectUniqueName6 = Gu.Wpf.Localization.Modern.Demo\\Gu.Wpf.Localization.Modern.Demo.csproj
49+
SccProjectName6 = Gu.Wpf.Localization.Modern.Demo
50+
SccLocalPath6 = Gu.Wpf.Localization.Modern.Demo
51+
EndGlobalSection
2852
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2953
CreateNugetPackage|Any CPU = CreateNugetPackage|Any CPU
3054
Debug|Any CPU = Debug|Any CPU

Gu.Wpf.Localization/Gu.Wpf.Localization.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<DependentUpon>Resources.resx</DependentUpon>
7777
</Compile>
7878
<Compile Include="StaticExtension.cs" />
79-
<Compile Include="ResourceExtensionConverter.cs" />
79+
<Compile Include="StaticExtensionConverter.cs" />
8080
<Compile Include="ResourceManagerWrapper.cs" />
8181
<Compile Include="Translator.cs" />
8282
</ItemGroup>

Gu.Wpf.Localization/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// Build Number
3131
// Revision
3232
//
33-
[assembly: AssemblyVersion("1.1.0.0")]
34-
[assembly: AssemblyFileVersion("1.1.0.0")]
33+
[assembly: AssemblyVersion("1.2.0.0")]
34+
[assembly: AssemblyFileVersion("1.2.0.0")]
3535
[assembly: NeutralResourcesLanguageAttribute("en")]
3636
[assembly: InternalsVisibleTo("Gu.Wpf.Localization.Tests", AllInternalsVisible = true) ]

Gu.Wpf.Localization/StaticExtension.cs

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// </summary>
1717
[MarkupExtensionReturnType(typeof(string))]
1818
[ContentProperty("Member"), DefaultProperty("Member")]
19-
[TypeConverter(typeof(ResourceExtensionConverter))]
19+
[TypeConverter(typeof(StaticExtensionConverter))]
2020
public class StaticExtension : System.Windows.Markup.StaticExtension
2121
{
2222
private static readonly ConcurrentDictionary<Type, ResourceManagerWrapper> Cache = new ConcurrentDictionary<Type, ResourceManagerWrapper>();
@@ -44,36 +44,25 @@ public override object ProvideValue(IServiceProvider serviceProvider)
4444
try
4545
{
4646
if (this.Member == null)
47-
throw new InvalidOperationException("MarkupExtensionStaticMember");
48-
if (DesignMode.IsDesignMode)
4947
{
50-
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
51-
if (target != null && target.TargetObject is FrameworkElement)
52-
{
53-
var frameworkElement = (FrameworkElement)target.TargetObject;
54-
var value = (XmlLanguage)frameworkElement.GetValue(FrameworkElement.LanguageProperty);
55-
if (value != null)
56-
{
57-
Translator.CurrentCulture = CultureInfo.GetCultureInfo(value.IetfLanguageTag);
58-
}
59-
}
60-
if (target != null && !(target.TargetObject is DependencyObject))
61-
{
62-
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
48+
throw new InvalidOperationException("MarkupExtensionStaticMember");
49+
}
6350

64-
return this;
65-
}
51+
if (DesignMode.IsDesignMode && IsTemplate(serviceProvider))
52+
{
53+
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
54+
return this;
6655
}
6756

68-
Type type = this.MemberType;
57+
var type = this.MemberType;
6958
string name;
7059
if (type != (Type)null)
7160
{
7261
name = this.Member;
7362
}
7463
else
7564
{
76-
int length = this.Member.IndexOf('.');
65+
var length = this.Member.IndexOf('.');
7766
if (length < 0)
7867
{
7968
if (DesignMode.IsDesignMode)
@@ -82,43 +71,26 @@ public override object ProvideValue(IServiceProvider serviceProvider)
8271
}
8372
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
8473
}
85-
else
74+
var qualifiedTypeName = this.Member.Substring(0, length);
75+
if (string.IsNullOrEmpty(qualifiedTypeName))
8676
{
87-
string qualifiedTypeName = this.Member.Substring(0, length);
88-
if (string.IsNullOrEmpty(qualifiedTypeName))
77+
if (DesignMode.IsDesignMode)
8978
{
90-
if (DesignMode.IsDesignMode)
91-
{
92-
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
93-
}
94-
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
79+
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
9580
}
96-
else
81+
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
82+
}
83+
84+
type = this.GetMemberType(serviceProvider, qualifiedTypeName);
85+
86+
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
87+
if (string.IsNullOrEmpty(name))
88+
{
89+
if (DesignMode.IsDesignMode)
9790
{
98-
if (serviceProvider == null)
99-
throw new ArgumentNullException("serviceProvider");
100-
if (_xamlTypeResolver == null)
101-
{
102-
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
103-
}
104-
if (_xamlTypeResolver == null)
105-
{
106-
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
107-
}
108-
else
109-
{
110-
type = _xamlTypeResolver.Resolve(qualifiedTypeName);
111-
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
112-
if (string.IsNullOrEmpty(name))
113-
{
114-
if (DesignMode.IsDesignMode)
115-
{
116-
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
117-
}
118-
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
119-
}
120-
}
91+
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
12192
}
93+
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
12294
}
12395
}
12496

@@ -160,5 +132,32 @@ private static ResourceManagerWrapper GetManager(Type type)
160132
}
161133
return new ResourceManagerWrapper(resourceManager);
162134
}
135+
136+
private static bool IsTemplate(IServiceProvider serviceProvider)
137+
{
138+
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
139+
return target != null &&
140+
!(target.TargetObject is DependencyObject);
141+
}
142+
143+
private Type GetMemberType(IServiceProvider serviceProvider, string qualifiedTypeName)
144+
{
145+
if (serviceProvider == null)
146+
{
147+
throw new ArgumentNullException("serviceProvider");
148+
}
149+
150+
if (_xamlTypeResolver == null)
151+
{
152+
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
153+
}
154+
155+
if (_xamlTypeResolver == null)
156+
{
157+
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
158+
}
159+
160+
return _xamlTypeResolver.Resolve(qualifiedTypeName);
161+
}
163162
}
164163
}

0 commit comments

Comments
 (0)