Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Nov 6, 2014
1 parent 825958a commit bb3f4ff
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 84 deletions.
20 changes: 2 additions & 18 deletions Gu.Wpf.Localization.Demo.Controls/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,10 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<!--
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="SwedishOnly" />
<TextBlock Grid.Row="0"
Grid.Column="1"
Text="{l:Static p:Resources.SwedishOnly}" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="TranslatedToAll" />
<TextBlock Grid.Row="1"
Grid.Column="1"
Text="{l:Static p:Resources.TranslatedToAll}" />
-->

<TextBlock Grid.Row="2"
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="KeyInControls" />
<TextBlock Grid.Row="2"
<TextBlock Grid.Row="0"
Grid.Column="1"
Text="{l:Static p:Resources.KeyInControls}" />
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization.Demo.WithResources/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Setter Property="Margin" Value="10,0,10,0" />
</Style>

<DataTemplate x:Key="DataTemplate">
<DataTemplate x:Key="DemoTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization.Demo.WithResources/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</GroupBox>

<GroupBox BorderBrush="BlueViolet" Header="DataTemplate">
<ContentPresenter ContentTemplate="{StaticResource DataTemplate}" />
<ContentPresenter ContentTemplate="{StaticResource DemoTemplate}" />
</GroupBox>

<GroupBox BorderBrush="HotPink"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization.Tests/Gu.Wpf.Localization.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ResourceManagerWrapperTests.cs" />
<Compile Include="TranslateExtensionTests.cs" />
<Compile Include="StaticExtensionTests.cs" />
<Compile Include="Properties\AssemblyInfo.generated.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
{
using System;
using System.Windows.Markup;

using Gu.Wpf.Localization;

using Moq;

using NUnit.Framework;
using StaticExtension = Localization.StaticExtension;

public class TranslateExtensionTests
public class StaticExtensionTests
{
[Test]
public void ProvideValueSharedDp()
Expand All @@ -20,7 +15,7 @@ public void ProvideValueSharedDp()
provideValueTargetMock.SetupGet(x => x.TargetObject).Returns(new SharedDp());
serviceProviderMock.Setup(x => x.GetService(typeof(IProvideValueTarget)))
.Returns(provideValueTargetMock.Object);
var translateExtension = new StaticExtension("meh");
var translateExtension = new Gu.Wpf.Localization.StaticExtension("meh");
var actual = translateExtension.ProvideValue(serviceProviderMock.Object);
Assert.AreEqual("#meh#", actual);
}
Expand Down
24 changes: 24 additions & 0 deletions Gu.Wpf.Localization.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ EndProject
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}"
EndProject
Global
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 7
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
SccTeamFoundationServer = http://tfshub.sandvik.com:8080/tfs/defaultcollection
SccProjectUniqueName0 = Gu.Wpf.Localization\\Gu.Wpf.Localization.csproj
SccProjectName0 = Gu.Wpf.Localization
SccLocalPath0 = Gu.Wpf.Localization
SccProjectUniqueName1 = Gu.Wpf.Localization.Demo.Controls\\Gu.Wpf.Localization.Demo.Controls.csproj
SccProjectName1 = Gu.Wpf.Localization.Demo.Controls
SccLocalPath1 = Gu.Wpf.Localization.Demo.Controls
SccProjectUniqueName2 = Gu.Wpf.Localization.Tests\\Gu.Wpf.Localization.Tests.csproj
SccProjectName2 = Gu.Wpf.Localization.Tests
SccLocalPath2 = Gu.Wpf.Localization.Tests
SccProjectUniqueName3 = Gu.Wpf.Localization.Demo.WithResources\\Gu.Wpf.Localization.Demo.WithResources.csproj
SccProjectName3 = Gu.Wpf.Localization.Demo.WithResources
SccLocalPath3 = Gu.Wpf.Localization.Demo.WithResources
SccProjectUniqueName4 = Gu.Wpf.Localization.NoResources\\Gu.Wpf.Localization.NoResources.csproj
SccProjectName4 = Gu.Wpf.Localization.NoResources
SccLocalPath4 = Gu.Wpf.Localization.NoResources
SccLocalPath5 = .
SccProjectUniqueName6 = Gu.Wpf.Localization.Modern.Demo\\Gu.Wpf.Localization.Modern.Demo.csproj
SccProjectName6 = Gu.Wpf.Localization.Modern.Demo
SccLocalPath6 = Gu.Wpf.Localization.Modern.Demo
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CreateNugetPackage|Any CPU = CreateNugetPackage|Any CPU
Debug|Any CPU = Debug|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization/Gu.Wpf.Localization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="StaticExtension.cs" />
<Compile Include="ResourceExtensionConverter.cs" />
<Compile Include="StaticExtensionConverter.cs" />
<Compile Include="ResourceManagerWrapper.cs" />
<Compile Include="Translator.cs" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Gu.Wpf.Localization/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: NeutralResourcesLanguageAttribute("en")]
[assembly: InternalsVisibleTo("Gu.Wpf.Localization.Tests", AllInternalsVisible = true) ]
103 changes: 51 additions & 52 deletions Gu.Wpf.Localization/StaticExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// </summary>
[MarkupExtensionReturnType(typeof(string))]
[ContentProperty("Member"), DefaultProperty("Member")]
[TypeConverter(typeof(ResourceExtensionConverter))]
[TypeConverter(typeof(StaticExtensionConverter))]
public class StaticExtension : System.Windows.Markup.StaticExtension
{
private static readonly ConcurrentDictionary<Type, ResourceManagerWrapper> Cache = new ConcurrentDictionary<Type, ResourceManagerWrapper>();
Expand Down Expand Up @@ -44,36 +44,25 @@ public override object ProvideValue(IServiceProvider serviceProvider)
try
{
if (this.Member == null)
throw new InvalidOperationException("MarkupExtensionStaticMember");
if (DesignMode.IsDesignMode)
{
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (target != null && target.TargetObject is FrameworkElement)
{
var frameworkElement = (FrameworkElement)target.TargetObject;
var value = (XmlLanguage)frameworkElement.GetValue(FrameworkElement.LanguageProperty);
if (value != null)
{
Translator.CurrentCulture = CultureInfo.GetCultureInfo(value.IetfLanguageTag);
}
}
if (target != null && !(target.TargetObject is DependencyObject))
{
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
throw new InvalidOperationException("MarkupExtensionStaticMember");
}

return this;
}
if (DesignMode.IsDesignMode && IsTemplate(serviceProvider))
{
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
return this;
}

Type type = this.MemberType;
var type = this.MemberType;
string name;
if (type != (Type)null)
{
name = this.Member;
}
else
{
int length = this.Member.IndexOf('.');
var length = this.Member.IndexOf('.');
if (length < 0)
{
if (DesignMode.IsDesignMode)
Expand All @@ -82,43 +71,26 @@ public override object ProvideValue(IServiceProvider serviceProvider)
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}
else
var qualifiedTypeName = this.Member.Substring(0, length);
if (string.IsNullOrEmpty(qualifiedTypeName))
{
string qualifiedTypeName = this.Member.Substring(0, length);
if (string.IsNullOrEmpty(qualifiedTypeName))
if (DesignMode.IsDesignMode)
{
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
else
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}

type = this.GetMemberType(serviceProvider, qualifiedTypeName);

name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
if (string.IsNullOrEmpty(name))
{
if (DesignMode.IsDesignMode)
{
if (serviceProvider == null)
throw new ArgumentNullException("serviceProvider");
if (_xamlTypeResolver == null)
{
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
}
if (_xamlTypeResolver == null)
{
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
}
else
{
type = _xamlTypeResolver.Resolve(qualifiedTypeName);
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
if (string.IsNullOrEmpty(name))
{
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}
}
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}
}

Expand Down Expand Up @@ -160,5 +132,32 @@ private static ResourceManagerWrapper GetManager(Type type)
}
return new ResourceManagerWrapper(resourceManager);
}

private static bool IsTemplate(IServiceProvider serviceProvider)
{
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
return target != null &&
!(target.TargetObject is DependencyObject);
}

private Type GetMemberType(IServiceProvider serviceProvider, string qualifiedTypeName)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}

if (_xamlTypeResolver == null)
{
_xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
}

if (_xamlTypeResolver == null)
{
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
}

return _xamlTypeResolver.Resolve(qualifiedTypeName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Gu.Wpf.Localization
using System.Reflection;
using System.Security;

internal class ResourceExtensionConverter : TypeConverter
internal class StaticExtensionConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
Expand Down

0 comments on commit bb3f4ff

Please sign in to comment.