Skip to content

Commit 2f1b119

Browse files
committed
Working ok now 1.1..0
1 parent 2045efc commit 2f1b119

8 files changed

Lines changed: 155 additions & 113 deletions

File tree

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<UserControl x:Class="Gu.Wpf.Localization.Demo.WithResources.Errors"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
54
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
65
xmlns:l="clr-namespace:Gu.Wpf.Localization;assembly=Gu.Wpf.Localization"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:p="clr-namespace:Gu.Wpf.Localization.Demo.WithResources.Properties"
8-
mc:Ignorable="d"
9-
d:DesignHeight="300" d:DesignWidth="300">
8+
d:DesignHeight="300"
9+
d:DesignWidth="300"
10+
mc:Ignorable="d">
1011
<Grid>
1112
<Grid>
1213
<Grid.ColumnDefinitions>
@@ -24,40 +25,40 @@
2425
<RowDefinition Height="*" />
2526
</Grid.RowDefinitions>
2627
<TextBlock Grid.Row="0"
27-
Grid.Column="0"
28-
Text="MissingKey" />
28+
Grid.Column="0"
29+
Text="MissingKey" />
2930
<TextBlock Grid.Row="0"
30-
Grid.Column="1"
31-
Text="{l:Resource p:Resources.MissingKey}" />
31+
Grid.Column="1"
32+
Text="{l:Resource p:Resources.MissingKey}" />
3233

3334
<TextBlock Grid.Row="1"
34-
Grid.Column="0"
35-
Text="SwedishAndNeutral" />
35+
Grid.Column="0"
36+
Text="SwedishAndNeutral" />
3637
<TextBlock Grid.Row="1"
37-
Grid.Column="1"
38-
Text="{l:Resource p:Resources.SwedishAndNeutral}" />
38+
Grid.Column="1"
39+
Text="{l:Resource p:Resources.SwedishAndNeutral}" />
3940

4041

4142
<TextBlock Grid.Row="2"
42-
Grid.Column="0"
43-
Text="MissingTranslation" />
43+
Grid.Column="0"
44+
Text="MissingTranslation" />
4445
<TextBlock Grid.Row="2"
45-
Grid.Column="1"
46-
Text="{l:Resource p:Resources.MissingTranslation}" />
46+
Grid.Column="1"
47+
Text="{l:Resource p:Resources.MissingTranslation}" />
4748

4849
<TextBlock Grid.Row="3"
49-
Grid.Column="0"
50-
Text="NeutralOnly" />
50+
Grid.Column="0"
51+
Text="NeutralOnly" />
5152
<TextBlock Grid.Row="3"
52-
Grid.Column="1"
53-
Text="{l:Resource p:Resources.NeutralOnly}" />
53+
Grid.Column="1"
54+
Text="{l:Resource p:Resources.NeutralOnly}" />
5455

5556
<TextBlock Grid.Row="4"
56-
Grid.Column="0"
57-
Text="BadFormat" />
57+
Grid.Column="0"
58+
Text="BadFormat" />
5859
<TextBlock Grid.Row="4"
59-
Grid.Column="1"
60-
Text="{l:Resource BadFormat}" />
60+
Grid.Column="1"
61+
Text="{l:Resource BadFormat}" />
6162
</Grid>
6263
</Grid>
6364
</UserControl>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
xmlns:withResources="clr-namespace:Gu.Wpf.Localization.Demo.WithResources"
1010
d:DesignHeight="300"
1111
d:DesignWidth="300"
12-
mc:Ignorable="d"
13-
l:Translator.Culture="sv">
12+
l:Translator.Culture="sv"
13+
mc:Ignorable="d">
1414
<Grid>
1515
<Grid.ColumnDefinitions>
1616
<ColumnDefinition Width="Auto" />
@@ -35,5 +35,6 @@
3535
<TextBlock Grid.Row="1"
3636
Grid.Column="1"
3737
Text="{l:Resource p:Resources.TranslatedToAll}" />
38+
3839
</Grid>
3940
</UserControl>

