Search code examples
.netasp.net-mvc-3resxxdt-transform

Using TransformXml task on resx file that is an embeded resource


Has anyone had experience performing xml transform on resx files before? I would like to transform a resx file per configuration. Each configuration's tranform file may replace some string values in the resource file. For example:

<None Include="Resources\Label.Release.resx.config">
  <DependentUpon>Label.resx</DependentUpon>
</None>
<EmbeddedResource Include="Resources\Label.resx">
  <Generator>PublicResXFileCodeGenerator</Generator>
  <LastGenOutput>Label.Designer.cs</LastGenOutput>
</EmbeddedResource>

I'm trying to tranform some data values in the resx file. In Label.Release.resx.cofing:

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <data name="Title" xml:space="preserve"  xdt:Locator="Match(name)">
    <value  xdt:Tranform="Replace">CEO</value>
  </data> 
</root>

I tried playing with this in the BeforeBuild task:

<Target Name="BeforeBuild">
<MakeDir Directories="$(IntermediateOutputPath)\Resources" 
    Condition="!Exists('$(IntermediateOutputPath)\Resources')"/>
<TransformXml Source="Resources\Label.resx" Transform="Resources\Label.$(Configuration).resx.config" Destination="$(IntermediateOutputPath)\Resources\Label.resx" />

The Label.resx that results in $(IntermediateOutputPath)\Resources folder after has had no transformation. I'm also not sure if that's the location that I would output the transformation result to because Lable.resx should be an embedded resorce in the end.

any help is appreciated


Solution

  • Here's what I ended up doing:

    <Target Name="TransLabel">
    <MakeDir Directories="$(IntermediateOutputPath)\Resources" Condition="!Exists('$(IntermediateOutputPath)\Resources')" />
    <TransformXml Source="Resources\Label.resx" Transform="Resources\Label.$(Configuration).resx.config" Destination="$(IntermediateOutputPath)\Resources\Label.resx" />
    <GenerateResource Sources="$(IntermediateOutputPath)\Resources\Label.resx" OutputResources="@(Resx->'$(IntermediateOutputPath)%(Identity).resources')">
    </GenerateResource>
    <Copy OverwriteReadOnlyFiles="true" SourceFiles="$(IntermediateOutputPath)\Resources\Label.resources" DestinationFiles="$(IntermediateOutputPath)\Ccwa.Resources.Label.resources" />
    <RemoveDir Directories="$(IntermediateOutputPath)\Resources" />
    </Target>
    
    <Target Name="AfterResGen">
    <CallTarget Targets="TransLabel" />
    

    I transform the resx file by hooking into the AfterResGen target, in which the normal resource generating process has taken place. Then I perform my own transformation and generate my own resource file. Then I replace the one already generated before the transformation. When the build continues and the project dll is generated, my transformed resource file is picked up.