Project level Culture setting for Azure Functions App

Viewed 2370

Is it possible to change culture on a project level on Azure Functions App?

https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings

The app is using Consumption plan or Premium plan, not via ASP.NET Core.

My Startup.cs file is like below:

  public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
       }
}

Can ASP.NET Core that is based on different Startup.cs not like above use Consumption plan or Premium plan??

Asp.net Core that must use App Service plan like below: https://andrewlock.net/adding-localisation-to-an-asp-net-core-application/

2 Answers

When migrating legacy application running on servers to Azure you always need to take care of Timezone and Culture settings that originally are fetched from the machine. For Azure Functions you can set the timezone in the app settings:

WEBSITE_TIME_ZONE=Europe/London

Possible values found here https://en.wikipedia.org/wiki/List_of_tz_database_time_zones. This actually differs for app services that seem to use TZ for Linux and WEBSITE_TIME_ZONE for Windows.

For culture it is more complicated. Using aspnet core you define it in Configure in the Startup class

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, 
ILoggerFactory loggerFactory)
    {
        var cultureInfo = new CultureInfo("en-US");
        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture(cultureInfo),
            SupportedCultures = new List<CultureInfo>
            {
                cultureInfo,
            },
            SupportedUICultures = new List<CultureInfo>
            {
                cultureInfo,
            }
        });
    }

That is not possible in Azure Function Apps. What you can do is to create a Setup class and then set the culture for the appdomain and the current thread. This will probably work as long Azure isnt altering the Appdomain.

[assembly: FunctionsStartup(typeof(Startup))]
namespace FunctionApp
{
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder app)
        {
            var cultureInfo = new CultureInfo("en-US");
            CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
            CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }
    }
}

Azure Function didn't provide a built-in method to change culture.

Put this at the starting of your function app:

using System.Threading;
using System.Globalization;

//......

string culture = "en-US";
CultureInfo CI = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = CI;

Above code will change the culture to en-US. You can set it to other value.

This is the document:

https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.currentculture?view=netcore-3.1

Does this solved your problem?

Related