Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions src/OpenClaw.Tray.WinUI/Helpers/LocalizationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,73 @@ private static ResourceContext GetContext()

public static string GetString(string resourceKey)
{
try
var found = TryGetValueAsString(resourceKey, out var value, out var lookupFailure);
if (found && !string.IsNullOrEmpty(value))
{
var candidate = Manager.MainResourceMap.GetValue($"Resources/{resourceKey}", GetContext());
var value = candidate?.ValueAsString;
return string.IsNullOrEmpty(value) ? resourceKey : value;
return value;
}
catch (Exception ex)

if (TryGetXamlPropertyResourcePath(resourceKey, out var propertyResourcePath))
{
var logKey = $"{_languageOverride ?? "<default>"}:{resourceKey}:{ex.GetType().FullName}";
var propertyFound = TryGetValueAsString(propertyResourcePath, out value, out var propertyLookupFailure);
if (propertyFound && !string.IsNullOrEmpty(value))
{
return value;
}

if (!found)
{
lookupFailure ??= propertyLookupFailure;
}
}

if (lookupFailure is not null)
{
var logKey = $"{_languageOverride ?? "<default>"}:{resourceKey}:{lookupFailure.GetType().FullName}";
if (s_loggedLookupFailures.ContainsKey(logKey))
return resourceKey;

if (s_loggedLookupFailures.Count < MaxLoggedLookupFailures && s_loggedLookupFailures.TryAdd(logKey, 0))
{
Logger.Warn($"LocalizationHelper: Resource lookup failed for '{resourceKey}' (language='{_languageOverride ?? "<default>"}'): {ex.Message}");
Logger.Warn($"LocalizationHelper: Resource lookup failed for '{resourceKey}' (language='{_languageOverride ?? "<default>"}'): {lookupFailure.Message}");
}
else if (System.Threading.Interlocked.Exchange(ref s_lookupFailureLimitLogged, 1) == 0)
{
Logger.Warn("LocalizationHelper: Resource lookup failure log limit reached; suppressing additional unique resource lookup failures");
}
return resourceKey;
}

return resourceKey;
}

private static bool TryGetValueAsString(string resourceKey, out string? value, out Exception? lookupFailure)
{
try
{
var candidate = Manager.MainResourceMap.GetValue($"Resources/{resourceKey}", GetContext());
value = candidate?.ValueAsString;
lookupFailure = null;
return true;
}
catch (Exception ex)
{
value = null;
lookupFailure = ex;
return false;
}
}

private static bool TryGetXamlPropertyResourcePath(string resourceKey, out string resourcePath)
{
var propertySeparator = resourceKey.LastIndexOf('.');
if (propertySeparator > 0 && propertySeparator < resourceKey.Length - 1)
{
resourcePath = $"{resourceKey[..propertySeparator]}/{resourceKey[(propertySeparator + 1)..]}";
return true;
}

resourcePath = string.Empty;
return false;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/OpenClaw.Tray.Tests/AssistantBridgeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public async Task StartListenServiceAsync_KillsTimedOutBackendCommand()
NullLogger.Instance,
"owner",
() => launcher,
TimeSpan.FromSeconds(2));
TimeSpan.FromSeconds(5));

var result = await service.StartListenServiceAsync();

Expand Down
20 changes: 20 additions & 0 deletions tests/OpenClaw.Tray.Tests/LocalizationHelperSourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.IO;

namespace OpenClaw.Tray.Tests;

public sealed class LocalizationHelperSourceTests
{
[Fact]
public void GetString_ResolvesXamlPropertyResourceKeys()
{
var source = ReadSource("src", "OpenClaw.Tray.WinUI", "Helpers", "LocalizationHelper.cs");

Assert.Contains("TryGetXamlPropertyResourcePath(resourceKey, out var propertyResourcePath)", source);
Assert.Contains("TryGetValueAsString(propertyResourcePath", source);
Assert.Contains("LastIndexOf('.')", source);
Assert.Contains("\"{resourceKey[..propertySeparator]}/{resourceKey[(propertySeparator + 1)..]}\"", source);
}

private static string ReadSource(params string[] parts)
=> File.ReadAllText(Path.Combine(new[] { TestRepositoryPaths.GetRepositoryRoot() }.Concat(parts).ToArray()));
}