This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
/
JsonLocalizerServiceExtension.cs
83 lines (73 loc) · 3.46 KB
/
JsonLocalizerServiceExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Askmethat.Aspnet.JsonLocalizer.Localizer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System;
using Askmethat.Aspnet.JsonLocalizer.JsonOptions;
namespace Askmethat.Aspnet.JsonLocalizer.Extensions
{
/// <summary>
/// Extension method for setting up services in a <see cref="IServiceCollection" />.
/// </summary>
public static class JsonLocalizerServiceExtension
{
/// <summary>
/// Add JsonLocalization to asp.net service collection
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddJsonLocalization(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
_ = services.AddOptions();
AddJsonLocalizationServices(services);
return services;
}
/// <summary>
/// Add JsonLocalization to asp.net service collection and localization options
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddJsonLocalization(
this IServiceCollection services,
Action<JsonLocalizationOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
Console.Error.WriteLine("Setup Action seems to be null, The localization options will not be override. For any helps create an issue at ");
AddJsonLocalizationServices(services);
}
//IWebHostEnvironment serverEnv, IWebAssemblyHostEnvironment clientEnv
AddJsonLocalizationServices(services, setupAction);
return services;
}
internal static void AddJsonLocalizationServices(IServiceCollection services)
{
_ = services.AddMemoryCache();
_ = services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>();
_ = services.AddScoped<IJsonStringLocalizer, JsonStringLocalizer>();
_ = services.AddScoped(typeof(IJsonStringLocalizer<>), typeof(JsonStringLocalizerOfT<>));
_ = services.AddScoped<IStringLocalizer, JsonStringLocalizer>();
_ = services.AddScoped(typeof(IStringLocalizer<>), typeof(JsonStringLocalizerOfT<>));
_ = services.AddSingleton<EnvironmentWrapper>();
}
/// <summary>
/// If user want to override default localization options
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to</param>
/// <param name="setupAction">The <see cref="LocalizationOptions"/> option to change default localization mecanism</param>
internal static void AddJsonLocalizationServices(
IServiceCollection services,
Action<JsonLocalizationOptions> setupAction)
{
AddJsonLocalizationServices(services);
_ = services.Configure(setupAction);
}
}
}