Why does not work ASP.NET Core Localization
Asked Answered
G

4

9

I created an empty project.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(s => s.ResourcesPath = "Resources");
    var supportedCultures = new CultureInfo[]
    {
        new CultureInfo("de-CH"),
        new CultureInfo("en-GB"),
    };

    services.Configure<RequestLocalizationOptions>(s =>
    {
        s.SupportedCultures = supportedCultures;
        s.SupportedUICultures = supportedCultures;
        s.DefaultRequestCulture = new RequestCulture(culture: "de-CH", uiCulture: "de-CH");
    });

    services.AddMvc()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

    app.UseStaticFiles();

    // Using localization 
    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value);

    app.UseMvc();
}

Folder Structure

Resources
|
|--Controllers 
|       HomeController.de.resx
|       HomeController.en.resx
|       HomeController.resx

Controller

public class HomeController : Controller
{
    private readonly IStringLocalizer<HomeController> _stringLocalizer;

    public HomeController(IStringLocalizer<HomeController> stringLocalizer)
    {
        _stringLocalizer = stringLocalizer;
    }

    public IActionResult Index()
    {
        string testValue = _stringLocalizer["Test"];
        return View();
    }
}

I'm new about asp.net core, I'm just trying to understand, Why testValue always return Test, it's a bit confusing. I'm doing something wrong? i will be happy if u help me.

Gerik answered 21/3, 2018 at 18:30 Comment(3)
What's the content of HomeController.de.resx, HomeController.en.resx and HomeController.resx? Also, this may be fixed by a Build > Clean and Build > Rebuild (this happened to me once)Wateriness
-HomeController.resx content name: Test, value: Default-Translation, -HomeController.en.resx content name: Test, value: This message comes from the controller. -HomeController.de.resx content name: Test, value: Diese Nachricht kommt vom Controller.Gerik
I have described the localization in asp.net core on this other post, please take a look.Fevre
O
13

Just add the package Microsoft.Extensions.Localization
After do that, it works.
The ResourcePath is optional, and if you leave it null, the resource files organization style is the same that classic Asp.Net application (in the same location of the target classes).

Oas answered 18/5, 2018 at 8:23 Comment(4)
Thnx for answer. Working now :)Gerik
In my project - developping on 2 separate computers - localization was working on one computer but not the other... This made me sick for days. Adding the package in my shared resources library somehow solved the issue on the second computer. Thanks!Bagby
No idea why this working. It does not make any sense due to if we really needs this package it should fail to compile.Fourthly
Maybe the compilation does not fails due the "standard packages", those that are part of standard WebApp template, contains some objects at Localization namespace that must be able to attend some basic localization requirements, such as Dependency Injection and configuration. By this way, you can configure the dependency injection, even that the implementation requires some additional packages or configuration to do their job. But I agree that if these objects are there (in standard packages), there is something wrong. Maybe all these objects must be moved to another Package...Oas
T
3

Two different errors here prevent correct loading of localized resources.

  1. You set incorrect ResourcesPath in AddLocalization() call. Since your resx files are placed in Resources/Controllers directory, you should replace call

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

    with

    services.AddLocalization(s => s.ResourcesPath = "Resources/Controllers");
    
  2. You use incorrect name for resx files. Check Resource file naming section in Globalization and localization in ASP.NET Core article:

    Resources are named for the full type name of their class minus the assembly name. For example, a French resource in a project whose main assembly is LocalizationWebsite.Web.dll for the class LocalizationWebsite.Web.Startup would be named Startup.fr.resx. A resource for the class LocalizationWebsite.Web.Controllers.HomeController would be named Controllers.HomeController.fr.resx. If your targeted class's namespace isn't the same as the assembly name you will need the full type name. For example, in the sample project a resource for the type ExtraNamespace.Tools would be named ExtraNamespace.Tools.fr.resx.

    So if your assembly is called TestMvcApplication and HomeController resides in namespace TestMvcApplication.Controllers, then you should call your resx files in the following way:

    Resources
    |
    |--Controllers 
    |       Controllers.HomeController.de.resx
    |       Controllers.HomeController.en.resx
    |       Controllers.HomeController.resx
    

I believe after you make above changes to your project, localization will work ok.

Tunstall answered 22/3, 2018 at 18:9 Comment(2)
ResourcePath can be set to "Resources" and still use folders to organize files for different classes: learn.microsoft.com/en-us/aspnet/core/fundamentals/…Prent
How is it possible to use a folder structure like this: Resources -> Controllers -> en-US -> HomeController.resxBelabor
A
1

I fixed it, by making sure that my folder structure reflected the namespace I used when working with resource designer files !!

Folder

Arliearliene answered 17/8, 2021 at 15:51 Comment(1)
Wow, thanks! That was actually my problemCibis
S
0

Make sure the "SharedResources.cs" has the same namespace as the project's name that contains it.

Sarajane answered 12/7, 2023 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.