Put translated resx files in a different folder in Visual Studio?

半城伤御伤魂 提交于 2019-12-11 10:22:37

问题


I've got a solution with 14 translations and 12 files and the "Resources" folder is getting a bit out of hand. Is there a way to put the translated files into a different folder so I can access the master English ones more easily?

I've noticed that you can change the namespace the designer file is generated in by setting the Custom Tool Namespace, but I haven't figured out how to pick up translations from a different folder. I toyed with the idea of using links to make a "shortcuts" folder and a "real" folder but you can't link to a file within the project structure and you can't link to the same file twice outside of the project structure. I thought about changing the namespace that the ResourceManager used based on the language but that would break the fallback behavior.

Is there any way to manage this so it isn't a giant list of files in Solution Explorer?


回答1:


Let's say you want to place all the translated .resx files in a Translations folder, like this (note I have left the default and en versions in the root folder):

Then all you need to do is edit the MSBuild project file (from Visual Studio, right-click on the project node, 'Unload project', then 'Edit xxx.csproj'), and reconfigure each Resx file in this folder like this:

before:

  <ItemGroup>
    ...
    <EmbeddedResource Include="Resource1.en.resx" />
    <EmbeddedResource Include="Translations\Resource1.fr.resx" />
    <EmbeddedResource Include="Resource1.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resource1.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    ...
  </ItemGroup>

after:

  <ItemGroup>
    ...
    <EmbeddedResource Include="Resource1.en.resx" />
    <EmbeddedResource Include="Translations\Resource1.fr.resx">
      <ManifestResourceName>$(TargetName).%(Filename)</ManifestResourceName> <!-- add this for all .resx files in a custom folder -->
    </EmbeddedResource>
    <EmbeddedResource Include="Resource1.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resource1.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    ...
  </ItemGroup>

What it does is instruct underlying MSBuild tasks to use the manifest name you specificy instead of generating one automatically. The automatically generated one always uses the folder layout where the RESX resides in which is not what we want.

After compilation, the final satellite ConsoleApplication1.resources.dll file will be placed in the fr folder as usual, but its content will be the same as if the resx file was placed in the root folder.




回答2:


One way to achieve this could be to write a custom resource provider. Rick Strahl wrote one to serve resources from a database rather than RESX files. The class he inherits from is System.Resources.ResourceManager in mscorlib. Here is his implementation.

https://github.com/RickStrahl/Westwind.Globalization/blob/master/Westwind.Globalization/DbResourceManager/DbResourceManager.cs

Here is a link to the article, and he provides the code.

http://weblog.west-wind.com/posts/2014/Mar/31/Updated-ASPNET-Database-Resource-Provider

I realise you dont want to store the resources from a DB, but the principle should apply



来源:https://stackoverflow.com/questions/33007969/put-translated-resx-files-in-a-different-folder-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!