This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
ApplicationBuilderExtensions.cs
122 lines (107 loc) · 4.84 KB
/
ApplicationBuilderExtensions.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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.AspNetCore.Localization;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Extension methods for adding the <see cref="RequestLocalizationMiddleware"/> to an application.
/// </summary>
public static class ApplicationBuilderExtensions
{
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseMiddleware<RequestLocalizationMiddleware>();
}
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="options">The <see cref="RequestLocalizationOptions"/> to configure the middleware with.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
RequestLocalizationOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options));
}
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="optionsAction"></param>
/// <remarks>
/// This will going to instantiate a new <see cref="RequestLocalizationOptions"/> that doesn't come from the services.
/// </remarks>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
Action<RequestLocalizationOptions> optionsAction)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (optionsAction == null)
{
throw new ArgumentNullException(nameof(optionsAction));
}
var options = new RequestLocalizationOptions();
optionsAction.Invoke(options);
return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options));
}
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="cultures">The culture names to be added by the application, which is represents both supported cultures and UI cultures.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
/// <remarks>
/// Note that the first culture is the default culture name.
/// </remarks>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
params string[] cultures)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (cultures == null)
{
throw new ArgumentNullException(nameof(cultures));
}
if (cultures.Length == 0)
{
throw new ArgumentException(Resources.Exception_CulturesShouldNotBeEmpty);
}
var options = new RequestLocalizationOptions()
.AddSupportedCultures(cultures)
.AddSupportedUICultures(cultures)
.SetDefaultCulture(cultures[0]);
return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options));
}
}
}