Gu.Wpf.Localization.Tests/TranslatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void TranslateFromCodeBehindTest()
5858
public void NotifiesOnLanguageChanged()
5959
{
6060
var cultureInfos = new List<CultureInfo>();
61-
Translator.LanguageCahnged += (sender, info) => cultureInfos.Add(info);
61+
Translator.LanguageChanged += (sender, info) => cultureInfos.Add(info);
6262

6363
Translator.CurrentCulture = CultureInfo.GetCultureInfo("en");
6464
CollectionAssert.AreEqual(new[] { "en" }, cultureInfos.Select(x => x.TwoLetterISOLanguageName));

Gu.Wpf.Localization/CultureProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class CultureProxy : INotifyPropertyChanged
88
public event PropertyChangedEventHandler PropertyChanged;
99
public CultureProxy()
1010
{
11-
Translator.LanguageCahnged += (sender, info) => OnPropertyChanged("CurrentCulture");
11+
Translator.LanguageChanged += (sender, info) => OnPropertyChanged("CurrentCulture");
1212
}
1313

1414
public CultureInfo CurrentCulture

Gu.Wpf.Localization/DesignMode.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ public static class DesignMode
99

1010
public static bool IsDesignMode
1111
{
12-
get { return DesignerProperties.GetIsInDesignMode(DependencyObject); }
12+
get
13+
{
14+
return DesignerProperties.GetIsInDesignMode(DependencyObject);
15+
}
1316
}
1417
}
1518
}

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.0.3.3")]
34-
[assembly: AssemblyFileVersion("1.0.3.3")]
33+
[assembly: AssemblyVersion("1.1.0.0")]
34+
[assembly: AssemblyFileVersion("1.1.0.0")]
3535
[assembly: NeutralResourcesLanguageAttribute("en")]
3636
[assembly: InternalsVisibleTo("Gu.Wpf.Localization.Tests", AllInternalsVisible = true) ]

