Skip to content

Commit 0e183d2

Browse files
committed
Add docs
1 parent f6e4d48 commit 0e183d2

14 files changed

+244
-4
lines changed

src/My.Extensions.Localization.Json/Caching/IResourceNamesCache.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33

44
namespace My.Extensions.Localization.Json.Caching;
55

6+
/// <summary>
7+
/// Defines a cache for storing and retrieving collections of resource names by key.
8+
/// </summary>
69
public interface IResourceNamesCache
710
{
11+
/// <summary>
12+
/// Gets the list of strings associated with the specified name, or adds a new list using the provided factory if
13+
/// none exists.
14+
/// </summary>
15+
/// <param name="name">The key used to locate the associated list of strings. Cannot be null.</param>
16+
/// <param name="valueFactory">A function that generates a new list of strings if the specified name does not exist. Cannot be null.</param>
17+
/// <returns>The list of strings associated with the specified name. If the name was not present, returns the newly created
18+
/// list from the factory.</returns>
819
IList<string> GetOrAdd(string name, Func<string, IList<string>> valueFactory);
920
}

src/My.Extensions.Localization.Json/Caching/ResourceNamesCache.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
namespace My.Extensions.Localization.Json.Caching;
66

7+
/// <summary>
8+
/// Provides a thread-safe cache for storing and retrieving lists of resource names by key.
9+
/// </summary>
710
public class ResourceNamesCache : IResourceNamesCache
811
{
912
private readonly ConcurrentDictionary<string, IList<string>> _cache = new();
1013

14+
/// <inheritdoc />
1115
public IList<string> GetOrAdd(string name, Func<string, IList<string>> valueFactory)
1216
=> _cache.GetOrAdd(name, valueFactory);
1317
}

src/My.Extensions.Localization.Json/Internal/IResourceStringProvider.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33

44
namespace My.Extensions.Localization.Json.Internal;
55

6+
/// <summary>
7+
/// Defines a provider for retrieving all resource strings for a specified culture.
8+
/// </summary>
69
public interface IResourceStringProvider
710
{
11+
/// <summary>
12+
/// Retrieves all resource strings for the specified culture.
13+
/// </summary>
14+
/// <param name="culture">The culture for which to retrieve resource strings. Cannot be null.</param>
15+
/// <param name="throwOnMissing">Specifies whether to throw an exception if resource strings for the specified culture are missing. If <see
16+
/// langword="true"/>, an exception is thrown when resources are not found; otherwise, an empty list is returned.</param>
17+
/// <returns>A list of resource strings associated with the specified culture. The list is empty if no resources are found
18+
/// and <paramref name="throwOnMissing"/> is <see langword="false"/>.</returns>
819
IList<string> GetAllResourceStrings(CultureInfo culture, bool throwOnMissing);
920
}

src/My.Extensions.Localization.Json/Internal/JsonFileWatcher.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace My.Extensions.Localization.Json.Internal;
55

