Morris.AutoLocalize.Fody 1.2.1

Prefix Reserved
dotnet add package Morris.AutoLocalize.Fody --version 1.2.1
                    
NuGet\Install-Package Morris.AutoLocalize.Fody -Version 1.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Morris.AutoLocalize.Fody" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Morris.AutoLocalize.Fody" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Morris.AutoLocalize.Fody" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Morris.AutoLocalize.Fody --version 1.2.1
                    
#r "nuget: Morris.AutoLocalize.Fody, 1.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Morris.AutoLocalize.Fody@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Morris.AutoLocalize.Fody&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Morris.AutoLocalize.Fody&version=1.2.1
                    
Install as a Cake Tool

AutoLocalize

alternate text is missing from this package README image

NuGet version NuGet downloads

[Releases]

AutoLocalize is a Fody weaver that fills in ErrorMessageResourceType and ErrorMessageResourceName for your ValidationAttribute based annotations so you only declare your resource type once per assembly.

Problem description

It's not possible to have DataAnnotations validation attributes automatically pick up the current locale and present translated messages.

If you have translated resource files then you can set ErrorMessageResourceType and ErrorMessageResourceName on your validation attributes, but when you have thousands of them this can become troublesome and error-prone.

Solution

AutoLocalize allows you to specify ErrorMessageResourceType at project level, which will then be used on every instance of validation attributes that do not already have that property set. It will also add ErrorMessageResourceName based on a convention.

Getting started

  1. Install Fody (if you do not already use it) and AutoLocalize:
    <ItemGroup>
      <PackageReference Include="Fody" Version="6.*" PrivateAssets="All" />
      <PackageReference Include="Morris.AutoLocalize.Fody" Version="1.*" />
    </ItemGroup>
    
  2. Build once. Fody will create a FodyWeavers.xml file in your project if it does not exist.
  3. Add AutoLocalize to FodyWeavers.xml, then build again so weaving runs.
    <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
      <Morris.AutoLocalize />
    </Weavers>
    
  4. Specify the assembly-level attribute that tells the weaver which resource class to use. The default ErrorMessageResourceNamePrefix is "AutoLocalize_" if you do not set it.
    using Morris.AutoLocalize;
    using System.ComponentModel.DataAnnotations;
    
    [assembly: AutoLocalizeValidationAttributes(
        typeof(Resources.ValidationMessages),
        ErrorMessageResourceNamePrefix = "AutoLocalize_")]
    
    public class Person
    {
        [Required]
        [StringLength(50, MinimumLength = 2)]
        public string Name { get; set; } = string.Empty;
    }
    

Note: In case you don't know, to get a class for your resx file you need to set its Custom Tool property to PublicResXFileCodeGenerator.

What the weaver does

  • Finds every field/property decorated with an attribute descended from ValidationAttribute.
  • Ensures the ErrorMessageResourceType and ErrorMessageResourceName properties are set on each attribute.
  • Removes the [assembly:AutoLocalizeValidationAttributes] attribute from your build output.
  • Removes the assembly reference to Morris.AutoLocalize from your build output, so your runtime assembly has no extra dependencies.

ValidationAttribute update rules

  • ErrorMessageResourceType is set to the value specified in the [assembly:AutoLocalizeValidationAttributes({value here})] attribute you added to your project.
  • ErrorMessageResourceName is set to {Prefix}_{ShortAttributeName}
  • If either the ErrorMessageResourceType or ErrorMessage properties are set on the attribute then it is assumed you want a specific error message, so AutoLocalize will leave the validation attribute untouched.

Resource automatic naming algorithm

  • Prefix = AutoLocalize_ - this can be overridden when declaring your [assembly:AutoLocalizeValidationAttributes(typeof(MyResource))] attribute by setting the ErrorMessageResourceNamePrefix property.
  • ShortAttributeName = The class name of the attribute that descends from ValidationAttribute without the word Attribute at the end.
    • RequiredAttributeRequired
    • SomeGenericValidationAttribute<int>SomeGenericValidation

Examples

[assembly:AutoLocalizeValidationAttributes(typeof(DataAnnotationsMessages))]
Attribute ErrorMessageResourceType ErrorMessageResourceName
[Required] DataAnnotationsMessages AutoLocalize_Required
[Range(0, int.MaxValue, ErrorMessageResourceName="CannotBeNegative")] DataAnnotationsMessages <unaltered>
[EmailAddress(ErrorMessageResourceType=typeof(X), ErrorMessageResourceName="MustBeAValidEmailAddress")] <unaltered> <unaltered>
[EmailAddress(ErrorMessage="{0} must be a valid email address")] <unaltered> <unaltered>
[assembly:AutoLocalizeValidationAttributes(
   typeof(DataAnnotationsMessages),
   ErrorMessageResourceNamePrefix = "ValidationError_"
   )]
Attribute ErrorMessageResourceType ErrorMessageResourceName
[Required] DataAnnotationsMessages ValidationError_Required

Manifest of resource keys

During weaving a CSV file named after your project (e.g. MyProject.Morris.AutoLocalize.ValidationAttributes.csv) is written next to the project file. It contains all resource keys the weaver added or discovered:

ErrorMessageResourceName
AutoLocalize_Required
AutoLocalize_StringLength

Use this manifest to ensure your resource file contains entries for each key.

Automatic translation

There is a script in the root of this repository named TranslateLanguage.ps1.

This script will use ChatGPT API to translate your resx files into another language. Instructions on how to run it are at the top of that file.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 110 3/22/2026
1.2.0-Preview1 68 3/22/2026
1.1.0 99 3/15/2026
1.0.1 89 3/10/2026
1.0.0 101 3/10/2026
0.0.11 78 3/10/2026
0.0.10 83 3/9/2026
0.0.9 82 3/9/2026
0.0.8 80 3/9/2026
0.0.7 87 3/9/2026
0.0.6 86 3/9/2026
0.0.5 79 3/8/2026
0.0.4 85 3/8/2026
0.0.3 81 3/8/2026
0.0.2 82 3/8/2026
0.0.1 82 3/8/2026