Skip to content

Commit

Permalink
Working ok now 1.1..0
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Nov 5, 2014
1 parent 2045efc commit 2f1b119
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 113 deletions.
47 changes: 24 additions & 23 deletions Gu.Wpf.Localization.Demo.WithResources/Errors.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<UserControl x:Class="Gu.Wpf.Localization.Demo.WithResources.Errors"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:l="clr-namespace:Gu.Wpf.Localization;assembly=Gu.Wpf.Localization"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:p="clr-namespace:Gu.Wpf.Localization.Demo.WithResources.Properties"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<Grid>
<Grid.ColumnDefinitions>
Expand All @@ -24,40 +25,40 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="MissingKey" />
Grid.Column="0"
Text="MissingKey" />
<TextBlock Grid.Row="0"
Grid.Column="1"
Text="{l:Resource p:Resources.MissingKey}" />
Grid.Column="1"
Text="{l:Resource p:Resources.MissingKey}" />

<TextBlock Grid.Row="1"
Grid.Column="0"
Text="SwedishAndNeutral" />
Grid.Column="0"
Text="SwedishAndNeutral" />
<TextBlock Grid.Row="1"
Grid.Column="1"
Text="{l:Resource p:Resources.SwedishAndNeutral}" />
Grid.Column="1"
Text="{l:Resource p:Resources.SwedishAndNeutral}" />


<TextBlock Grid.Row="2"
Grid.Column="0"
Text="MissingTranslation" />
Grid.Column="0"
Text="MissingTranslation" />
<TextBlock Grid.Row="2"
Grid.Column="1"
Text="{l:Resource p:Resources.MissingTranslation}" />
Grid.Column="1"
Text="{l:Resource p:Resources.MissingTranslation}" />

<TextBlock Grid.Row="3"
Grid.Column="0"
Text="NeutralOnly" />
Grid.Column="0"
Text="NeutralOnly" />
<TextBlock Grid.Row="3"
Grid.Column="1"
Text="{l:Resource p:Resources.NeutralOnly}" />
Grid.Column="1"
Text="{l:Resource p:Resources.NeutralOnly}" />

<TextBlock Grid.Row="4"
Grid.Column="0"
Text="BadFormat" />
Grid.Column="0"
Text="BadFormat" />
<TextBlock Grid.Row="4"
Grid.Column="1"
Text="{l:Resource BadFormat}" />
Grid.Column="1"
Text="{l:Resource BadFormat}" />
</Grid>
</Grid>
</UserControl>
5 changes: 3 additions & 2 deletions Gu.Wpf.Localization.Demo.WithResources/LocalUserControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
xmlns:withResources="clr-namespace:Gu.Wpf.Localization.Demo.WithResources"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d"
l:Translator.Culture="sv">
l:Translator.Culture="sv"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand All @@ -35,5 +35,6 @@
<TextBlock Grid.Row="1"
Grid.Column="1"
Text="{l:Resource p:Resources.TranslatedToAll}" />

</Grid>
</UserControl>
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization.Tests/TranslatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void TranslateFromCodeBehindTest()
public void NotifiesOnLanguageChanged()
{
var cultureInfos = new List<CultureInfo>();
Translator.LanguageCahnged += (sender, info) => cultureInfos.Add(info);
Translator.LanguageChanged += (sender, info) => cultureInfos.Add(info);

Translator.CurrentCulture = CultureInfo.GetCultureInfo("en");
CollectionAssert.AreEqual(new[] { "en" }, cultureInfos.Select(x => x.TwoLetterISOLanguageName));
Expand Down
2 changes: 1 addition & 1 deletion Gu.Wpf.Localization/CultureProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CultureProxy : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public CultureProxy()
{
Translator.LanguageCahnged += (sender, info) => OnPropertyChanged("CurrentCulture");
Translator.LanguageChanged += (sender, info) => OnPropertyChanged("CurrentCulture");
}

public CultureInfo CurrentCulture
Expand Down
5 changes: 4 additions & 1 deletion Gu.Wpf.Localization/DesignMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public static class DesignMode

public static bool IsDesignMode
{
get { return DesignerProperties.GetIsInDesignMode(DependencyObject); }
get
{
return DesignerProperties.GetIsInDesignMode(DependencyObject);
}
}
}
}
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.0.3.3")]
[assembly: AssemblyFileVersion("1.0.3.3")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: NeutralResourcesLanguageAttribute("en")]
[assembly: InternalsVisibleTo("Gu.Wpf.Localization.Tests", AllInternalsVisible = true) ]
132 changes: 70 additions & 62 deletions Gu.Wpf.Localization/ResourceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,40 @@ public ResourceExtension(string member)
/// <param name="serviceProvider">An object that can provide services for the markup extension. The service provider is expected to provide a service that implements a type resolver (<see cref="T:System.Windows.Markup.IXamlTypeResolver"/>).</param><exception cref="T:System.InvalidOperationException">The <paramref name="member"/> value for the extension is null at the time of evaluation.</exception><exception cref="T:System.ArgumentException">Some part of the <paramref name="member"/> string did not parse properly-or-<paramref name="serviceProvider"/> did not provide a service for <see cref="T:System.Windows.Markup.IXamlTypeResolver"/>-or-<paramref name="member"/> value did not resolve to a static member.</exception><exception cref="T:System.ArgumentNullException"><paramref name="serviceProvider"/> is null.</exception>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.Member == null)
throw new InvalidOperationException("MarkupExtensionStaticMember");
if (DesignMode.IsDesignMode)
try
{
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (target != null && target.TargetObject is FrameworkElement)
if (this.Member == null)
throw new InvalidOperationException("MarkupExtensionStaticMember");
if (DesignMode.IsDesignMode)
{
var frameworkElement = (FrameworkElement)target.TargetObject;
var value = (XmlLanguage)frameworkElement.GetValue(FrameworkElement.LanguageProperty);
if (value != null)
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (target != null && target.TargetObject is FrameworkElement)
{
Translator.CurrentCulture = CultureInfo.GetCultureInfo(value.IetfLanguageTag);
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;
if (target != null && !(target.TargetObject is DependencyObject))
{
xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;

return this;
return this;
}
}
}

Type type = this.MemberType;
string name;
string str;
if (type != (Type)null)
{
name = this.Member;
str = type.FullName + "." + this.Member;
}
else
{
str = this.Member;
int length = this.Member.IndexOf('.');
if (length < 0)
Type type = this.MemberType;
string name;
if (type != (Type)null)
{
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
name = this.Member;
}
else
{
string qualifiedTypeName = this.Member.Substring(0, length);
if (qualifiedTypeName == string.Empty)
int length = this.Member.IndexOf('.');
if (length < 0)
{
if (DesignMode.IsDesignMode)
{
Expand All @@ -94,50 +82,70 @@ public override object ProvideValue(IServiceProvider serviceProvider)
}
else
{
if (serviceProvider == null)
throw new ArgumentNullException("serviceProvider");
if (xamlTypeResolver == null)
{
xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
}
if (xamlTypeResolver == null)
string qualifiedTypeName = this.Member.Substring(0, length);
if (string.IsNullOrEmpty(qualifiedTypeName))
{
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}
else
{
type = xamlTypeResolver.Resolve(qualifiedTypeName);
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
if (name == string.Empty)
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
{
if (DesignMode.IsDesignMode)
type = xamlTypeResolver.Resolve(qualifiedTypeName);
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}
}
}
}
}

var resourceManager = Cache.GetOrAdd(type, GetManager);
if (resourceManager == null)
var resourceManager = Cache.GetOrAdd(type, GetManager);
if (resourceManager == null)
{
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
}
return string.Format(Properties.Resources.NullManagerFormat, name);
}

var translator = new Translator(resourceManager, name);
var binding = new Binding("Value")
{
Source = translator
};
var provideValue = binding.ProvideValue(serviceProvider);
return provideValue;
}
catch (Exception)
{
if (DesignMode.IsDesignMode)
{
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
throw;
}
return string.Format(Properties.Resources.NullManagerFormat, name);
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
}

var translator = new Translator(resourceManager, name);
var binding = new Binding("Value")
{
Source = translator
};
var provideValue = binding.ProvideValue(serviceProvider);
return provideValue;
}

private static ResourceManagerWrapper GetManager(Type type)
Expand Down
Loading

0 comments on commit 2f1b119

Please sign in to comment.