How to localize WPF Application with RESX files in subdirectories

旧街凉风 提交于 2021-01-28 20:25:54

问题


My team has some WPF projects written using XAML. We recently added a large number of local specific RESX files to each project. In order to keep things tidy, I was asked to store these files in [Project]>Localization>[locale]

Now, when we run the application on a non en-US locale, strings are pulled from the appropriate RESX file. We've tried to update Namespaces in the properties of the RESX files, as well as setting them to Public so that a resulting Designer is created. The only way that we can get things to work is by moving the locale RESX files directly into the Project's Properties directory.

Is there anyway to update the XAML to search for locale RESX files in a subdirectory?

EDIT XAML Codebase and Solution Explorer

I've attached an image of the problem for clarification.

Some code changes that we've tried:
1) We attempted to set the Namespace for xmlns:res="clr-namespace:[PROJECT].CoreUI.Localization"
2) Attempted to set the Namespace to xmlns:res="clr-namespace:[PROJECT].CoreUI"
3) We've also attempted to change the Custom Tool Namespace of the RESX file to match the current namespace My.Properties as well as CoreUI.Properties

We've also confirmed that the Access Modifiers for both Resources.resx and Resources.ru.resx (for example) are set to Public and that both Build Actions show "Embedded Resource"


回答1:


Thanks for the help!

My colleague actually stumbled upon another post in here that we somehow missed during out countless searching:

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

In short, we were thinking about making the project search down into subdirectories for the RESX files. In actuality, we need the RESX file to search for the already created designer further up in the chain.

<ItemGroup>
<EmbeddedResource Include="Localization\ar\Resources.ar.resx" >
<ManifestResourceName>$(TargetName).Properties.%(Filename)</ManifestResourceName>
</EmbeddedResource>

Entering the ManifestResourceName appears to tell the RESX file that we already have a Designer created and not to make a new one.

Thanks again for everyone's help!




回答2:


You've to first include your folder in the namespace and mention project name like:

xmlns:resx="clr-namespace:Prism_Modules.MyResources;assembly=Prism_Modules"

Also you must include:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Finally, you should call values in resources as:

<dxb:BarButtonItem x:Name="menuFile" Content="{x:Static resx:Resources.mnuFile}"  }" />



回答3:


When moving .resx file to another folder/project, the .Designer.cs file that exposes resource members will still point to the old namespace and also Visual Studio will place it outside the .resx file.

To fix this, I simply deleted the generated .Designer.cs file and then after opening and saving .resx file, the Designer.cs file got generated with proper namespace and I could normally refer to it even from other projects. To generate .Designer.cs file for resource languages/files that have none just open .resx file and use the Access Modifier combobox and VS will create it for you.

I hope I was of any help.



来源:https://stackoverflow.com/questions/55072839/how-to-localize-wpf-application-with-resx-files-in-subdirectories

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