1
- using System . Globalization ;
1
+ using System ;
2
2
using System . Linq ;
3
3
using Abp . AspNetCore . EmbeddedResources ;
4
4
using Abp . AspNetCore . Localization ;
5
- using Abp . AspNetCore . Mvc . Views ;
6
5
using Abp . Dependency ;
7
6
using Abp . Localization ;
8
7
using Castle . LoggingFacility . MsLogging ;
8
+ using JetBrains . Annotations ;
9
9
using Microsoft . AspNetCore . Builder ;
10
10
using Microsoft . AspNetCore . Localization ;
11
11
using Microsoft . Extensions . DependencyInjection ;
12
12
using Microsoft . Extensions . Logging ;
13
13
14
14
namespace Abp . AspNetCore
15
15
{
16
- public static class AbpApplicationBuilderExtensions
16
+ public static class AbpApplicationBuilderExtensions
17
17
{
18
18
public static void UseAbp ( this IApplicationBuilder app )
19
19
{
20
- AddCastleLoggerFactory ( app ) ;
20
+ app . UseAbp ( null ) ;
21
+ }
21
22
22
- InitializeAbp ( app ) ;
23
+ public static void UseAbp ( [ NotNull ] this IApplicationBuilder app , Action < AbpApplicationBuilderOptions > optionsAction )
24
+ {
25
+ Check . NotNull ( app , nameof ( app ) ) ;
23
26
24
- ConfigureRequestLocalization ( app ) ;
25
- }
27
+ var options = new AbpApplicationBuilderOptions ( ) ;
28
+ optionsAction ? . Invoke ( options ) ;
29
+
30
+ if ( options . UseCastleLoggerFactory )
31
+ {
32
+ app . UseCastleLoggerFactory ( ) ;
33
+ }
34
+
35
+ InitializeAbp ( app ) ;
26
36
27
- public static void UseEmbeddedFiles ( this IApplicationBuilder app )
37
+ if ( options . UseAbpRequestLocalization )
38
+ {
39
+ //TODO: This should be added later than authorization middleware!
40
+ app . UseAbpRequestLocalization ( ) ;
41
+ }
42
+ }
43
+
44
+ public static void UseEmbeddedFiles ( this IApplicationBuilder app )
28
45
{
29
- //TODO: Can improve it or create a custom middleware?
30
46
app . UseStaticFiles (
31
47
new StaticFileOptions
32
48
{
@@ -43,7 +59,7 @@ private static void InitializeAbp(IApplicationBuilder app)
43
59
abpBootstrapper . Initialize ( ) ;
44
60
}
45
61
46
- private static void AddCastleLoggerFactory ( IApplicationBuilder app )
62
+ public static void UseCastleLoggerFactory ( this IApplicationBuilder app )
47
63
{
48
64
var castleLoggerFactory = app . ApplicationServices . GetService < Castle . Core . Logging . ILoggerFactory > ( ) ;
49
65
if ( castleLoggerFactory == null )
@@ -56,35 +72,32 @@ private static void AddCastleLoggerFactory(IApplicationBuilder app)
56
72
. AddCastleLogger ( castleLoggerFactory ) ;
57
73
}
58
74
59
- private static void ConfigureRequestLocalization ( IApplicationBuilder app )
75
+ public static void UseAbpRequestLocalization ( this IApplicationBuilder app , Action < RequestLocalizationOptions > optionsAction = null )
60
76
{
61
77
var iocResolver = app . ApplicationServices . GetRequiredService < IIocResolver > ( ) ;
62
78
using ( var languageManager = iocResolver . ResolveAsDisposable < ILanguageManager > ( ) )
63
79
{
64
- var defaultLanguage = languageManager . Object
65
- . GetLanguages ( )
66
- . FirstOrDefault ( l => l . IsDefault ) ;
67
-
68
- if ( defaultLanguage == null )
69
- {
70
- return ;
71
- }
72
-
73
80
var supportedCultures = languageManager . Object
74
81
. GetLanguages ( )
75
82
. Select ( l => CultureInfoHelper . Get ( l . Name ) )
76
83
. ToArray ( ) ;
77
84
78
- var defaultCulture = new RequestCulture ( defaultLanguage . Name ) ;
79
-
80
85
var options = new RequestLocalizationOptions
81
86
{
82
- DefaultRequestCulture = defaultCulture ,
83
87
SupportedCultures = supportedCultures ,
84
88
SupportedUICultures = supportedCultures
85
89
} ;
86
90
91
+ var userProvider = new UserRequestCultureProvider ( ) ;
92
+
87
93
options . RequestCultureProviders . Insert ( 0 , new AbpLocalizationHeaderRequestCultureProvider ( ) ) ;
94
+ options . RequestCultureProviders . Insert ( 2 , userProvider ) ;
95
+ options . RequestCultureProviders . Insert ( 4 , new DefaultRequestCultureProvider ( ) ) ;
96
+
97
+ optionsAction ? . Invoke ( options ) ;
98
+
99
+ //TODO: Find cookie provider, set to UserRequestCultureProvider to set user's setting!!!
100
+ userProvider . CookieProvider = options . RequestCultureProviders . FirstOrDefault ( p => p is CookieRequestCultureProvider ) ;
88
101
89
102
app . UseRequestLocalization ( options ) ;
90
103
}
0 commit comments