Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Contributor

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants