I have a Blazor server application and I'm following Microsoft's recommended approach for i18n and l10n together with input validation via a model class.
One of my Blazor components looks like this:
// MyComponent.razor
@inject IStringLocalizer<Main> _loc
<EditForm Model="@_myModel" OnValidSubmit="@Save">
<DataAnnotationsValidator/>
<ValidationSummary/>
<label for="my-input">@_loc["Title"]</label>
<InputText id="my-input" @bind-Value="_myModel.Title"/>
<button type="submit">@_loc["Save"]</button>
</EditForm>
@code {
private readonly MyModel _myModel = new();
private async Task Save()
{
// Do something fancy
}
}
The model looks like this:
public class MyModel
{
[Required(ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(Main))]
public string Title { get; set; } = string.Empty;
}
And last but not least there are regular C# resource files with the following entries:
<!-- Main.resx -->
<data name="RequiredError" xml:space="preserve">
<value>The field {0} is mandatory</value>
</data>
<data name="Title" xml:space="preserve">
<value>Title</value>
</data>
<data name="Save" xml:space="preserve">
<value>Save</value>
</data>
<!-- Main.de.resx -->
<data name="RequiredError" xml:space="preserve">
<value>Das Feld {0} ist obligatorisch</value>
</data>
<data name="Title" xml:space="preserve">
<value>Überschrift</value>
</data>
<data name="Save" xml:space="preserve">
<value>Speichern</value>
</data>
Now when an error occurs, the error message gets properly translated, but the argument of RequiredError is still in English.
So instead of Das Feld Überschrift ist obligatorisch I'm getting Das Feld Title ist obligatorisch.
How can retrieve a completely localized string, including parameters like {0} within RequiredError? Is that even possible? Or do I have to add resource entries for every field, e. g. RequiredErrorTitle?
Thanks!