Gu.Wpf.Localization/ResourceExtension.cs

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -39,52 +39,40 @@ public ResourceExtension(string member)
3939
/// <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>
4040
public override object ProvideValue(IServiceProvider serviceProvider)
4141
{
42-
if (this.Member == null)
43-
throw new InvalidOperationException("MarkupExtensionStaticMember");
44-
if (DesignMode.IsDesignMode)
42+
try
4543
{
46-
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
47-
if (target != null && target.TargetObject is FrameworkElement)
44+
if (this.Member == null)
45+
throw new InvalidOperationException("MarkupExtensionStaticMember");
46+
if (DesignMode.IsDesignMode)
4847
{
49-
var frameworkElement = (FrameworkElement)target.TargetObject;
50-
var value = (XmlLanguage)frameworkElement.GetValue(FrameworkElement.LanguageProperty);
51-
if (value != null)
48+
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
49+
if (target != null && target.TargetObject is FrameworkElement)
5250
{
53-
Translator.CurrentCulture = CultureInfo.GetCultureInfo(value.IetfLanguageTag);
51+
var frameworkElement = (FrameworkElement)target.TargetObject;
52+
var value = (XmlLanguage)frameworkElement.GetValue(FrameworkElement.LanguageProperty);
53+
if (value != null)
54+
{
55+
Translator.CurrentCulture = CultureInfo.GetCultureInfo(value.IetfLanguageTag);
56+
}
5457
}
55-
}
56-
if (target != null && !(target.TargetObject is DependencyObject))
57-
{
58-
xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
58+
if (target != null && !(target.TargetObject is DependencyObject))
59+
{
60+
xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
5961

60-
return this;
62+
return this;
63+
}
6164
}
62-
}
6365

64-
Type type = this.MemberType;
65-
string name;
66-
string str;
67-
if (type != (Type)null)
68-
{
69-
name = this.Member;
70-
str = type.FullName + "." + this.Member;
71-
}
72-
else
73-
{
74-
str = this.Member;
75-
int length = this.Member.IndexOf('.');
76-
if (length < 0)
66+
Type type = this.MemberType;
67+
string name;
68+
if (type != (Type)null)
7769
{
78-
if (DesignMode.IsDesignMode)
79-
{
80-
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
81-
}
82-
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
70+
name = this.Member;
8371
}
8472
else
8573
{
86-
string qualifiedTypeName = this.Member.Substring(0, length);
87-
if (qualifiedTypeName == string.Empty)
74+
int length = this.Member.IndexOf('.');
75+
if (length < 0)
8876
{
8977
if (DesignMode.IsDesignMode)
9078
{
@@ -94,50 +82,70 @@ public override object ProvideValue(IServiceProvider serviceProvider)
9482
}
9583
else
9684
{
97-
if (serviceProvider == null)
98-
throw new ArgumentNullException("serviceProvider");
99-
if (xamlTypeResolver == null)
100-
{
101-
xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
102-
}
103-
if (xamlTypeResolver == null)
85+
string qualifiedTypeName = this.Member.Substring(0, length);
86+
if (string.IsNullOrEmpty(qualifiedTypeName))
10487
{
105-
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
88+
if (DesignMode.IsDesignMode)
89+
{
90+
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
91+
}
92+
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
10693
}
10794
else
10895
{
109-
type = xamlTypeResolver.Resolve(qualifiedTypeName);
110-
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
111-
if (name == string.Empty)
96+
if (serviceProvider == null)
97+
throw new ArgumentNullException("serviceProvider");
98+
if (xamlTypeResolver == null)
99+
{
100+
xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
101+
}
102+
if (xamlTypeResolver == null)
103+
{
104+
throw new ArgumentException("MarkupExtensionNoContext IXamlTypeResolver");
105+
}
106+
else
112107
{
113-
if (DesignMode.IsDesignMode)
108+
type = xamlTypeResolver.Resolve(qualifiedTypeName);
109+
name = this.Member.Substring(length + 1, this.Member.Length - length - 1);
110+
if (string.IsNullOrEmpty(name))
114111
{
115-
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
112+
if (DesignMode.IsDesignMode)
113+
{
114+
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
115+
}
116+
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
116117
}
117-
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
118118
}
119119
}
120120
}
121121
}
122-
}
123122

124-
var resourceManager = Cache.GetOrAdd(type, GetManager);
125-
if (resourceManager == null)
123+
var resourceManager = Cache.GetOrAdd(type, GetManager);
124+
if (resourceManager == null)
125+
{
126+
if (DesignMode.IsDesignMode)
127+
{
128+
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
129+
}
130+
return string.Format(Properties.Resources.NullManagerFormat, name);
131+
}
132+
133+
var translator = new Translator(resourceManager, name);
134+
var binding = new Binding("Value")
135+
{
136+
Source = translator
137+
};
138+
var provideValue = binding.ProvideValue(serviceProvider);
139+
return provideValue;
140+
}
141+
catch (Exception)
126142
{
127143
if (DesignMode.IsDesignMode)
128144
{
129-
throw new ArgumentException("Expecting format p:Resources.Key was:" + Member);
145+
throw;
130146
}
131-
return string.Format(Properties.Resources.NullManagerFormat, name);
147+
return string.Format(Properties.Resources.UnknownErrorFormat, Member);
132148
}
133-
134-
var translator = new Translator(resourceManager, name);
135-
var binding = new Binding("Value")
136-
{
137-
Source = translator
138-
};
139-
var provideValue = binding.ProvideValue(serviceProvider);
140-
return provideValue;
141149
}
142150

143151
private static ResourceManagerWrapper GetManager(Type type)

0 commit comments

Comments
 (0)