Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4539652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:58:34+00:00 2026-05-21T14:58:34+00:00

I work on a fairly large ASP .NET Web Forms application that is currently

  • 0

I work on a fairly large ASP .NET Web Forms application that is currently used primarily in the United States. We are in the process of rolling it out to other parts of the world, which of course means we are currently working on localizing all areas of the application. Generally speaking our approach has been to set the current thread’s CurrentCulture and CurrentUICulture properties at the beginning of each request to support the proper formatting and resource extraction based on the current user’s locale.

In some cases, however, we have a need to run a certain bit of a code using a culture other than the culture of the current user. For example, ‘User A’ lives in Germany but works for a company that does business with other companies in France. When ‘User A’ wants to create an invoice (PDF) for one of those French companies, we want that invoice generation code to run with the ‘fr-FR’ culture rather than the ‘de-DE’ culture.

I’ve considered a couple ways of doing this easily and am wondering if I’m going about this correctly. My main concerns are around performance and thread safety.

One approach involves a static method designed to run a given task with a provided culture. Something like this:

 public static void RunWithCulture(CultureInfo culture, Action task)
    {
        if (culture == null)
            throw new ArgumentNullException("culture");

        var originalCulture = new
                                  {
                                      Culture = Thread.CurrentThread.CurrentCulture,
                                      UICulture = Thread.CurrentThread.CurrentUICulture
                                  };

        try
        {
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
            task();
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = originalCulture.Culture;
            Thread.CurrentThread.CurrentUICulture = originalCulture.UICulture;
        }
    }

This method could then be invoked like this:

var customerCulture = new CultureInfo(currentCustomer.Locale);
CultureRunner.RunWithCulture(customerCulture, () => invoiceService.CreateInvoice(currentCustomer.CustomerId));

I’ve also considered creating a class that implements IDisposable that would be responsible for setting the thread culture in it’s ctor and then returning the original cultures back in the Dispose method, so you could call it like this:

var customerCulture = new CultureInfo(currentCustomer.Locale);
using(new CultureRunner(currentCustomer.Locale))
{
  invoiceService.CreateInvoice(currentCustomer.CustomerId);
}

Am I going about this all wrong? Which, if any of these approaches is preferable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:58:34+00:00Added an answer on May 21, 2026 at 2:58 pm

    I like the using approach. I’d also create an extension method to make things read better:

    var customerCulture = new CultureInfo(currentCustomer.Locale);  
    using (customerCulture.AsCurrent()) {
      invoiceService.CreateInvoice(currentCustomer.CustomerId);
    }
    

    Something like this:

    public static class CultureInfoExtensions {
      public static IDisposable AsCurrent(this CultureInfo culture) {
        return new CultureRunner(culture);
      }
    }
    

    CultureRunner example:

    public class CultureRunner : IDisposable
    {
        readonly CultureInfo originalCulture;
        readonly CultureInfo originalUICulture;
    
        public CultureRunner(CultureInfo culture)
        {
            if (culture == null)
                throw new ArgumentNullException(nameof(culture));
    
            originalCulture = Thread.CurrentThread.CurrentCulture;
            originalUICulture = Thread.CurrentThread.CurrentUICulture;
    
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }
    
        public void Dispose()
        {
            Thread.CurrentThread.CurrentCulture = originalCulture;
            Thread.CurrentThread.CurrentUICulture = originalUICulture;
        }
    }
    

    Or, if it’s always your customer object who sets the culture, another extension method would raise the abstraction even further:

    using (currentCustomer.CultureContext()) {
      invoiceService.CreateInvoice(currentCustomer.CustomerId);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm thinking about starting work on a fairly large scale .NET or ASP.NET project
I have a fairly large PHP codebase (10k files) that I work with using
I work for a .NET/MSSQL shop that has trouble supporting customers running Novell, partially
Ok where I work we have a fairly substantial number of systems written over
At work we are currently still using JUnit 3 to run our tests. We
At work, we have multiple branches that we may be working on at any
I work on a complex application where different teams work on their own modules
I work for a firm that provides certain types of financial consulting services in
We have several fairly large JavaScript files embedded into a single script resources DLL.
I had a working solution using ASP.NET MVC Preview 3 (was upgraded from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.