6+
/// <summary>
7+
/// Provides a mechanism for monitoring changes to JSON files within a specified directory.
8+
/// </summary>
69
public class JsonFileWatcher : IDisposable
710
{
811
private const string JsonExtension = "*.json";
@@ -11,8 +14,16 @@ public class JsonFileWatcher : IDisposable
1114

1215
private readonly FileSystemWatcher _filesWatcher;
1316

17+
/// <summary>
18+
/// Occurs when a file or directory in the specified path is changed.
19+
/// </summary>
1420
public event FileSystemEventHandler Changed;
1521

22+
/// <summary>
23+
/// Initializes a new instance of the JsonFileWatcher class to monitor changes to JSON files in the specified
24+
/// directory.
25+
/// </summary>
26+
/// <param name="rootDirectory">The path to the directory to monitor for changes to JSON files. Must be a valid directory path.</param>
1627
public JsonFileWatcher(string rootDirectory)
1728
{
1829
_filesWatcher = new(rootDirectory)
@@ -24,17 +35,26 @@ public JsonFileWatcher(string rootDirectory)
2435
_filesWatcher.Changed += (s, e) => Changed?.Invoke(s, e);
2536
}
2637

38+
/// <summary>
39+
/// Finalizes the JsonFileWatcher instance and releases unmanaged resources before the object is reclaimed by
40+
/// garbage collection.
41+
/// </summary>
2742
~JsonFileWatcher()
2843
{
2944
Dispose(false);
3045
}
3146

47+
/// <inheritdoc />
3248
public void Dispose()
3349
{
3450
Dispose(true);
3551
GC.SuppressFinalize(true);
3652
}
3753

54+
/// <summary>
55+
/// Releases the unmanaged resources used by the object and optionally releases the managed resources.
56+
/// </summary>
57+
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
3858
public virtual void Dispose(bool disposing)
3959
{
4060
if (_disposed)

src/My.Extensions.Localization.Json/Internal/JsonResourceLoader.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace My.Extensions.Localization.Json.Internal;
66

7+
/// <summary>
8+
/// Provides functionality to load string resources from a JSON file into a dictionary, using dot notation for nested
9+
/// keys.
10+
/// </summary>
711
public static class JsonResourceLoader
812
{
913
private static readonly JsonDocumentOptions _jsonDocumentOptions = new()
@@ -12,7 +16,12 @@ public static class JsonResourceLoader
1216
AllowTrailingCommas = true,
1317
};
1418

15-
19+
/// <summary>
20+
/// Loads key-value pairs from a JSON resource file at the specified path.
21+
/// </summary>
22+
/// <param name="filePath">The path to the JSON file containing resource definitions. Must refer to an existing file.</param>
23+
/// <returns>A dictionary containing resource keys and their corresponding values parsed from the file. Returns an empty
24+
/// dictionary if the file does not exist or contains no resources.</returns>
1625
public static IDictionary<string, string> Load(string filePath)
1726
{
1827
var resources = new Dictionary<string, string>();

src/My.Extensions.Localization.Json/Internal/JsonResourceManager.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,35 @@
77

88
namespace My.Extensions.Localization.Json.Internal;
99

10+
/// <summary>
11+
/// Provides access to localized string resources loaded from JSON files, supporting culture-specific lookups and
12+
/// optional fallback to parent UI cultures.
13+
/// </summary>
1014
public class JsonResourceManager
1115
{
1216
private readonly List<JsonFileWatcher> _jsonFileWatchers = [];
1317
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, string>> _resourcesCache = new();
1418
private readonly ConcurrentDictionary<string, HashSet<string>> _loadedFilesCache = new();
1519

20+
/// <summary>
21+
/// Initializes a new instance of the JsonResourceManager class using the specified resource directory and optional
22+
/// resource name.
23+
/// </summary>
24+
/// <param name="resourcesPath">The path to the directory containing the JSON resource files. Cannot be null or empty.</param>
25+
/// <param name="resourceName">The name of the resource to load. If null, the default resource name is used.</param>
1626
public JsonResourceManager(string resourcesPath, string resourceName = null)
1727
: this([resourcesPath], fallBackToParentUICultures: true, resourceName)
1828
{
1929
}
2030

31+
/// <summary>
32+
/// Initializes a new instance of the JsonResourceManager class using the specified resource file paths and
33+
/// configuration options.
34+
/// </summary>
35+
/// <param name="resourcesPaths">An array of file system paths to JSON resource files to be managed. If null, an empty array is used.</param>
36+
/// <param name="fallBackToParentUICultures">Indicates whether resource lookups should fall back to parent UI cultures when a resource is not found for the
37+
/// requested culture.</param>
38+
/// <param name="resourceName">The name of the resource to be managed. If null, the manager will use the default resource name resolution.</param>
2139
public JsonResourceManager(string[] resourcesPaths, bool fallBackToParentUICultures, string resourceName = null)
2240
{
2341
ResourcesPaths = resourcesPaths ?? Array.Empty<string>();
@@ -30,15 +48,31 @@ public JsonResourceManager(string[] resourcesPaths, bool fallBackToParentUICultu
3048
}
3149
}
3250

51+
/// <summary>
52+
/// Initializes a new instance of the JsonResourceManager class using the specified resource file paths and an
53+
/// optional resource name.
54+
/// </summary>
55+
/// <param name="resourcesPaths">An array of file system paths to JSON resource files to be managed. Each path should point to a valid resource
56+
/// file. Cannot be null.</param>
57+
/// <param name="resourceName">The name of the resource to be used for lookups. If null, the default resource name will be used.</param>
3358
public JsonResourceManager(string[] resourcesPaths, string resourceName = null)
3459
: this(resourcesPaths, fallBackToParentUICultures: true, resourceName)
3560
{
3661
}
3762

63+
/// <summary>
64+
/// Gets the name of the resource associated with this instance.
65+
/// </summary>
3866
public string ResourceName { get; }
3967

68+
/// <summary>
69+
/// Gets the collection of file system paths to resource files associated with the current instance.
70+
/// </summary>
4071
public string[] ResourcesPaths { get; }
4172

73+
/// <summary>
74+
/// Gets the file path to the resources file used by the application.
75+
/// </summary>
4276
public string ResourcesFilePath { get; private set; }
4377

4478
/// <summary>
@@ -47,6 +81,15 @@ public JsonResourceManager(string[] resourcesPaths, string resourceName = null)
4781
/// </summary>
4882
public bool FallBackToParentUICultures { get; }
4983

84+
/// <summary>
85+
/// Retrieves the set of localized resources for the specified culture, optionally including resources from parent
86+
/// cultures.
87+
/// </summary>
88+
/// <param name="culture">The culture for which to retrieve the resource set. This determines which localized resources are returned.</param>
89+
/// <param name="tryParents">If <see langword="true"/>, resources from parent cultures are included in the result; otherwise, only resources
90+
/// for the specified culture are returned.</param>
91+
/// <returns>A <see cref="ConcurrentDictionary{string, string}"/> containing the resources for the specified culture, or <see
92+
/// langword="null"/> if no resources are available for that culture.</returns>
5093
public virtual ConcurrentDictionary<string, string> GetResourceSet(CultureInfo culture, bool tryParents)
5194
{
5295
TryLoadResourceSet(culture);
@@ -84,6 +127,11 @@ public virtual ConcurrentDictionary<string, string> GetResourceSet(CultureInfo c
84127
}
85128
}
86129

130+
/// <summary>
131+
/// Retrieves the localized string resource associated with the specified name for the current UI culture.
132+
/// </summary>
133+
/// <param name="name">The name of the resource to retrieve. This value is case-sensitive and must not be null.</param>
134+
/// <returns>The localized string value if found; otherwise, null.</returns>
87135
public virtual string GetString(string name)
88136
{
89137
var culture = CultureInfo.CurrentUICulture;
@@ -116,6 +164,13 @@ public virtual string GetString(string name)
116164
return null;
117165
}
118166

167+
/// <summary>
168+
/// Retrieves the localized string resource associated with the specified name and culture.
169+
/// </summary>
170+
/// <param name="name">The name of the resource to retrieve. This value is case-sensitive and must not be null.</param>
171+
/// <param name="culture">The culture for which the resource should be retrieved. If the resource is not found for this culture and parent
172+
/// culture fallback is enabled, parent cultures will be searched.</param>
173+
/// <returns>The localized string value for the specified resource name and culture, or null if the resource is not found.</returns>
119174
public virtual string GetString(string name, CultureInfo culture)
120175
{
121176
GetResourceSet(culture, tryParents: FallBackToParentUICultures);

src/My.Extensions.Localization.Json/Internal/JsonStringProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
namespace My.Extensions.Localization.Json.Internal;
77

8+
/// <summary>
9+
/// Provides resource string retrieval for JSON-based resource sets, supporting culture-specific access and caching of
10+
/// resource names.
11+
/// </summary>
12+
/// <param name="resourceNamesCache">A cache used to store and retrieve lists of resource names for specific cultures, improving performance by avoiding
13+
/// repeated resource enumeration.</param>
14+
/// <param name="jsonResourceManager">The resource manager responsible for accessing JSON resource sets and their associated strings for a given culture.</param>
815
public class JsonStringProvider(IResourceNamesCache resourceNamesCache, JsonResourceManager jsonResourceManager) : IResourceStringProvider
916
{
1017
private string GetResourceCacheKey(CultureInfo culture)
@@ -14,6 +21,7 @@ private string GetResourceCacheKey(CultureInfo culture)
1421
return $"Culture={culture.Name};resourceName={resourceName}";
1522
}
1623

24+
/// <inheritdoc />
1725
public IList<string> GetAllResourceStrings(CultureInfo culture, bool throwOnMissing)
1826
{
1927
var cacheKey = GetResourceCacheKey(culture);

src/My.Extensions.Localization.Json/Internal/PathHelpers.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44
namespace My.Extensions.Localization.Json.Internal;
55

6+
/// <summary>
7+
/// Provides helper methods for working with file system paths related to the application's location.
8+
/// </summary>
69
public static class PathHelpers
710
{
11+
/// <summary>
12+
/// Gets the root directory of the currently executing application.
13+
/// </summary>
14+
/// <returns>A string containing the full path to the application's root directory. Returns null if the directory cannot be
15+
/// determined.</returns>
816
public static string GetApplicationRoot() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
917
}

src/My.Extensions.Localization.Json/JsonLocalizationOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22

33
namespace My.Extensions.Localization.Json;
44

5+
/// <summary>
6+
/// Provides configuration options for JSON-based localization, including resource type and resource path settings.
7+
/// </summary>
58
public class JsonLocalizationOptions : LocalizationOptions
69
{
10+
/// <summary>
11+
/// Gets or sets the strategy used to determine how resources are categorized or accessed.
12+
/// </summary>
713
public ResourcesType ResourcesType { get; set; } = ResourcesType.TypeBased;
814

15+
/// <summary>
16+
/// Gets or sets the collection of file system paths to resource directories used by the application.
17+
/// </summary>
918
public new string[] ResourcesPath { get; set; } = [];
1019
}

src/My.Extensions.Localization.Json/JsonLocalizationServiceCollectionExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66

77
namespace Microsoft.Extensions.DependencyInjection;
88

9+
/// <summary>
10+
/// Provides extension methods for registering JSON-based localization services with an <see
11+
/// cref="IServiceCollection"/>.
12+
/// </summary>
913
public static class JsonLocalizationServiceCollectionExtensions
1014
{
15+
/// <summary>
16+
/// Adds JSON-based localization services to the specified service collection.
17+
/// </summary>
18+
/// <param name="services">The service collection to which the JSON localization services will be added. Cannot be null.</param>
19+
/// <returns>The same service collection instance, with JSON localization services registered.</returns>
1120
public static IServiceCollection AddJsonLocalization(this IServiceCollection services)
1221
{
1322
ArgumentNullException.ThrowIfNull(services);
@@ -19,6 +28,12 @@ public static IServiceCollection AddJsonLocalization(this IServiceCollection ser
1928
return services;
2029
}
2130

31+
/// <summary>
32+
/// Adds JSON-based localization services to the specified service collection.
33+
/// </summary>
34+
/// <param name="services">The service collection to which the JSON localization services will be added. Cannot be null.</param>
35+
/// <param name="setupAction">An action to configure the JSON localization options. Cannot be null.</param>
36+
/// <returns>The same instance of <see cref="IServiceCollection"/> with JSON localization services registered.</returns>
2237
public static IServiceCollection AddJsonLocalization(this IServiceCollection services, Action<JsonLocalizationOptions> setupAction)
2338
{
2439
ArgumentNullException.ThrowIfNull(services);

0 commit comments

Comments
 (0)