Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created January 23, 2014 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoehls/8583576 to your computer and use it in GitHub Desktop.
Save jpoehls/8583576 to your computer and use it in GitHub Desktop.
Execute .NET unit test (or any) code under a specific culture.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Makes it easy to execute test code under a specific culture.
/// </summary>
/// <example>
/// using (AnotherCulture.Finland())
/// {
/// // Execute code in Finland's culture.
/// }
/// // Back to the original culture.
/// </example>
/// <author>Joshua Poehls</author>
internal sealed class AnotherCulture : IDisposable
{
private bool _disposed;
private readonly CultureInfo _originalCurrentCulture;
private readonly CultureInfo _originalCurrentUICulture;
private readonly CultureInfo _originalDefaultThreadCurrentCulture;
private readonly CultureInfo _originalDefaultThreadCurrentUICulture;
public AnotherCulture(string cultureName)
{
_originalCurrentCulture = Thread.CurrentThread.CurrentCulture;
_originalCurrentUICulture = Thread.CurrentThread.CurrentUICulture;
_originalDefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentCulture;
_originalDefaultThreadCurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture;
var otherCulture = CultureInfo.CreateSpecificCulture(cultureName);
// Change the current thread's culture.
Thread.CurrentThread.CurrentCulture = otherCulture;
Thread.CurrentThread.CurrentUICulture = otherCulture;
// Change the default culture of any new threads created by the application domain.
// These properties are only available as of .NET 4.5.
CultureInfo.DefaultThreadCurrentCulture = otherCulture;
CultureInfo.DefaultThreadCurrentUICulture = otherCulture;
}
#region - IDisposable Implementation -
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!this._disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
Thread.CurrentThread.CurrentCulture = _originalCurrentCulture;
Thread.CurrentThread.CurrentUICulture = _originalCurrentUICulture;
CultureInfo.DefaultThreadCurrentCulture = _originalDefaultThreadCurrentCulture;
CultureInfo.DefaultThreadCurrentUICulture = _originalDefaultThreadCurrentUICulture;
}
// Clean up unmanaged resources here.
// Note disposing has been done.
_disposed = true;
}
}
~AnotherCulture()
{
Dispose(false);
}
#endregion
public static AnotherCulture Finland()
{
return new AnotherCulture("fi-FI");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment