aspnet / Localization

[Archived] Localization abstractions and implementations for ASP.NET Core applications. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
157 stars 65 forks source link

MVC 6 / Asp.net VNEXT how to use shared, separate Resource DLL with IViewLocalizer or IHtmlLocalizer and .resx #246

Closed Eilon closed 8 years ago

Eilon commented 8 years ago

Moved from https://github.com/aspnet/Mvc/issues/4723 Opened by @MrWinky


There are lots of great VNEXT articles explaining how to use RESX resources when the \Resources folder are in the VNEXT application's sub folder path.

However, I have an MVC application which requires I place the RESX files in a separate VNEXT class library dll in the solution and share it among projects (assume the separate project is called MyResourceProject). Maybe I should be using a different project type for sharing resource strings in a RESX?

The following does not work:

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

and

  services.AddMvc()
          .AddViewLocalization(options => options.ResourcesPath = "MyResourceProject .Resources")

How do I point to a separate library path in an MVC VNEXT application?

Eilon commented 8 years ago

Possibly related: https://github.com/aspnet/Localization/issues/214

css-code commented 8 years ago

In 214, I am not seeing a solution. The problem is when I design a 3 tiered application this issue causes a circular reference. The main web portal module suddenly becomes a dependency to the Data and Business layer modules.

For example:

  1. EF 7 is kept in it's own project. MyProject.Data.EntityFramework
  2. Business logic layer objects which use the strings MyProject.BLL.ViewModels
  3. The web portal (with startup.cs) is in it's own project MyProject.Portal

This is pretty standard N tier architecture.

Each of these projects need the resx files.

However, If I am forced to place MyProject.Resources.resx in the web portal app, now I have a circular reference problem. Because Portal project is dependent on EntityFramework and ViewModel projects (as it should be). Conversely, EntityFramework and BLL need those SAME resource strings and now becomes dependent on the Portal project!?.

ryanbrandenburg commented 8 years ago

@MrWinky we did the work to allow class library resources in #283. To enable this scenario you need to add ResourceLocationAttribute to the AssemblyInfo of the class libraries so that the "main" project knows where to look for the resources.

Let us know if you have any trouble with that, or if I misunderstood and that doesn't actually resolve your issue.

ryanbrandenburg commented 8 years ago

On second thought I'll close this as it seems to be the exact use case #283, but feel free to re-open if the need arises.

fabercs commented 5 years ago

@MrWinky we did the work to allow class library resources in #283. To enable this scenario you need to add ResourceLocationAttribute to the AssemblyInfo of the class libraries so that the "main" project knows where to look for the resources.

hi @ryanbrandenburg, could you please elaborate this answer? You say AssemblyInfo of the class libraries. Which libraries? Where exactly to put the attribute?

hishamco commented 5 years ago

@fabercs you should decorate your assembly by adding ResourceLocationAttribute to the AssemblyInfo. Have a look to this line for instance https://github.com/aspnet/Localization/blob/master/test/ResourcesClassLibraryWithAttribute/AssemblyInfo.cs#L7

fabercs commented 5 years ago

@hishamco thank you for answer, this is my solution structure;

|--MyProject.Infrastructure
|--MyProject.Core
|--MyProject.Web
    |--Controllers
    |--ViewModels
    |--Views
|--MyProject.Resources
    |--Controllers
    |--ViewModels
    |--Views

Should I create an AssemblyInfo.cs file under MyProject.Resources and add the line [assembly: ResourceLocation("ResourceFolder")]? Should I wrap all the files in a ResourceFolder or keep it as is?

hishamco commented 5 years ago

Yep, add it under MyProject.Resources

Should I wrap all the files in a ResourceFolder or keep it as is

Yes & No, but the important thing is resource naming, you can check the docs on this one

fabercs commented 5 years ago

@hishamco I did lots of thing but I couldn't get it work. Is there any real world application that works like this way? I know resources and pages or classes follow namespace convention, but I couldn't understand how using an assemblyInfo solves this.

Here what I did;

Do we need any other configuration in Startup.cs?

And project structure is as follows;

|--MyProject.Infrastructure
|--MyProject.Core
|--MyProject.Web
    |--Controllers
    |--ViewModels
    |--Views
       |--Customer
          |--Index.cshtml
|--MyProject.Resources
    |--Resources
       |--Controllers
       |--ViewModels
       |--Views
          |--Customer
             |--Index.en-US.resx
             |--Index.tr-TR.resx
       |--SharedResource.cs
       |--SharedResource.en-US.resx
       |--SharedResource.tr-TR.resx   

SharedResources are the only working part in project right now btw. I suspect AssemblyInfo should be placed in Web project, maybe?

hishamco commented 5 years ago

@fabercs seems this issue is died, could you please file a new issue in the AspNetCore repo and copy your latest comment

fabercs commented 5 years ago

@hishamco actually I have already created a new issue, https://github.com/aspnet/Mvc/issues/8739 I have also moved my last comment to there..

hishamco commented 5 years ago

That's great, waiting for your reply there