Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ThreadCultureChange.cs « System « TestUtilities « tests « Common « libraries « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee799eb87f8566bfea0a9e911daae0a02b2a62b5 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Globalization;

namespace System.Tests
{
    public sealed class ThreadCultureChange : IDisposable
    {
        private readonly CultureInfo _origCulture = CultureInfo.CurrentCulture;
        private readonly CultureInfo _origUICulture = CultureInfo.CurrentUICulture;

        public ThreadCultureChange(string? cultureName) :
            this(cultureName != null ? new CultureInfo(cultureName) : null)
        {
        }

        public ThreadCultureChange(CultureInfo? newCulture) :
            this(newCulture, null)
        {
        }

        public ThreadCultureChange(CultureInfo? newCulture, CultureInfo? newUICulture)
        {
            if (newCulture != null)
            {
                _origCulture = CultureInfo.CurrentCulture;
                CultureInfo.CurrentCulture = newCulture;
            }

            if (newUICulture != null)
            {
                _origUICulture = CultureInfo.CurrentUICulture;
                CultureInfo.CurrentUICulture = newUICulture;
            }
        }

        public void Dispose()
        {
            CultureInfo.CurrentCulture = _origCulture;
            CultureInfo.CurrentUICulture = _origUICulture;
        }
    }
}