-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3699 from BDisp/v2_3698_resourcemanager-wrapper-new
Fixes #3698. ResourceManager GetResourceSet doesn't fallback to default for no translated keys.
- Loading branch information
Showing
4 changed files
with
360 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#nullable enable | ||
|
||
using System.Collections; | ||
using System.Globalization; | ||
using System.Resources; | ||
|
||
namespace Terminal.Gui.Resources; | ||
|
||
/// <summary> | ||
/// Provide static access to the ResourceManagerWrapper | ||
/// </summary> | ||
public static class GlobalResources | ||
{ | ||
private static readonly ResourceManagerWrapper _resourceManagerWrapper; | ||
|
||
static GlobalResources () | ||
{ | ||
// Initialize the ResourceManagerWrapper once | ||
var resourceManager = new ResourceManager (typeof (Strings)); | ||
_resourceManagerWrapper = new (resourceManager); | ||
} | ||
|
||
/// <summary> | ||
/// Looks up a resource value for a particular name. Looks in the specified CultureInfo, and if not found, all parent | ||
/// CultureInfos. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <param name="culture"></param> | ||
/// <returns>Null if the resource was not found in the current culture or the invariant culture.</returns> | ||
public static object GetObject (string name, CultureInfo culture = null!) { return _resourceManagerWrapper.GetObject (name, culture); } | ||
|
||
/// <summary> | ||
/// Looks up a set of resources for a particular CultureInfo. This is not useful for most users of the ResourceManager | ||
/// - call GetString() or GetObject() instead. The parameters let you control whether the ResourceSet is created if it | ||
/// hasn't yet been loaded and if parent CultureInfos should be loaded as well for resource inheritance. | ||
/// </summary> | ||
/// <param name="culture"></param> | ||
/// <param name="createIfNotExists"></param> | ||
/// <param name="tryParents"></param> | ||
/// <returns></returns> | ||
public static ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents) | ||
{ | ||
return _resourceManagerWrapper.GetResourceSet (culture, createIfNotExists, tryParents)!; | ||
} | ||
|
||
/// <summary> | ||
/// Looks up a set of resources for a particular CultureInfo. This is not useful for most users of the ResourceManager | ||
/// - call GetString() or GetObject() instead. The parameters let you control whether the ResourceSet is created if it | ||
/// hasn't yet been loaded and if parent CultureInfos should be loaded as well for resource inheritance. Allows | ||
/// filtering of resources. | ||
/// </summary> | ||
/// <param name="culture"></param> | ||
/// <param name="createIfNotExists"></param> | ||
/// <param name="tryParents"></param> | ||
/// <param name="filter"></param> | ||
/// <returns></returns> | ||
public static ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents, Func<DictionaryEntry, bool>? filter) | ||
{ | ||
return _resourceManagerWrapper.GetResourceSet (culture, createIfNotExists, tryParents, filter)!; | ||
} | ||
|
||
/// <summary> | ||
/// Looks up a resource value for a particular name. Looks in the specified CultureInfo, and if not found, all parent | ||
/// CultureInfos. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <param name="culture"></param> | ||
/// <returns>Null if the resource was not found in the current culture or the invariant culture.</returns> | ||
public static string GetString (string name, CultureInfo? culture = null!) { return _resourceManagerWrapper.GetString (name, culture); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#nullable enable | ||
|
||
using System.Collections; | ||
using System.Globalization; | ||
using System.Resources; | ||
|
||
namespace Terminal.Gui.Resources; | ||
|
||
internal class ResourceManagerWrapper (ResourceManager resourceManager) | ||
{ | ||
private readonly ResourceManager _resourceManager = resourceManager ?? throw new ArgumentNullException (nameof (resourceManager)); | ||
|
||
// Optionally, expose other ResourceManager methods as needed | ||
public object GetObject (string name, CultureInfo culture = null!) | ||
{ | ||
object value = _resourceManager.GetObject (name, culture)!; | ||
|
||
if (Equals (culture, CultureInfo.InvariantCulture)) | ||
{ | ||
return value; | ||
} | ||
|
||
if (value is null) | ||
{ | ||
value = _resourceManager.GetObject (name, CultureInfo.InvariantCulture)!; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
public ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents) | ||
{ | ||
ResourceSet value = _resourceManager.GetResourceSet (culture, createIfNotExists, tryParents)!; | ||
|
||
if (Equals (culture, CultureInfo.InvariantCulture)) | ||
{ | ||
return value; | ||
} | ||
|
||
if (value!.Cast<DictionaryEntry> ().Any ()) | ||
{ | ||
value = _resourceManager.GetResourceSet (CultureInfo.InvariantCulture, createIfNotExists, tryParents)!; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
public ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents, Func<DictionaryEntry, bool>? filter) | ||
{ | ||
ResourceSet value = _resourceManager.GetResourceSet (culture, createIfNotExists, tryParents)!; | ||
|
||
IEnumerable<DictionaryEntry> filteredEntries = value.Cast<DictionaryEntry> ().Where (filter ?? (_ => true)); | ||
|
||
ResourceSet? filteredValue = ConvertToResourceSet (filteredEntries); | ||
|
||
if (Equals (culture, CultureInfo.InvariantCulture)) | ||
{ | ||
return filteredValue; | ||
} | ||
|
||
if (!filteredValue!.Cast<DictionaryEntry> ().Any ()) | ||
{ | ||
filteredValue = GetResourceSet (CultureInfo.InvariantCulture, createIfNotExists, tryParents, filter)!; | ||
} | ||
|
||
return filteredValue; | ||
} | ||
|
||
public string GetString (string name, CultureInfo? culture = null!) | ||
{ | ||
// Attempt to get the string for the specified culture | ||
string value = _resourceManager.GetString (name, culture)!; | ||
|
||
// If it's already using the invariant culture return | ||
if (Equals (culture, CultureInfo.InvariantCulture)) | ||
{ | ||
return value; | ||
} | ||
|
||
// If the string is empty or null, fall back to the invariant culture | ||
if (string.IsNullOrEmpty (value)) | ||
{ | ||
value = _resourceManager.GetString (name, CultureInfo.InvariantCulture)!; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private static ResourceSet? ConvertToResourceSet (IEnumerable<DictionaryEntry> entries) | ||
{ | ||
using var memoryStream = new MemoryStream (); | ||
|
||
using var resourceWriter = new ResourceWriter (memoryStream); | ||
|
||
// Add each DictionaryEntry to the ResourceWriter | ||
foreach (DictionaryEntry entry in entries) | ||
{ | ||
resourceWriter.AddResource ((string)entry.Key, entry.Value); | ||
} | ||
|
||
// Finish writing to the stream | ||
resourceWriter.Generate (); | ||
|
||
// Reset the stream position to the beginning | ||
memoryStream.Position = 0; | ||
|
||
// Create a ResourceSet from the MemoryStream | ||
var resourceSet = new ResourceSet (memoryStream); | ||
|
||
return resourceSet; | ||
} | ||
} |
Oops, something went wrong.