Class LocalizationServiceCollectionExtensions
- Namespace
- NuvTools.AspNetCore.Localization
- Assembly
- NuvTools.AspNetCore.dll
Provides extension methods for configuring composite localization services.
public static class LocalizationServiceCollectionExtensions
- Inheritance
-
LocalizationServiceCollectionExtensions
- Inherited Members
Methods
AddCompositeLocalizer(IServiceCollection, IDictionary<string, Type>?, IEnumerable<Type>?)
Adds the composite localizer to the service collection, enabling multiple localization sources with prefix-based routing and fallback resolution.
public static IServiceCollection AddCompositeLocalizer(this IServiceCollection services, IDictionary<string, Type>? namedResourceTypes = null, IEnumerable<Type>? unnamedResourceTypes = null)
Parameters
servicesIServiceCollectionThe IServiceCollection to add services to.
namedResourceTypesIDictionary<string, Type>A dictionary of named resource types where the key is the prefix name (e.g., "Errors") and the value is the resource type. Named localizers can be targeted using prefix syntax (e.g., "Errors:ValidationFailed").
unnamedResourceTypesIEnumerable<Type>A collection of resource types to be used as fallback localizers. These are searched when a key is not found in named localizers.
Returns
- IServiceCollection
The IServiceCollection so that additional calls can be chained.
Examples
services.AddCompositeLocalizer(
namedResourceTypes: new Dictionary<string, Type>
{
["Errors"] = typeof(ErrorResources),
["Messages"] = typeof(MessageResources)
},
unnamedResourceTypes: new[] { typeof(SharedResources) }
);
Remarks
This method registers all necessary IStringLocalizer<T> instances and a scoped CompositeLocalizer that combines them.
Named resource types take precedence when using prefix syntax. Unnamed resource types are used for fallback resolution when a key is not found in any named localizer.
Duplicate types between named and unnamed collections are automatically handled, with named registrations taking precedence.