Skip to content

Commit

Permalink
Merge pull request #38 from AndrewKeepCoding/36-add-support-for-attac…
Browse files Browse the repository at this point in the history
…hed-properties

Add support for attached properties
  • Loading branch information
AndrewKeepCoding authored Nov 1, 2023
2 parents c93ba39 + 560eb49 commit 0cd51fc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
7 changes: 7 additions & 0 deletions WinUI3Localizer.SampleApp/ControlsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@
<x:String xml:space="preserve">&lt;Slider l:Uids.Uid="ControlsPage_Slider" /&gt;</x:String>
</local:ExamplePresenter.XamlSampleCode>
</local:ExamplePresenter>
<!-- ToolTip -->
<local:ExamplePresenter HeaderText="ToolTip">
<Button l:Uids.Uid="ControlsPage_ToolTipButton" />
<local:ExamplePresenter.XamlSampleCode>
<x:String xml:space="preserve">&lt;Button l:Uids.Uid="ControlsPage_ToolTipButton" /&gt;</x:String>
</local:ExamplePresenter.XamlSampleCode>
</local:ExamplePresenter>
<!-- ColorPicker -->
<local:ExamplePresenter
HeaderText="ColorPicker"
Expand Down
9 changes: 9 additions & 0 deletions WinUI3Localizer.SampleApp/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@
<data name="ControlsPage_ToggleSwitch.OnContent" xml:space="preserve">
<value>Activated</value>
</data>
<data name="ControlsPage_ToolTipButton.Content" xml:space="preserve">
<value>Place the mouse pointer</value>
</data>
<data name="ControlsPage_ToolTipButton.ToolTipService.Placement" xml:space="preserve">
<value>Bottom</value>
</data>
<data name="ControlsPage_ToolTipButton.ToolTipService.ToolTip" xml:space="preserve">
<value>This is a localized ToolTip.</value>
</data>
<data name="en-US" xml:space="preserve">
<value>English</value>
</data>
Expand Down
9 changes: 9 additions & 0 deletions WinUI3Localizer.SampleApp/Strings/es-ES/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@
<data name="ControlsPage_ToggleSwitch.OnContent" xml:space="preserve">
<value>Activado</value>
</data>
<data name="ControlsPage_ToolTipButton.Content" xml:space="preserve">
<value>Coloca el mouse pointer</value>
</data>
<data name="ControlsPage_ToolTipButton.ToolTipService.Placement" xml:space="preserve">
<value>Right</value>
</data>
<data name="ControlsPage_ToolTipButton.ToolTipService.ToolTip" xml:space="preserve">
<value>Esto es un ToolTip localizado.</value>
</data>
<data name="en-US" xml:space="preserve">
<value>English</value>
</data>
Expand Down
9 changes: 9 additions & 0 deletions WinUI3Localizer.SampleApp/Strings/ja/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@
<data name="ControlsPage_ToggleSwitch.OnContent" xml:space="preserve">
<value>有効</value>
</data>
<data name="ControlsPage_ToolTipButton.Content" xml:space="preserve">
<value>マウスポインタを置いてください</value>
</data>
<data name="ControlsPage_ToolTipButton.ToolTipService.Placement" xml:space="preserve">
<value>Top</value>
</data>
<data name="ControlsPage_ToolTipButton.ToolTipService.ToolTip" xml:space="preserve">
<value>これはローカライズされたToolTipです。</value>
</data>
<data name="en-US" xml:space="preserve">
<value>English</value>
</data>
Expand Down
40 changes: 35 additions & 5 deletions WinUI3Localizer/Localizer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace WinUI3Localizer;

Expand Down Expand Up @@ -242,17 +244,46 @@ private static void DependencyObjectsReferences_DependencyObjectRemoved(object?
{
return property;
}
else if (type.GetField(

if (type.GetField(
dependencyPropertyName,
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) is FieldInfo fieldInfo &&
fieldInfo.GetValue(null) is DependencyProperty field)
{
return field;
}

// TODO: This should be done on the building process.
if (dependencyPropertyName.Split(".") is string[] splitResult &&
splitResult.Length is 2)
{
string attachedPropertyClassName = splitResult[0];
IEnumerable<Type> types = GetTypesFromName(attachedPropertyClassName);

string attachedPropertyName = splitResult[1];
IEnumerable<PropertyInfo> attachedProperties = types
.Select(x => x.GetProperty(attachedPropertyName))
.OfType<PropertyInfo>();

foreach (PropertyInfo attachedProperty in attachedProperties)
{
if (attachedProperty.GetValue(null) is DependencyProperty dependencyProperty)
{
return dependencyProperty;
}
}
}

return null;
}

private static IEnumerable<Type> GetTypesFromName(string name)
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x => x.Name == name);
}

private async Task LocalizeDependencyObjects()
{
foreach (DependencyObject dependencyObject in await this.dependencyObjectsReferences.GetDependencyObjects())
Expand Down Expand Up @@ -280,11 +311,10 @@ private void LocalizeDependencyObject(DependencyObject dependencyObject, Languag
item.DependencyPropertyName) is DependencyProperty dependencyProperty)
{
LocalizeDependencyObjectsWithDependencyProperty(dependencyObject, dependencyProperty, item.Value);
return;
}
else
{
LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value);
}

LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value);
}

private void LocalizeDependencyObjectsWithDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string value)
Expand Down
4 changes: 2 additions & 2 deletions WinUI3Localizer/LocalizerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ private static LanguageDictionary CreateLanguageDictionaryFromStringResourceItem
private static LanguageDictionary.Item CreateLanguageDictionaryItem(StringResourceItem stringResourceItem)
{
string name = stringResourceItem.Name;
(string Uid, string DependencyPropertyName) = name.LastIndexOf(".") is int lastSeparatorIndex && lastSeparatorIndex > 1
? (name[..lastSeparatorIndex], string.Concat(name.AsSpan(lastSeparatorIndex + 1), "Property"))
(string Uid, string DependencyPropertyName) = name.IndexOf(".") is int firstSeparatorIndex && firstSeparatorIndex > 1
? (name[..firstSeparatorIndex], string.Concat(name.AsSpan(firstSeparatorIndex + 1), "Property"))
: (name, string.Empty);
return new LanguageDictionary.Item(
Uid,
Expand Down
2 changes: 1 addition & 1 deletion WinUI3Localizer/WinUI3Localizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- You/users can add new languages even after deployment
- Use standard Resources.resw
</Description>
<Version>2.0.1</Version>
<Version>2.1.0</Version>
<PackageTags>winui3;winappsdk;localization;localize;language;multilanguage</PackageTags>
<PackageProjectUrl>https://github.com/AndrewKeepCoding/WinUI3Localizer</PackageProjectUrl>
<RepositoryUrl>https://github.com/AndrewKeepCoding/WinUI3Localizer</RepositoryUrl>
Expand Down

0 comments on commit 0cd51fc

Please sign in to comment.