From 2f164905c815f6aead435b18a49cf3324cc3f0ad Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Wed, 1 Nov 2023 09:07:08 +0900 Subject: [PATCH 1/4] Separate the Uid using the first "." instead of the last one For example, the Uid of "SomeButton.ToolTipService.Tooltip" should be "SomeButton". --- WinUI3Localizer/LocalizerBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WinUI3Localizer/LocalizerBuilder.cs b/WinUI3Localizer/LocalizerBuilder.cs index c94830e..98bda66 100644 --- a/WinUI3Localizer/LocalizerBuilder.cs +++ b/WinUI3Localizer/LocalizerBuilder.cs @@ -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, From cae8034fd67bf2139c9afbae5ec0d57d48262abb Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Wed, 1 Nov 2023 09:37:18 +0900 Subject: [PATCH 2/4] Add AttachedProperties localization This should be done on the building process. --- WinUI3Localizer/Localizer.cs | 40 +++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/WinUI3Localizer/Localizer.cs b/WinUI3Localizer/Localizer.cs index af48827..44e3d8e 100644 --- a/WinUI3Localizer/Localizer.cs +++ b/WinUI3Localizer/Localizer.cs @@ -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; @@ -242,7 +244,8 @@ 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) @@ -250,9 +253,37 @@ private static void DependencyObjectsReferences_DependencyObjectRemoved(object? 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 types = GetTypesFromName(attachedPropertyClassName); + + string attachedPropertyName = splitResult[1]; + IEnumerable attachedProperties = types + .Select(x => x.GetProperty(attachedPropertyName)) + .OfType(); + + foreach (PropertyInfo attachedProperty in attachedProperties) + { + if (attachedProperty.GetValue(null) is DependencyProperty dependencyProperty) + { + return dependencyProperty; + } + } + } + return null; } + private static IEnumerable 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()) @@ -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) From d87aa542c173d9cd070bfe9ccdc9d39d71218477 Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Wed, 1 Nov 2023 09:42:33 +0900 Subject: [PATCH 3/4] Update version to v2.1.0 --- WinUI3Localizer/WinUI3Localizer.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WinUI3Localizer/WinUI3Localizer.csproj b/WinUI3Localizer/WinUI3Localizer.csproj index 4cb2fad..ea833d1 100644 --- a/WinUI3Localizer/WinUI3Localizer.csproj +++ b/WinUI3Localizer/WinUI3Localizer.csproj @@ -17,7 +17,7 @@ - You/users can add new languages even after deployment - Use standard Resources.resw - 2.0.1 + 2.1.0 winui3;winappsdk;localization;localize;language;multilanguage https://github.com/AndrewKeepCoding/WinUI3Localizer https://github.com/AndrewKeepCoding/WinUI3Localizer From 560eb49809027f63b601089aa6f34fd484dd7381 Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Wed, 1 Nov 2023 09:42:43 +0900 Subject: [PATCH 4/4] Update sample app --- WinUI3Localizer.SampleApp/ControlsPage.xaml | 7 +++++++ WinUI3Localizer.SampleApp/Strings/en-US/Resources.resw | 9 +++++++++ WinUI3Localizer.SampleApp/Strings/es-ES/Resources.resw | 9 +++++++++ WinUI3Localizer.SampleApp/Strings/ja/Resources.resw | 9 +++++++++ 4 files changed, 34 insertions(+) diff --git a/WinUI3Localizer.SampleApp/ControlsPage.xaml b/WinUI3Localizer.SampleApp/ControlsPage.xaml index 156dc81..50190ae 100644 --- a/WinUI3Localizer.SampleApp/ControlsPage.xaml +++ b/WinUI3Localizer.SampleApp/ControlsPage.xaml @@ -184,6 +184,13 @@ <Slider l:Uids.Uid="ControlsPage_Slider" /> + + +