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.

Fatih Erol
  • 633
  • 2
  • 8
  • 22
  • 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) – Camilo Terevinto Mar 21 '18 at 18:37
  • -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. – Fatih Erol Mar 21 '18 at 18:43
  • I have described the localization in asp.net core on [this other post](https://stackoverflow.com/a/52498946/9780579), please take a look. – Mohammad Khodabandeh Sep 25 '18 at 13:20

4 Answers4

12

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).

Thiago Daher
  • 126
  • 4
  • 1
    Thnx for answer. Working now :) – Fatih Erol Jun 02 '18 at 14:07
  • 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! – TaiT's Dec 10 '18 at 14:01
  • 2
    No idea why this working. It does not make any sense due to if we really needs this package it should fail to compile. – UltimaWeapon Aug 12 '20 at 11:01
  • 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... – Thiago Daher Aug 13 '20 at 20:52
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.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79
  • 2
    ResourcePath can be set to "Resources" and still use folders to organize files for different classes: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.0#resource-file-naming – Gorgi Rankovski Jun 03 '18 at 10:05
1

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

Folder

hannes neukermans
  • 12,017
  • 7
  • 37
  • 56
0

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