Skip to content

Commit

Permalink
Replaced ResourceLoader with ResourceManager, for better debugging ex…
Browse files Browse the repository at this point in the history
…perience.
  • Loading branch information
azchohfi committed Oct 30, 2021
1 parent c9df5b8 commit 82e2bbb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CommunityToolkit.WinUI.UI.Converters
/// </summary>
public sealed class ResourceNameToResourceStringConverter : IValueConverter
{
private readonly ResourceLoader _resourceLoader = new ResourceLoader();
private readonly ResourceManager _resourceManager = new ResourceManager();

/// <summary>
/// Take the source string as a resource name that will be looked up in the App Resources.
Expand All @@ -31,7 +31,7 @@ public object Convert(object value, Type targetType, object parameter, string la
return string.Empty;
}

return _resourceLoader.GetString(value.ToString());
return _resourceManager.MainResourceMap.TryGetValue(value.ToString()).ValueAsString;
}

/// <summary>
Expand Down
19 changes: 14 additions & 5 deletions CommunityToolkit.WinUI/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace CommunityToolkit.WinUI
/// </summary>
public static class StringExtensions
{
private static readonly ResourceLoader Loader;
private static readonly ResourceManager ResourceManager;

static StringExtensions()
{
try
{
Loader = new ResourceLoader();
ResourceManager = new ResourceManager();
}
catch
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public static string GetViewLocalized(this string resourceKey, UIContext uiConte
/// <returns>string value for given resource or empty string if not found.</returns>
public static string GetLocalized(this string resourceKey)
{
return Loader?.GetString(resourceKey);
return ResourceManager.MainResourceMap.TryGetValue(resourceKey)?.ValueAsString;
}

/*
Expand Down Expand Up @@ -75,11 +75,20 @@ public static string GetLocalized(this string resourceKey, UIContext uiContext)
public static string GetLocalized(this string resourceKey, string resourcePath)
{
// Try and retrieve resource at app level first.
var result = Loader?.GetString(resourceKey);
var result = ResourceManager.MainResourceMap.TryGetValue(resourceKey)?.ValueAsString;

if (string.IsNullOrEmpty(result))
{
result = new ResourceLoader(ResourceLoader.GetDefaultResourceFilePath(), resourcePath).GetString(resourceKey);
var manager = new ResourceManager();
var subTree = manager.MainResourceMap.TryGetSubtree(resourcePath);
if (subTree != null)
{
var r = subTree.TryGetValue(resourceKey);
if (r != null)
{
result = r.ValueAsString;
}
}
}

return result;
Expand Down

0 comments on commit 82e2bbb

Please sign in to comment.