Skip to content

Setup for MVC

Ziya Mollamahmut edited this page Jan 14, 2020 · 5 revisions

1 - install LazZiya.ExpressLocalization nuget

Install-Package LazZiya.ExpressLocalization

2 - Configure ExpressLocalization

using System.Globalization;
using LazZiya.ExpressLocalization;
using Microsoft.AspNetCore.Localization;

//setup express localization under ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
    // Other configuration settings....
    
    var cultures = new CultureInfo[]
    {
        new CultureInfo("en"),
        new CultureInfo("tr"),
        new CultureInfo("ar")
    };

    services.AddControllersWithViews()
        .AddExpressLocalization<ExpressLocalizationResource>(
            exOps =>
            {
                exOps.ResourcesPath = "LocalizationResources";
                exOps.RequestLocalizationOptions = ops =>
                {
                    ops.SupportedCultures = cultures;
                    ops.SupportedUICultures = cultures;
                    ops.DefaultRequestCulture = new RequestCulture("en");
                };
            });
}

3 - Use RequestLocalization middleware :

Then configure the app to use RequestLocalizationMiddleware :

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Other codes...
    
    // Use localization middleware
    app.UseRequestLocalization();

    // Add {culture} to the route
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{culture=en}/{controller=Home}/{action=Index}/{id?}");
    });
}
Clone this wiki locally