Asp net core localization with help of sharedresources

ASP.NET Core is a powerful programming that allows developers to build robust and scalable web applications. One of the key features of ASP.NET Core is its localization capabilities, which enable developers to create applications that can be easily translated into different languages.

To achieve localization in ASP.NET Core, developers can make use of shared resources. Shared resources are a collection of key-value pairs that contain the translated strings for different languages. These resources can be shared across multiple views, controllers, and other components of the application.

To begin with, let's take a look at how to set up localization in an ASP.NET Core application. First, we need to add the required NuGet packages to our . Open the Package Manager Console and run the following command:

-Package .Extensions.Localization

Once the package is installed, we need to configure the localization services in the .cs file. In the method, add the following code:

services.AddLocalization(options => options.ResourcesPath = "Resources");

This code configures the localization services and specifies the path where the shared resources are located. In this example, the resources are stored in a folder “Resources”.

Next, we need to add the that will the localization. In the Configure method of the Startup.cs file, add the following code:

var supportedCultures = new[]
    new CultureInfo("en-US"),
    new CultureInfo("fr-FR"),

app.UseRequestLocalization(new RequestLocalizationOptions
    DefaultRequestCulture = new RequestCulture("en-US"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures

This code sets up the supported cultures and specifies the default culture. In this example, we have specified “en-US” as the default culture and “fr-FR” as an additional supported culture.

Now that we have set up the localization services, let's see how to use shared resources in our views. First, create a folder named “Resources” in the root of your project. this folder, create a file named “SharedResources.resx”. This file will contain the key-value pairs for the translated strings.

For example, let's say we want to translate the string “Hello, world!” into different languages. Open the “SharedResources.resx” file and add a new key-value pair:

Key: Greeting
Value: Hello, world!

To use this translated string in our view, we need to inject the IStringLocalizer interface into our view. In the view's code-behind file, add the following code:

private readonly IStringLocalizer _localizer;

public IndexModel(IStringLocalizer localizer)
    _localizer = localizer;

Now, we can use the translated string in our view by calling the GetString method of the IStringLocalizer interface:


This will display the translated string “Hello, world!” in the view.

In conclusion, ASP.NET Core provides powerful localization capabilities that allow developers to easily translate their applications into different languages. By using shared resources, developers can centralize the translation strings and reuse them across different components of the application. This not only simplifies the localization process but also improves the maintainability of the codebase.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents