/
CaptureCultureAttribute.cs
163 lines (142 loc) · 5.87 KB
/
CaptureCultureAttribute.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// This file is part of Hangfire. Copyright © 2013-2014 Hangfire OÜ.
//
// Hangfire is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3
// of the License, or any later version.
//
// Hangfire is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Globalization;
using Hangfire.Annotations;
using Hangfire.Client;
using Hangfire.Common;
using Hangfire.Logging;
using Hangfire.Server;
namespace Hangfire
{
public sealed class CaptureCultureAttribute : JobFilterAttribute, IClientFilter, IServerFilter
{
private readonly ILog _logger = LogProvider.GetLogger(typeof(CaptureCultureAttribute));
public CaptureCultureAttribute() : this(null)
{
}
public CaptureCultureAttribute([CanBeNull] string defaultCultureName, bool captureDefault = true)
: this(defaultCultureName, defaultCultureName, captureDefault)
{
}
public CaptureCultureAttribute(
[CanBeNull] string defaultCultureName,
[CanBeNull] string defaultUICultureName,
bool captureDefault = true)
{
DefaultCultureName = defaultCultureName;
DefaultUICultureName = defaultUICultureName;
CaptureDefault = captureDefault;
}
[CanBeNull]
public string DefaultCultureName { get; }
[CanBeNull]
public string DefaultUICultureName { get; }
public bool CaptureDefault { get; }
public void OnCreating(CreatingContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
var currentCulture = CultureInfo.CurrentCulture;
var currentUICulture = CultureInfo.CurrentUICulture;
if (CaptureDefault == false && currentCulture.Name.Equals(DefaultCultureName, StringComparison.Ordinal))
{
// Don't set the 'CurrentCulture' job parameter when it's equal to the default one
}
else
{
context.SetJobParameter("CurrentCulture", currentCulture.Name);
}
if (CaptureDefault == false && currentUICulture.Name.Equals(DefaultUICultureName, StringComparison.Ordinal))
{
// Don't set the 'CurrentUICulture' job parameter when it's equal to the default one
}
else if (GlobalConfiguration.HasCompatibilityLevel(CompatibilityLevel.Version_180) &&
currentUICulture.Equals(currentCulture))
{
// Don't set the 'CurrentUICulture' when it's the same as 'CurrentCulture' under
// CompatibilityLevel.Version_180
}
else
{
context.SetJobParameter("CurrentUICulture", currentUICulture.Name);
}
}
public void OnCreated(CreatedContext context)
{
}
public void OnPerforming(PerformingContext context)
{
var cultureName = context.GetJobParameter<string>("CurrentCulture", allowStale: true);
var uiCultureName = context.GetJobParameter<string>("CurrentUICulture", allowStale: true) ?? cultureName;
cultureName = cultureName ?? DefaultCultureName;
uiCultureName = uiCultureName ?? DefaultUICultureName;
try
{
if (cultureName != null)
{
context.Items["PreviousCulture"] = CultureInfo.CurrentCulture;
SetCurrentCulture(new CultureInfo(cultureName));
}
}
catch (CultureNotFoundException ex)
{
// TODO: Make this overridable, and start with throwing an exception
_logger.WarnException($"Unable to set CurrentCulture for job {context.BackgroundJob.Id} due to an exception", ex);
}
try
{
if (uiCultureName != null)
{
context.Items["PreviousUICulture"] = CultureInfo.CurrentUICulture;
SetCurrentUICulture(new CultureInfo(uiCultureName));
}
}
catch (CultureNotFoundException ex)
{
// TODO: Make this overridable, and start with throwing an exception
_logger.WarnException($"Unable to set CurrentUICulture for job {context.BackgroundJob.Id} due to an exception", ex);
}
}
public void OnPerformed(PerformedContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (context.Items.TryGetValue("PreviousCulture", out var culture))
{
SetCurrentCulture((CultureInfo)culture);
}
if (context.Items.TryGetValue("PreviousUICulture", out var uiCulture))
{
SetCurrentUICulture((CultureInfo)uiCulture);
}
}
private static void SetCurrentCulture(CultureInfo value)
{
#if !NETSTANDARD1_3
System.Threading.Thread.CurrentThread.CurrentCulture = value;
#else
CultureInfo.CurrentCulture = value;
#endif
}
// ReSharper disable once InconsistentNaming
private static void SetCurrentUICulture(CultureInfo value)
{
#if !NETSTANDARD1_3
System.Threading.Thread.CurrentThread.CurrentUICulture = value;
#else
CultureInfo.CurrentUICulture = value;
#endif
}
}
}