From f8a65cbe82fa148dd7ae286876d83b8106fbc475 Mon Sep 17 00:00:00 2001 From: Peter Umpleby Date: Tue, 18 Jan 2022 22:20:47 +0000 Subject: [PATCH 1/4] create resource manager directly using embedded resource and assembly (addresses an issue where resource classes are not created). --- src/Providers/ResxLocalizationProviderBase.cs | 66 +++++++++++++------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/src/Providers/ResxLocalizationProviderBase.cs b/src/Providers/ResxLocalizationProviderBase.cs index c96469e..907848d 100644 --- a/src/Providers/ResxLocalizationProviderBase.cs +++ b/src/Providers/ResxLocalizationProviderBase.cs @@ -470,6 +470,14 @@ string TryGetNamespace(Type type) // remove ".resources" from the end foundResource = foundResource.Substring(0, foundResource.Length - ResourceFileExtension.Length); + if (TryCreateResourceManager(foundResource, assembly, out ResourceManager resourceManager)) + { + CacheResourceManager(resManKey, resourceManager); + + return resourceManager; + } + + // First try the simple retrieval Type resourceManagerType; try @@ -519,27 +527,47 @@ bool MatchesDictTypeName(Type type) if (resManager == null) throw new ArgumentException(string.Format("No resource manager for dictionary '{0}' in assembly '{1}' found! ({1}.{0})", resourceDictionary, resourceAssembly)); - // Add the ResourceManager to the cachelist - Add(resManKey, resManager); + CacheResourceManager(resManKey, resManager); + } - try - { - // Look in all cultures and check available ressources. - foreach (var c in SearchCultures) - { - var rs = resManager.GetResourceSet(c, true, false); - if (rs != null) - AddCulture(c); - } - } - catch + // return the found ResourceManager + return resManager; + } + + private void CacheResourceManager(string resManKey, ResourceManager resManager) + { + // Add the ResourceManager to the cachelist + Add(resManKey, resManager); + + try + { + // Look in all cultures and check available ressources. + foreach (var c in SearchCultures) { - // ignored + var rs = resManager.GetResourceSet(c, true, false); + if (rs != null) + AddCulture(c); } } + catch + { + // ignored + } + } + + private bool TryCreateResourceManager(string resourceManagerBaseName, Assembly resourceManagerAssembly, out ResourceManager resourceManager) + { + try + { + resourceManager = new ResourceManager(resourceManagerBaseName, resourceManagerAssembly); + } + catch + { + resourceManager = null; + } + + return resourceManager != null; - // return the found ResourceManager - return resManager; } private ResourceManager GetResourceManagerFromType(IReflect type) @@ -561,9 +589,9 @@ private ResourceManager GetResourceManagerFromType(IReflect type) return null; } } - #endregion +#endregion - #region ILocalizationProvider implementation +#region ILocalizationProvider implementation /// /// Uses the key and target to build a fully qualified resource key (Assembly, Dictionary, Key) /// @@ -711,6 +739,6 @@ public virtual object GetLocalizedObject(string key, DependencyObject target, Cu /// An observable list of available cultures. /// public ObservableCollection AvailableCultures { get; protected set; } - #endregion +#endregion } } From 2eb5af14a5c7d8ce99bf4717aa7bde97fe6664aa Mon Sep 17 00:00:00 2001 From: Peter Umpleby Date: Tue, 18 Jan 2022 23:24:57 +0000 Subject: [PATCH 2/4] codefactor --- src/Providers/ResxLocalizationProviderBase.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Providers/ResxLocalizationProviderBase.cs b/src/Providers/ResxLocalizationProviderBase.cs index 907848d..760c420 100644 --- a/src/Providers/ResxLocalizationProviderBase.cs +++ b/src/Providers/ResxLocalizationProviderBase.cs @@ -476,8 +476,7 @@ string TryGetNamespace(Type type) return resourceManager; } - - + // First try the simple retrieval Type resourceManagerType; try @@ -569,7 +568,6 @@ private bool TryCreateResourceManager(string resourceManagerBaseName, Assembly r return resourceManager != null; } - private ResourceManager GetResourceManagerFromType(IReflect type) { if (type == null) From 695c5e0fa649e790eff60a19d85b4a7fcd3a90f4 Mon Sep 17 00:00:00 2001 From: Peter Umpleby Date: Tue, 18 Jan 2022 23:26:50 +0000 Subject: [PATCH 3/4] codefactor --- src/Providers/ResxLocalizationProviderBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/ResxLocalizationProviderBase.cs b/src/Providers/ResxLocalizationProviderBase.cs index 760c420..f76185e 100644 --- a/src/Providers/ResxLocalizationProviderBase.cs +++ b/src/Providers/ResxLocalizationProviderBase.cs @@ -566,8 +566,8 @@ private bool TryCreateResourceManager(string resourceManagerBaseName, Assembly r } return resourceManager != null; - } + private ResourceManager GetResourceManagerFromType(IReflect type) { if (type == null) From 9a09e5921f9b46ebf06c4a393f73aa22ec748cbe Mon Sep 17 00:00:00 2001 From: Peter Umpleby Date: Wed, 19 Jan 2022 07:36:14 +0000 Subject: [PATCH 4/4] added no resx designer example --- tests/HelloWorldWPF.NoDesignFiles.sln | 51 ++++++ tests/HelloWorldWPF.NoDesignFiles/App.xaml | 7 + tests/HelloWorldWPF.NoDesignFiles/App.xaml.cs | 16 ++ .../HelloWorldWPF.NoDesignFiles.csproj | 42 +++++ .../MainWindow.xaml | 91 ++++++++++ .../MainWindow.xaml.cs | 70 ++++++++ .../Properties/AssemblyInfo.cs | 28 +++ .../Ressourcen.ar.resx | 144 ++++++++++++++++ .../Ressourcen.de.resx | 144 ++++++++++++++++ .../Ressourcen.en.resx | 159 ++++++++++++++++++ .../Ressourcen.he.resx | 144 ++++++++++++++++ .../Ressourcen.resx | 144 ++++++++++++++++ tests/HelloWorldWPF.NoDesignFiles/TestVM.cs | 77 +++++++++ 13 files changed, 1117 insertions(+) create mode 100644 tests/HelloWorldWPF.NoDesignFiles.sln create mode 100644 tests/HelloWorldWPF.NoDesignFiles/App.xaml create mode 100644 tests/HelloWorldWPF.NoDesignFiles/App.xaml.cs create mode 100644 tests/HelloWorldWPF.NoDesignFiles/HelloWorldWPF.NoDesignFiles.csproj create mode 100644 tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml create mode 100644 tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml.cs create mode 100644 tests/HelloWorldWPF.NoDesignFiles/Properties/AssemblyInfo.cs create mode 100644 tests/HelloWorldWPF.NoDesignFiles/Ressourcen.ar.resx create mode 100644 tests/HelloWorldWPF.NoDesignFiles/Ressourcen.de.resx create mode 100644 tests/HelloWorldWPF.NoDesignFiles/Ressourcen.en.resx create mode 100644 tests/HelloWorldWPF.NoDesignFiles/Ressourcen.he.resx create mode 100644 tests/HelloWorldWPF.NoDesignFiles/Ressourcen.resx create mode 100644 tests/HelloWorldWPF.NoDesignFiles/TestVM.cs diff --git a/tests/HelloWorldWPF.NoDesignFiles.sln b/tests/HelloWorldWPF.NoDesignFiles.sln new file mode 100644 index 0000000..699c6d8 --- /dev/null +++ b/tests/HelloWorldWPF.NoDesignFiles.sln @@ -0,0 +1,51 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WPFLocalizeExtension", "..\src\WPFLocalizeExtension.csproj", "{3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloWorldWPF.NoDesignFiles", "HelloWorldWPF.NoDesignFiles\HelloWorldWPF.NoDesignFiles.csproj", "{585EA5B3-C6A5-4444-8890-4D06561B4914}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|Mixed Platforms = Debug|Mixed Platforms + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|Mixed Platforms = Release|Mixed Platforms + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|x86.ActiveCfg = Debug|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Debug|x86.Build.0 = Debug|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Any CPU.Build.0 = Release|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|x86.ActiveCfg = Release|Any CPU + {3B2FA6D1-6EC0-4C0F-BDF0-D90158904ED3}.Release|x86.Build.0 = Release|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Any CPU.Build.0 = Debug|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|x86.ActiveCfg = Debug|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Debug|x86.Build.0 = Debug|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Any CPU.ActiveCfg = Release|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Any CPU.Build.0 = Release|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|x86.ActiveCfg = Release|Any CPU + {585EA5B3-C6A5-4444-8890-4D06561B4914}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F0544A03-17E3-4EFB-BB50-4EDEB145B721} + EndGlobalSection +EndGlobal diff --git a/tests/HelloWorldWPF.NoDesignFiles/App.xaml b/tests/HelloWorldWPF.NoDesignFiles/App.xaml new file mode 100644 index 0000000..8da038e --- /dev/null +++ b/tests/HelloWorldWPF.NoDesignFiles/App.xaml @@ -0,0 +1,7 @@ + + + diff --git a/tests/HelloWorldWPF.NoDesignFiles/App.xaml.cs b/tests/HelloWorldWPF.NoDesignFiles/App.xaml.cs new file mode 100644 index 0000000..70be8ca --- /dev/null +++ b/tests/HelloWorldWPF.NoDesignFiles/App.xaml.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Windows; + +namespace HalloWeltWPF +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/tests/HelloWorldWPF.NoDesignFiles/HelloWorldWPF.NoDesignFiles.csproj b/tests/HelloWorldWPF.NoDesignFiles/HelloWorldWPF.NoDesignFiles.csproj new file mode 100644 index 0000000..d33b057 --- /dev/null +++ b/tests/HelloWorldWPF.NoDesignFiles/HelloWorldWPF.NoDesignFiles.csproj @@ -0,0 +1,42 @@ + + + + + netcoreapp3.1 + HalloWeltWPF + WinExe + + True + true + HelloWorldWPF + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml b/tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml new file mode 100644 index 0000000..4c4fba0 --- /dev/null +++ b/tests/HelloWorldWPF.NoDesignFiles/MainWindow.xaml @@ -0,0 +1,91 @@ + + + + + + + + + + + + +