70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
// 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
|
|
{
|
|
/// <summary>
|
|
/// Extension methods for setting up localization services in an <see cref="IServiceCollection" />.
|
|
/// </summary>
|
|
public static class LocalizationServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds services required for application localization.
|
|
/// </summary>
|
|
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
|
public static void AddLocalization(this IServiceCollection services)
|
|
{
|
|
if (services == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(services));
|
|
}
|
|
|
|
services.AddOptions();
|
|
|
|
AddLocalizationServices(services);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds services required for application localization.
|
|
/// </summary>
|
|
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
|
/// <param name="setupAction">
|
|
/// An <see cref="Action{LocalizationOptions}"/> to configure the <see cref="LocalizationOptions"/>.
|
|
/// </param>
|
|
public static void AddLocalization(
|
|
this IServiceCollection services,
|
|
Action<LocalizationOptions> 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<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>();
|
|
services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
|
|
}
|
|
|
|
internal static void AddLocalizationServices(
|
|
IServiceCollection services,
|
|
Action<LocalizationOptions> setupAction)
|
|
{
|
|
AddLocalizationServices(services);
|
|
services.Configure(setupAction);
|
|
}
|
|
}
|
|
} |