Getting started
editInstall the package
Add the Meziantou.Framework.ResxSourceGenerator package to the project that contains the .resx files:
dotnet package add Meziantou.Framework.ResxSourceGeneratorEnable the generator
The generator processes the .resx files that are AdditionalFiles of the project. Add them in the project file:
<ItemGroup>
<AdditionalFiles Include="**/*.resx" />
</ItemGroup>The .resx files stay EmbeddedResource items, as the SDK declares them by default: the generated class reads the resources that MSBuild embeds in the assembly.
When a project already uses the Visual Studio generator (ResXFileCodeGenerator and a .Designer.cs file), remove it: both would declare the same class.
Use the generated class
For a Sample.resx file at the root of the project, the generator writes a Sample class in the root namespace, and a SampleNames class with the names of the resources:
<data name="Hello" xml:space="preserve">
<value>Hello {0}!</value>
</data>Assert.Equal("Hello {0}!", Sample.Hello);
Assert.Equal("Hello meziantou!", Sample.FormatHello("meziantou"));Sample.Helloreturns the string for the current UI culture.Sample.FormatHelloexists because the value contains{0}: it takes one argument and formats the string.
A file in a folder gets the namespace of the folder: Resources/Errors.resx generates the Errors class in the <RootNamespace>.Resources namespace. See Configuration to change it.
See the generated code
The generated code is not written to disk by default. To inspect it, ask the compiler to write the generated files:
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)/GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>Each file starts with a comment that lists the values the generator used (namespace, class name, resource name...), which helps when a class does not have the expected name.
Next steps
- Generated members: the properties and methods of the generated classes.
- Format parameters: give meaningful names and types to the arguments of format methods.