Shared projects and resource files
Asked Answered
S

3

15

We have a solution with a shared project that is referenced by two other projects.

In the shared project, we have resx files, but we noticed the code-behind Designer.cs file is not updated when adding or modifying terms in the resx. Apparently, custom tool generation (ResXFileCodeGenerator) for embedded resources is not supported in shared projects.

I have been searching quite a while to overcome this limitation. I tried creating a class library with all resource files but then I stumbled on the problem that I cannot add a reference to that class library in the shared project.

Does anyone have any idea how I could fix this? Having resource files in a class library, and being able to use them in our shared project views?

--

Apart from the problem above, we can get the resx files working on the projects when we just add the code ourselves in the Designer.cs file, but still it looks like there are still some issues.

For example, in the view, we can get the translations through our namespace and the term name, but in the IDE they are shown as red (not resolvable). However, if we run the application, it works like it should.

UPDATE: I was able to have translations from a resx file in a separate project shown in my views when running the application. However, in the shared project, where the views reside, my references to the resx file are still displayed in red. This is probably because the shared projects has no references to the translation project. Once built, the 'real' projects which have a reference, can find the resx translations so no problem when running the application.

Is there any way to tell visual studio that my shared project uses resx files from a separate class library, so that it finds the terms and doesn't underline my references? It would be nice to have the intellisense working.


As requested in the comments, see a snippet of my View/Html code:

@using System.Configuration
@{
    ViewBag.Title = "ManageCategories";
}
<div class="main container-fluid">
<div id="spis-categories" ng-controller="categoriesController as categoriesVm" ng-cloak ng-init="categoriesVm.siteName='@ConfigurationManager.AppSettings["siteName"]';categoriesVm.pageSize = @inConnexion.Common.Constants.Constants.PageSize">
<div class="modal cust-modal-ontop" tabindex="-1" role="dialog" id="confirmDeleteDialog" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog cust-modal-dialog">
        <div class="modal-content cust-modal-content">
            <div class="modal-header">
                <h4 class="modal-title">@CategoryResources.SPIS_Categories_Maintenance.Button_Delete</h4>
            </div>
            <div class="modal-body">
                <p>@CategoryResources.SPIS_Categories_Maintenance.Confirm_Delete</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" ng-click="categoriesVm.doDeleteSelected();"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>&nbsp;@CategoryResources.SPIS_Categories_Maintenance.Label_Yes</button>
                <button type="button" class="btn btn-default" ng-click="categoriesVm.cancelDeleteSelected();"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>&nbsp;@CategoryResources.SPIS_Categories_Maintenance.Label_No</button>
            </div>
        </div>
    </div>
</div>

And then see this screenshot, where the references aren't recognized:

No references found

Notice also, that the ViewBag is also not recognized. But when running, all works as expected...

Scurrilous answered 5/9, 2017 at 14:58 Comment(6)
can you try to use a link in your real project that points to the shared project resource? You can modify the content element in your csproj and try to use the nesed element link: <Content Include="..\MainProject\SomeFolder\resource.resx"> <Link>..\SharedProkect\SomeFolder\resource.resx</Link> </Content>Intervocalic
Does it work using approach described here?Jung
Hey Luca, thanks for your answer. Currently, my resources are in a separate class library projects. The 'real' projects have references to that class library and translations work upon build. The problem is that my code is in the shared project, where i can't lay a reference to (there isn't even a reference option in solution explorer). I guess, when i can lay some kind of 'ghost' reference to that shared project, then my intellisense should work, but no idea if this is possible...Scurrilous
Eugene, no sorry, because there the shared file is in the shared project. My files are in a separate project, but the shared project does not have like a reference to that project, thus intellisense is not working. Upon build, the shared resources are copied as a dll to the 'real' projects, and then those projects can find the assemblies. I just want to have the intellisense available in the shared project, referencing to the class library.Scurrilous
Could yo share a code-snippet of the view-code / markup referencing those strings from the resx-files?Badman
Sure, see the updated questionScurrilous
W
2

Could be just a namespace issue in the design view. Include the namespace for the resources in the Views/web.config file. The Views will use the namespaces included there to resolve types.

Views/web.config

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="System.Net.Http" />
    <add namespace="{Resources name space here}" />
    <!-- You could also include System.Configuration to avoid having to use using -->
    <add namespace="System.Configuration" />
  </namespaces>
</pages>
Who answered 12/9, 2017 at 12:55 Comment(2)
This doesn't seem to work but i think i'm getting closer to a solution by your reply. The problem is that in the shared projects, no real references can be laid (at least not through IDE). I'm currently trying to force the references through the projitems, and other config files of the project.Scurrilous
Awarded the points to this answer, because stackoverflow made me choose one. This came closest to an actual solution i think. Meanwhile we have decided to abandon the shared projects approach...Scurrilous
U
1

This is working for me using Visual Studio 2015, try the following:

  1. Don't name your shared resources "Resources.resx" or "Resource.resx" as this can lead to conflicts. Use something like "Strings.resx" or "Translation.resx".

  2. Make sure the access modifier is public when you open the resx file in the designer:

Resources Access Modifier

  1. Add the reference in your project and reference the namespace in your Views/web.config as explained in @Nkosi's answer.
Upkeep answered 12/9, 2017 at 13:3 Comment(1)
Sadly this option is disabled for me in a VS2019 SharedProject with a manually added .resx fileMullite
A
0

I was able to make a shared resource .resx file that was visible to each of the other projects and to a shared project. After figuring this out, I came across this question that provides the details: Share Resource Files Across Projects. The important point, as Charlie notes in the second answer, is that in the .csproj file for each project that gets the shared resource, the LogicalName for the linked .resx file must be adjusted so that the designer file can find the resource in the assembly. I created a test set of files to illustrate this - there is too much code to list here but if anyone is interested it is on github. I don't know if it is Ok to post a link to the repository so I'll leave this out.

Albumose answered 13/9, 2018 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.