dotnet / templates

Templates for .NET
184 stars 59 forks source link

Add a template for RESX files #739

Open drewnoakes opened 1 year ago

drewnoakes commented 1 year ago

It should be possible to create a new RESX resource file from the command line.

dotnet new resx

Note that the default template in Visual Studio includes a large comment and XSD. These are not required. A minimal template would contain:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>1.3</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
</root>
tmeschter commented 1 year ago

It may be a bit more complicated than this as you also want a corresponding *.Designer.cs file, and you need to make some updates to the project file:

  <ItemGroup>
    <Compile Update="Resources.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <EmbeddedResource Update="Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>
rainersigwald commented 1 year ago

I'd prefer we not encourage that mode of C#-from-resx codegen, in favor of the MSBuild one that works outside of VS.

bitbonk commented 1 year ago

Microsoft also has a source generator that can generate the code (C#, VB, F#). This has the benefit of not having to mention the resx files in the project files at all. Also the generated code is nullable aware which the code generated by ResXFileCodeGenerator and the PublicResXFileCodeGenerator is not.

See also https://github.com/dotnet/roslyn-analyzers/issues/5521#issuecomment-1658897400