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

[Blazor] Data annotations localization support #12158

Open
qw-34 opened this issue Jul 14, 2019 · 14 comments
Open

[Blazor] Data annotations localization support #12158

qw-34 opened this issue Jul 14, 2019 · 14 comments
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-blazor-server feature-localization Pillar: Technical Debt Priority:1 Work that is critical for the release, but we could probably ship without severity-major This label is used by an internal tool triaged

Comments

@qw-34
Copy link

qw-34 commented Jul 14, 2019

In my case, I have custom localization service (like IStringLocalizer), that retrieve localization from database for user-specific culture. User culture configures in user settings, that may changes in application runtime, i.e. without page reload.

ILocalizationService
{
	string this[string key] { get; }
	string GetString(string key);
	void ChangeLanguage(CultureInfo culture);
}

Service registers as scoped, i.e. each user have own ILocalizationService with own specific culture settings.

services.AddScoped<ILocalizationService, LocalizationService>();

In components this works great, just inject service.

@page "/some-page"
@inject ILocalizationService T
<h1>@T["SomeText"]</h1>

Of course, this does not work in standard case of using data annotation attributes. For example:

User
{
	[Required]
	[Display(Name = "Name")]
	string Name { get; set; }
	
	[Display(Name = "Email")]
	string Email { get; set; }
}

Data annotation attributes can be provided with custom type with localized strings. Something like this:

User
{
	[Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(LocalizationResources))]
	[Display(Name = "Name", ResourceType = typeof(LocalizationResources))]
	string Name { get; set; }
	
	[Display(Name = "Email", ResourceType = typeof(LocalizationResources))]
	string Email { get; set; }
}

LocalizationResources
{
	public static string Name => GetString();
	public static string Email => GetString();
	public static string Validation_Required => GetString();
	
	public static Func<ILocalizationService> LocalizationServiceProvider { get; set; }

	public static string GetString([CallerMemberName] string key = null)
	{
		return LocalizationServiceProvider().GetString(key);
	}
}

Main problem of this approach is that localized strings in LocalizationResources must be as static properties. So we cant directly inject user-specific ILocalizationService. But we can try get user-specific ILocalizationService via Func<ILocalizationService> LocalizationServiceProvider that can be configured, for example, in Configure() method:

public void Configure(IServiceProvider serviceProvider)
{
	LocalizationResources.LocalizationServiceProvider = () => serviceProvider.GetRequiredService<ILocalizationService>();
}

Of course, this does not work, because of scope of serviceProvider in Configure() method differs from user-specific scopes. In order for this approach to work it is necessary in LocalizationResources.LocalizationServiceProvider uses some kind of context that will provide user-scoped IServiceProvider to resolve user-specific ILocalizationService.

So, at this moment, in server-side Blazor is exists any method to localize the data annotation attributes in non-resx scenarios? Have a plan to implement its in future?

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 15, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @qw-34.
We don't have any plans to support this in 3.0, but we'll look to bring support for the in the future.

@mkArtakMSFT mkArtakMSFT added enhancement This issue represents an ask for new feature or an enhancement to an existing one PRI: 2 - Preferred labels Jul 16, 2019
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Jul 16, 2019
@sven5
Copy link

sven5 commented Sep 2, 2019

Please also add support for DataAnnotationLocalizerProvider in Blazor - we already have this in MVC.

@agonzalezm
Copy link

will this be supported in 3.1? we also need full multi-language support for server-side application texts, data annotations validations, etc

@marinasundstrom
Copy link

marinasundstrom commented Apr 20, 2020

Localization is now supported in Blazor, usingMicrosoft.AspNetCore.Localization:

builder.Services.AddLocalization();

DataAnnotations are not really supported though.

After some quick investigation, my conclusion is that the DataAnnotationsValidator is the class that should be responsible for resolving the string from the preferred .resx file.

It could be implemented there by injecting StringLocalizer<T>(where T is the Component) into the ``DataAnnotationsValidatorand then pass it to EditContextDataAnnotationsExtensions .AddDataAnnotationsValidation```.

https://github.com/dotnet/aspnetcore/blob/057ec9cfae1831f91f1a0e01397059b20431db7b/src/Components/Forms/src/EditContextDataAnnotationsExtensions.cs

But you need to create adapters to handle the localization of specific data annotations, like in MVC:

https://github.com/dotnet/aspnetcore/tree/c565386a3ed135560bc2e9017aa54a950b4e35dd/src/Mvc/Mvc.DataAnnotations/src

It might be a bigger task than expected.

There is also an experimental ObjectGraphDataAnnotationsValidator that validates not only top-level properties. Maybe that will replace the DataAnnotationsValidator in the future.

@fededim
Copy link

fededim commented Mar 30, 2021

Is there any schedule to support Data Annotations localization in Blazor ? Without them Blazor apps are fully localized in a language, but the standard data annotation errors remain still in English....this "double language" is not great from the user perspective. Think about this feature, it's important for every application.

@TanayParikh TanayParikh changed the title [Blazor] Problems with localization of data annotation attributes in server-side Blazor [Blazor] Data annotations localization support Oct 18, 2021
@TanayParikh TanayParikh modified the milestones: Backlog, .NET 7 Planning Oct 18, 2021
@TanayParikh TanayParikh added the Priority:2 Work that is important, but not critical for the release label Oct 22, 2021
@mkArtakMSFT mkArtakMSFT modified the milestones: .NET 7 Planning, Backlog Nov 11, 2021
@ghost
Copy link

ghost commented Nov 11, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@daniel-scatigno
Copy link

With .net Core came the new "way" of Translating strings, using the IStringLocalizer is the new sensation!!
But it came incomplete, it works within MVC and it half works with Blazor. It's a mess in my opinion! This needs to be completed. We need to be able to localize those DataAnnotations Validators message

@ghost
Copy link

ghost commented Oct 12, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ghost
Copy link

ghost commented Jun 29, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@davhdavh
Copy link
Contributor

davhdavh commented Oct 6, 2023

This belonged in Blazor version 1.0... we are 5 YEARS from that date.

This issue absolutely blocks Blazor from being used for small projects that needs to support a language different than en-US.

On larger projects of-course you can choose shoot yourself in the foot and not use dataannotations, or you can have enough processes in place to make sure that everyone uses your custom dataannotations with your project level translations and have build processes setup to make sure that noone uses the built-in ones, all major undertakings in themselves.

@drlee91
Copy link

drlee91 commented Oct 6, 2023

i'm using mudblazor as designframework, there is alternatively instead of , there dataannotations is done in the razor page, so i could implement formvalidation multilingual for me.. i dont think anything will ever happen here with classic annotations :( maybe u can be inspired ;) https://mudblazor.com/components/form#simple-form-validation

@ghost
Copy link

ghost commented Dec 19, 2023

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@mkArtakMSFT mkArtakMSFT removed the Priority:2 Work that is important, but not critical for the release label Dec 19, 2023
@mkArtakMSFT mkArtakMSFT added Priority:1 Work that is critical for the release, but we could probably ship without Pillar: Dev Experience Pillar: Technical Debt and removed Pillar: Complete Blazor Web Pillar: Dev Experience labels Jan 17, 2024
@mduu
Copy link

mduu commented May 15, 2024

Really? This basic functionality is now laying in your backlog for five years...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-blazor-server feature-localization Pillar: Technical Debt Priority:1 Work that is critical for the release, but we could probably ship without severity-major This label is used by an internal tool triaged
Projects
None yet
Development

No branches or pull requests