Aspnetcore.docs: ASP.NET Core - Shared Resource (resx) Localization

Created on 3 Jun 2018  Â·  9Comments  Â·  Source: dotnet/AspNetCore.Docs

General

I am looking for some help at trying to figure out how to create a shared resource (resx) file and use it from different Controllers.

I followed the localization tutorial from Microsoft but unable to get it to work:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1

Looking for some help on what I might be doing wrong.

Thank you for any help. Details below of project.

Startup.cs

public void ConfigureServices(IServiceCollection services){
    ...
    services.AddLocalization(options => options.ResourcesPath = "Resources"); 
    ...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    IList<CultureInfo> supportedCultures = new List<CultureInfo>{
                                                    new CultureInfo("en-CA"),
                                                    new CultureInfo("fr-CA")
            };
            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en-CA"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            });
}

Resources Folder Structure and Files:

ProjectRoot/Resources
ProjectRoot/Resources/SharedResources.cs (dummy class)
ProjectRoot/Resources/SharedResources.resx (Not able to load / DI into Controller)
ProjectRoot/Resources/SharedResources.fr-CA.resx (Not able to load / DI into Controller)
ProjectRoot/Resources/Controllers/MyController.resx (works great)
ProjectRoot/Resources/Controllers/MyController.fr-CA.resx (works great)

Dummy Class:

    public class SharedResources
    {
    }

Sample Controller:

private readonly IStringLocalizer<SharedResources> _sharedLocalizer;
public SampleController(IStringLocalizer<SharedResources> sharedLocalizer){
    _sharedLocalizer = sharedLocalizer
}
[HttpGet]
public string MyMethod(){
 return _sharedLocalizer["GoodMorning"].toString(); //returns "GoodMorning" and not the actual translation.
}

Issues with Existing Topics N/A

Requests for new Topics N/A

P4 Source - Docs.ms

Most helpful comment

@tonyawad88 I hope it's all working great for you :) I believe what you found is that the namespace of "SharedResources" was causing the localizer to not be able to find the correct path of the resource file.

I found that using the following file structure works fine:
ProjectRoot/Resources/SharedResource.cs
ProjectRoot/Resources/SharedResource.resx

You just need to ensure the namespace in SharedResource.cs is without "Resources", e.g.:

namespace ProjectRoot // Not "ProjectRoot.Resources"
{
    public class SharedResource
    {
    }
}

All 9 comments

@tonyawad88 Thanks for the question. I'm not sure what you're doing wrong. You'll probably get an answer sooner if you post the question to StackOverflow or another support forum. Have you tested the sample download?

@Rick-Anderson I figured it out, any chance you can confirm the following:

The "SharedResources.cs" file has to be at the root of the project and not within the resources folder namespace.
ProjectRoot/SharedResources.cs ✓
ProjectRoot/Resources/SharedResource.cs ✖

I am not sure why, if someone can shed some light on this, it will be great.

Cheers !

@ryanbrandenburg please confirm.

Hey i tried to do that @tonyawad88 said but it didn't work :'(

@hishamco please review

Please checkout the @damienbod AspNetCoreMvcSharedLocalization sample

@Rick-Anderson what's the action needed here, seems the question has been answered

@tonyawad88 I hope it's all working great for you :) I believe what you found is that the namespace of "SharedResources" was causing the localizer to not be able to find the correct path of the resource file.

I found that using the following file structure works fine:
ProjectRoot/Resources/SharedResource.cs
ProjectRoot/Resources/SharedResource.resx

You just need to ensure the namespace in SharedResource.cs is without "Resources", e.g.:

namespace ProjectRoot // Not "ProjectRoot.Resources"
{
    public class SharedResource
    {
    }
}

@Xerillio good finding. So the shared class file can probably go under the folder Resources/SharedResource.cs as long as the namespace of the shared class is ProjectRoot.

Was this page helpful?
0 / 5 - 0 ratings