// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Localization; namespace Microsoft.Extensions.DependencyInjection { /// /// Extension methods for setting up localization services in an . /// public static class LocalizationServiceCollectionExtensions { /// /// Adds services required for application localization. /// /// The to add the services to. public static void AddLocalization(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); AddLocalizationServices(services); } /// /// Adds services required for application localization. /// /// The to add the services to. /// /// An to configure the . /// public static void AddLocalization( this IServiceCollection services, Action setupAction) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (setupAction == null) { throw new ArgumentNullException(nameof(setupAction)); } AddLocalizationServices(services, setupAction); } // To enable unit testing internal static void AddLocalizationServices(IServiceCollection services) { services.TryAddSingleton(); services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>)); } internal static void AddLocalizationServices( IServiceCollection services, Action setupAction) { AddLocalizationServices(services); services.Configure(setupAction); } } }