From 1b35aed21f209303a7d04f5e65d2c751035bfffe Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 30 Aug 2021 15:08:18 +0200 Subject: [PATCH 1/2] fix: Fallback to specific regional culture if neutral culture isn't found --- .../SamplesApp.Shared.projitems | 2 + .../Strings/es-ES/Resources.resw | 123 ++++++++++++++++++ .../Strings/es-MX/Resources.resw | 123 ++++++++++++++++++ .../Given_ResourceLoader.cs | 9 ++ .../Resources/ResourceLoader.cs | 11 ++ 5 files changed, 268 insertions(+) create mode 100644 src/SamplesApp/SamplesApp.Shared/Strings/es-ES/Resources.resw create mode 100644 src/SamplesApp/SamplesApp.Shared/Strings/es-MX/Resources.resw diff --git a/src/SamplesApp/SamplesApp.Shared/SamplesApp.Shared.projitems b/src/SamplesApp/SamplesApp.Shared/SamplesApp.Shared.projitems index 2fe49c11cc40..235e5ef87211 100644 --- a/src/SamplesApp/SamplesApp.Shared/SamplesApp.Shared.projitems +++ b/src/SamplesApp/SamplesApp.Shared/SamplesApp.Shared.projitems @@ -62,6 +62,8 @@ + + diff --git a/src/SamplesApp/SamplesApp.Shared/Strings/es-ES/Resources.resw b/src/SamplesApp/SamplesApp.Shared/Strings/es-ES/Resources.resw new file mode 100644 index 000000000000..10e51849631d --- /dev/null +++ b/src/SamplesApp/SamplesApp.Shared/Strings/es-ES/Resources.resw @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Text in 'es-ES' + + \ No newline at end of file diff --git a/src/SamplesApp/SamplesApp.Shared/Strings/es-MX/Resources.resw b/src/SamplesApp/SamplesApp.Shared/Strings/es-MX/Resources.resw new file mode 100644 index 000000000000..82add48aca5d --- /dev/null +++ b/src/SamplesApp/SamplesApp.Shared/Strings/es-MX/Resources.resw @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Text in 'es-MX' + + \ No newline at end of file diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel_Resources/Given_ResourceLoader.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel_Resources/Given_ResourceLoader.cs index 2984c98b0081..d765603f8040 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel_Resources/Given_ResourceLoader.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel_Resources/Given_ResourceLoader.cs @@ -123,6 +123,15 @@ public void When_MissingLocalizedResource_FallbackOnParent() Assert.AreEqual(@"Text in 'fr'", SUT.GetString("Given_ResourceLoader/When_LocalizedResource")); } + [TestMethod] + public void When_MissingLocalizedResource_FallbackOnRegional() + { + var SUT = ResourceLoader.GetForViewIndependentUse(); + + CultureInfo.CurrentUICulture = new CultureInfo("es"); + Assert.AreEqual(@"Text in 'es-MX'", SUT.GetString("Given_ResourceLoader/When_LocalizedResource")); + } + [TestMethod] public void When_MissingLocalizedResource_FallbackOnDefault() { diff --git a/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs b/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs index 9c25d35f5139..0e6511ba805a 100644 --- a/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs +++ b/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs @@ -184,6 +184,17 @@ private static IEnumerable GetCulturesHierarchy(CultureInfo culture) { yield return culture.IetfLanguageTag.ToLowerInvariant(); + // If we have a culture that doesn't specify country/region, we want to match its specific cultures if it's not found. + // For example, if we have es and it's not found, we want to match es-MX + if (culture.IsNeutralCulture) + { + var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Where(c => c.Parent.Equals(culture)); + foreach (var specificCulture in specificCultures) + { + yield return specificCulture.IetfLanguageTag.ToLowerInvariant(); + } + } + culture = culture.Parent; } From e5386e8c2a25f18daf84300e4377cf490e66559c Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 1 Sep 2021 19:34:52 +0200 Subject: [PATCH 2/2] chore: Order cultures for consistent behavior across platforms --- src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs b/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs index 0e6511ba805a..9b3d7b3129c8 100644 --- a/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs +++ b/src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.cs @@ -188,7 +188,7 @@ private static IEnumerable GetCulturesHierarchy(CultureInfo culture) // For example, if we have es and it's not found, we want to match es-MX if (culture.IsNeutralCulture) { - var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Where(c => c.Parent.Equals(culture)); + var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Where(c => c.Parent.Equals(culture)).OrderByDescending(x => x.Name); foreach (var specificCulture in specificCultures) { yield return specificCulture.IetfLanguageTag.ToLowerInvariant();