我正在尝试使用MSBUILD配置为本地化资源构建附属程序集:
Automated build of Satellite Assemblies in Visual Studio with MSBUILD
但是,更改文化时不会加载附属程序集。我做错了什么?
这是我的Visual Studio 2017配置:
strings.resx 和 strings.de.resx 只定义了一个字符串String1,它是"我的英文字符串"或者"我的德语字符串。"
我有以下帖子构建事件:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\msbuild" "$(SolutionDir)\TestLocalization\build.msbuild" /v:d /property:destination="$(TargetDir)
build.msbuild :
<!-- Resource files -->
<ItemGroup>
<Res Include = "strings.de.resx">
<Culture>de</Culture>
</Res>
</ItemGroup>
<Target Name="Main">
<Message Text="$(destination)"/>
<CallTarget Targets="CompileResources" />
<CallTarget Targets="BuildSatelliteAssemblies" />
<CallTarget Targets="CopyToFolder" Condition="$(destination)!=''"/>
<CallTarget Targets="CleanUp" />
</Target>
<!-- Generate .resource file from .resx -->
<Target Name="CompileResources">
<GenerateResource Sources="@(Res)" PublicClass="true" >
<Output ItemName="Resources" TaskParameter="OutputResources"/>
</GenerateResource>
</Target>
<!-- Create a satellite assembly for each culture -->
<Target Name="BuildSatelliteAssemblies" DependsOnTargets="CompileResources">
<MakeDir Directories="%(Res.Culture)"/>
<AL OutputAssembly="%(Culture)\TestLocalization.resources.dll"
Version="1.0.0.0"
Culture="%(Culture)"
EmbedResources="@(Resources)"/>
</Target>
<!-- copy from the resources folder at the solution level to
the destination folder passed in as a property to the msbuild -->
<Target Name="CopyToFolder" DependsOnTargets="BuildSatelliteAssemblies">
<MakeDir Directories="$(destination)\%(Res.Culture)"/>
<CreateItem Include="%(Res.Culture)\*.dll" AdditionalMetadata="Culture=%(Res.Culture)">
<Output ItemName="SatelliteAssemblies" TaskParameter="Include"/>
</CreateItem>
<Copy DestinationFolder="$(destination)%(Culture)"
SourceFiles="@(SatelliteAssemblies)" />
</Target>
<!-- remove temporary files and folders-->
<Target Name="CleanUp">
<Delete Files="@(Resources)"/>
<RemoveDir Directories="%(Res.Culture)" />
</Target>
Form1.cs的:
namespace TestLocalization
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(strings.String1);
label1.Text = strings.String1;
}
}
}
我看到生成了资源DLL bin \ Debug \ de \ TestLocalization.resources.dll ,但是在运行程序时没有加载相应的德语字符串。如果我删除了MSBUILD post build事件并且只为 strings.resx 和 strings.de.resx 使用了嵌入资源,那么德语字符串会正确加载。