diff options
Diffstat (limited to 'src/libraries/Common/tests/TestUtilities/System/ThreadCultureChange.cs')
-rw-r--r-- | src/libraries/Common/tests/TestUtilities/System/ThreadCultureChange.cs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/libraries/Common/tests/TestUtilities/System/ThreadCultureChange.cs b/src/libraries/Common/tests/TestUtilities/System/ThreadCultureChange.cs new file mode 100644 index 00000000000..ee799eb87f8 --- /dev/null +++ b/src/libraries/Common/tests/TestUtilities/System/ThreadCultureChange.cs @@ -0,0 +1,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; + } + } +} |