2

Hi– I'm planning to handle server side localization for error strings etc. based on the “Accept-Language” header, by setting the CurrentUICulture based on that header, but apparently it doesn’t flow though the async calls, below is a sample code to illustrate the problem, is there any default way of handling the localization for async calls?

   public async Task<HttpResponseMessage> GetAsync()
    {            
        //set the current ui culture, to say "fr-FR", based on "Accept-Language" header
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("**fr-FR**");  

       var task = httpClient.PutAsync(endpoint, content)            

        //do some processing

        var res  = await task;

        var culture = Thread.CurrentThread.CurrentUICulture.Name; **//ITS NOT necessarily fr-FR**

        //do some more processing
        //and handle localizations etc.

        return res;
    }

I'm looking for a cleaner/seamless way of handling localization for cases where there are real async operations esp. for the code following the await call

Edit: replaced Task.Run() with httpClient.PutAsync for clarity

1 Answer 1

2

Task.Run and Task.Factory.StartNew do not have any context. That's an expected behavior. Context is preserved when you use the await keyword. So here's what you could do:

public static async Task<IEnumerable<string>> GetAsync()
{            
    //set the current ui culture, to say "fr-FR", based on "Accept-Language" header
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");

    //do some processing

    var s = await GetSomething();

    var culture = Thread.CurrentThread.CurrentUICulture.Name; //It's ja-JP

    return new[] { s, s };
}

public static Task<string> GetSomething()
{
    var cuture = Thread.CurrentThread.CurrentUICulture.Name; // It's fr-FR
    var tcs = new TaskCompletionSource<string>();
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("ja-JP");
    tcs.SetResult("<something>");
    return tcs.Task;
}
Sign up to request clarification or add additional context in comments.

15 Comments

It's not guaranteed as the 3 parts (1. GetSomething() call 2. the code before and 3. the code after it) can potentially run on 3 different asp.net threads esp. if GetSomething() is a long running async operation.
If you are using the code in my example, they will run on the same context. If you create tasks with Task.Run or Task.Factory.StartNew they are not guaranteed to run on the same thread.
I'm looking for a cleaner/seamless way of handling localization for cases where there are real async operations like calls to another webservice etc, by default, the context won't be the same for such cases.
yes but when it returns, the remaining part can potentially execute on a 3rd thread not necessarily on the original thread
The sample given in the question sets the current ui culture, to say "fr-FR", based on "Accept-Language" header before the await call, one approach is we can set the current ui culture again after each await call, to say "fr-FR", based on "Accept-Language" header. The ask was if this can be done in a seamless way, looks like that's not possible. Anyways, thanks Darin for the prompt responses.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.