dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.44k stars 25.33k forks source link

Replace legacy services.Configure code #29219

Open Rick-Anderson opened 1 year ago

Rick-Anderson commented 1 year ago

See embedded code here

Write that for .NET 6+ or better yet put that code in https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/localization/sample/6.x so we can import the snippet

@hishamcocan you take this?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.


Associated WorkItem - 89656

Rick-Anderson commented 1 year ago

Something like the following

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System.Globalization;
using System.Threading.Tasks;

public class CustomRequestCultureProvider : RequestCultureProvider
{
    public override async Task<ProviderCultureResult?> DetermineProviderCultureResult(HttpContext httpContext)
    {
        // Retrieve user's language and culture from the database.
        // Replace the following code with your logic to fetch the user's preferred language and culture.

        // eg: Fetch user's language and culture from the database
        var userId = httpContext.User.Identity?.Name;
        var userLanguage = await FetchUserLanguageFromDatabase(userId);

        if (!string.IsNullOrEmpty(userLanguage))
        {
            var cultureInfo = new CultureInfo(userLanguage);
            return new ProviderCultureResult(cultureInfo.Name, cultureInfo.Name);
        }

        return null;
    }

    private async Task<string> FetchUserLanguageFromDatabase(string userId)
    {
        // Replace this method with your actual implementation to fetch the user's preferred language from the database.
        // eg: Query the database using the userId to retrieve the user's preferred language
        // eg: return await dbContext.Users.Where(u => u.Id == userId).Select(u => u.Language).FirstOrDefaultAsync();

        return "en-US"; // Return a default language if the user's preferred language is not found in the database
    }
}

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add your database and other services here

        // Configure the application services
        builder.Services.AddLocalization();

        // Configure the request localization options.
        builder.Services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("fr-FR")
            };

            options.DefaultRequestCulture = new RequestCulture("en-US");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;

            // Add your custom request culture provider
            options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider());
        });

        var app = builder.Build();

        // Configure the request localization middleware.
        app.UseRequestLocalization();

        // Add app middleware and endpoints here

        await app.RunAsync();
    }
}
Rick-Anderson commented 1 week ago

@hishamco can you update the app for .NET 8?

hishamco commented 1 week ago

Sure, it is in mind, also I will update the PO sample too

hishamco commented 1 week ago

Could you assign this to me?