Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Add a template for RESX files #739

Open
drewnoakes opened this issue Jul 14, 2023 · 3 comments
Open

Add a template for RESX files #739

drewnoakes opened this issue Jul 14, 2023 · 3 comments

Comments

@drewnoakes
Copy link
Member

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
Copy link

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
Copy link
Member

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
Copy link

bitbonk commented Aug 14, 2023

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 dotnet/roslyn-analyzers#5521 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants