Implement parent culture fallback for OverrideLocaleResources

Co-authored-by: rabbitism <14807942+rabbitism@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-07-28 15:18:47 +00:00
parent 8ac1e010a3
commit 6c8f3a4772

View File

@@ -46,7 +46,7 @@ public class SemiTheme : Styles
{ {
try try
{ {
if (TryGetLocaleResource(value, out var resource) && resource is not null) if (TryGetLocaleResource(value, false, out var resource) && resource is not null)
{ {
_locale = value; _locale = value;
foreach (var kv in resource) Resources[kv.Key] = kv.Value; foreach (var kv in resource) Resources[kv.Key] = kv.Value;
@@ -65,6 +65,11 @@ public class SemiTheme : Styles
} }
private static bool TryGetLocaleResource(CultureInfo? locale, out ResourceDictionary? resourceDictionary) private static bool TryGetLocaleResource(CultureInfo? locale, out ResourceDictionary? resourceDictionary)
{
return TryGetLocaleResource(locale, false, out resourceDictionary);
}
private static bool TryGetLocaleResource(CultureInfo? locale, bool enableParentCultureFallback, out ResourceDictionary? resourceDictionary)
{ {
if (Equals(locale, CultureInfo.InvariantCulture)) if (Equals(locale, CultureInfo.InvariantCulture))
{ {
@@ -78,20 +83,56 @@ public class SemiTheme : Styles
return false; return false;
} }
// Try exact match first
if (_localeToResource.TryGetValue(locale, out var resource)) if (_localeToResource.TryGetValue(locale, out var resource))
{ {
resourceDictionary = resource; resourceDictionary = resource;
return true; return true;
} }
// If enabled, try to find a culture that has this locale as parent or that shares the same parent
if (enableParentCultureFallback)
{
// Look for any supported culture that has this locale as its parent
foreach (var supportedCulture in _localeToResource.Keys)
{
if (Equals(supportedCulture.Parent, locale))
{
resourceDictionary = _localeToResource[supportedCulture];
return true;
}
}
// If this locale has a parent (and it's not invariant), try to find
// a supported culture that shares the same parent
var targetParent = locale.Parent;
if (!Equals(targetParent, CultureInfo.InvariantCulture))
{
foreach (var supportedCulture in _localeToResource.Keys)
{
if (Equals(supportedCulture.Parent, targetParent))
{
resourceDictionary = _localeToResource[supportedCulture];
return true;
}
}
}
}
resourceDictionary = _defaultResource; resourceDictionary = _defaultResource;
return false; return false;
} }
public static void OverrideLocaleResources(Application application, CultureInfo? culture) public static void OverrideLocaleResources(Application application, CultureInfo? culture)
{
OverrideLocaleResources(application, culture, false);
}
public static void OverrideLocaleResources(Application application, CultureInfo? culture, bool enableParentCultureFallback)
{ {
if (culture is null) return; if (culture is null) return;
if (!_localeToResource.TryGetValue(culture, out var resources)) return; if (!TryGetLocaleResource(culture, enableParentCultureFallback, out var resources)) return;
if (resources is null) return;
foreach (var kv in resources) foreach (var kv in resources)
{ {
application.Resources[kv.Key] = kv.Value; application.Resources[kv.Key] = kv.Value;
@@ -99,9 +140,15 @@ public class SemiTheme : Styles
} }
public static void OverrideLocaleResources(StyledElement element, CultureInfo? culture) public static void OverrideLocaleResources(StyledElement element, CultureInfo? culture)
{
OverrideLocaleResources(element, culture, false);
}
public static void OverrideLocaleResources(StyledElement element, CultureInfo? culture, bool enableParentCultureFallback)
{ {
if (culture is null) return; if (culture is null) return;
if (!_localeToResource.TryGetValue(culture, out var resources)) return; if (!TryGetLocaleResource(culture, enableParentCultureFallback, out var resources)) return;
if (resources is null) return;
foreach (var kv in resources) foreach (var kv in resources)
{ {
element.Resources[kv.Key] = kv.Value; element.Resources[kv.Key] = kv.Value;