ASP.Net Core 2: Shared Localization

main-qimg-82f43ebb6d5b671466ae8fbce0195735.pngIn this post we’ll cover the absolute simplest way to localize using a shared resource instead of one resx per controller. Here’s a good deep dive as there is much more to know, but this will get you going.

Step 1: Set up the Startup.cs

public void ConfigureServices(IServiceCollection services)
        opts =>
            var supportedCultures = new List<CultureInfo>
                new CultureInfo("en"),
                new CultureInfo("fr"),
            opts.DefaultRequestCulture = new RequestCulture("en");
            // Formatting numbers, dates, etc.
            opts.SupportedCultures = supportedCultures;
            // UI strings that we have localized.
            opts.SupportedUICultures = supportedCultures;

Step 2: Then update Configure()

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    var options = app.ApplicationServices

Step 3: Add a folder called “Resources” to the web project. NOT in an external project. Add these three resx files.

2018-05-27 09_35_12-Window.png

Step 4: Reference the shared resource in your controller

public class PickersController : Controller
    private readonly IStringLocalizer<Startup_localizaer;
    public PickersController(IStringLocalizer<Startup> localizaer)
        _localizaer = localizaer;
       var someString =  _localizaer["Your Key from Resx"];


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s