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 Localization Roadmap? #22178

Closed
mysteryx93 opened this issue May 23, 2020 · 11 comments
Closed

Blazor Localization Roadmap? #22178

mysteryx93 opened this issue May 23, 2020 · 11 comments
Labels
area-blazor Includes: Blazor, Razor Components feature-localization investigate
Milestone

Comments

@mysteryx93
Copy link

According to this article,

A limited set of ASP.NET Core's localization scenarios are currently supported: IStringLocalizer<> is supported in Blazor apps.

IStringLocalizer is supported, and that's about it. The problem with IStringLocalizer is that

  1. It requires a separate resource file for each class -- which can grow exponentially in large projects
  2. Changing a value in the default language requires editing all other languages, very messy and prone to errors
  3. Some localized strings are shared between classes

I tried adding localization to validation attributes.

First with a custom DisplayNameAttribute with built-in localization. DataAnnotations Validation doesn't read this attribute though.

Then with Display(Name="FirstName", Type=typeof(Resources)), but it made the application crash (at least when the resource is in a different project).

So it seems there's really not much localization support at all for now... better leave out localization altogether.

I don't really need localization anyway, but wanted to structure my code in clean way that would make allowing localization easy. What are the plans for Blazer localization support in .NET 5? I'd be better to structure the code in a way that would be compatible for that.

IStringLocalizer really doesn't seem like a good solution... does it even work to localize DisplayAttribute for validation messages?

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label May 25, 2020
@mkArtakMSFT
Copy link
Member

Thanks for contacting us.
Please share a small repro app so that we can investigate the crash you're facing.

@mkArtakMSFT mkArtakMSFT added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label May 26, 2020
@mysteryx93
Copy link
Author

mysteryx93 commented May 26, 2020

It's not because of being in a different project; within the same project it also crashes.

System.InvalidOperationException: Cannot retrieve property 'Name' because localization failed.  Type 'HanumanInstitute.WebStore.Properties.Resources' is not public or does not contain a public static string property with the name 'ProcessOrderAddressFirstName'.
   at System.ComponentModel.DataAnnotations.LocalizableString.<>c__DisplayClass12_1.<GetLocalizableValue>b__2()
   at System.ComponentModel.DataAnnotations.LocalizableString.GetLocalizableValue()
   at System.ComponentModel.DataAnnotations.ValidationContext.GetDisplayName()
   at System.ComponentModel.DataAnnotations.ValidationContext.get_DisplayName()
   at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
   at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
   at System.ComponentModel.DataAnnotations.Validator.TryValidate(Object value, ValidationContext validationContext, ValidationAttribute attribute, ValidationError& validationError)
   at System.ComponentModel.DataAnnotations.Validator.GetValidationErrors(Object value, ValidationContext validationContext, IEnumerable`1 attributes, Boolean breakOnFirstError)
   at System.ComponentModel.DataAnnotations.Validator.TryValidateProperty(Object value, ValidationContext validationContext, ICollection`1 validationResults)
   at Microsoft.AspNetCore.Components.Forms.ObjectGraphDataAnnotationsValidator.ValidateField(EditContext editContext, ValidationMessageStore messages, FieldIdentifier& fieldIdentifier)
   at Microsoft.AspNetCore.Components.Forms.ObjectGraphDataAnnotationsValidator.<OnInitialized>b__7_1(Object sender, FieldChangedEventArgs eventArgs)
   at Microsoft.AspNetCore.Components.Forms.EditContext.NotifyFieldChanged(FieldIdentifier& fieldIdentifier)
   at Radzen.Blazor.RadzenTextBox.OnChange(ChangeEventArgs args)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

I have a repro project, can I send it to you via email?

There's also the fact that there's no support for displaying the field name in a label; although such a Label For component can be implemented manually.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels May 26, 2020
@mkArtakMSFT
Copy link
Member

@mysteryx93 we don't need a private app for this - just a minimal repro app hosted in GitHub is what we're looking for.

@mkArtakMSFT mkArtakMSFT added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels May 27, 2020
@mysteryx93
Copy link
Author

I have a minimal repro but won't put it on GitHub

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels May 27, 2020
@mkArtakMSFT
Copy link
Member

@mysteryx93 to clarify, by minimal repro we refer to the minimum required changes on top of a new project, which will reproduce the issue. We are not interested nor we have enough resources to look into customer projects.
If you can provide such a repro project hosted in a GitHub public repo, we will try the scenario out.

@JvanderStad
Copy link

@mysteryx93 You can roll your own localizer. I'm using Json files and custom attributes to make it work in Blazor. I'm using translation over multiple projects.

  • It requires a separate resource file for each class -- which can grow exponentially in large projects

You can design your own strategy. I use one translation file (per language) per 'Area'.

  • Changing a value in the default language requires editing all other languages, very messy and prone to errors

Same here, design your own strategy. If you don't change your keys, create an 'override' mechanism

  • Some localized strings are shared between classes

You can share translations by using the same type. Merge multiple json files to share translations between different area's (base translation + custom translations).

	[Translation("Translation\\Shared")]
	public class SharedTranslation
	{
		//Dummy class to group localization, can stay empty
	}

	[Translation("Translation\\Button")]
	public class ButtonTranslation
	{
		//Dummy class to group localization, can stay empty
	}

	[Translation("Translation\\ValidationRules")]
	public class ValidationRulesTranslation
	{

	}

I used https://github.com/rwwilden/AspNet5Localization for inspiration

@mkArtakMSFT mkArtakMSFT added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jun 1, 2020
@mysteryx93
Copy link
Author

mysteryx93 commented Jun 1, 2020

@JvanderStad, it doesn't seem your solution has any reference to DataAnnotations and DisplayAttribute to use and localize them

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jun 1, 2020
@mysteryx93
Copy link
Author

ok here's a minimal repro
BlazorApp1.zip

@mkArtakMSFT mkArtakMSFT added investigate and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jun 2, 2020
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Jun 2, 2020
@GioviQ
Copy link

GioviQ commented Sep 1, 2020

@SteveSandersonMS
Starting from this custom sql based localizer
https://github.com/damienbod/AspNetCoreLocalization/tree/master/src/Localization.SqlLocalizer
and adding package Microsoft.AspNetCore.Components.DataAnnotations.Validation 3.2.0-rc1.20223.4,
I cannot find a way to localize data annotation error messages.

Is there a way to use my custom localizer factory?

I also read:
#12158
dotnet/AspNetCore.Docs#17201

Thank you

@ghost
Copy link

ghost commented Oct 9, 2020

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.

@SteveSandersonMS
Copy link
Member

The original issues described here no longer seem to apply, so I'm going to close this. If anyone still has issues in this area, could you please post a new issue describing your scenario directly? Thanks!

It requires a separate resource file for each class -- which can grow exponentially in large projects

You can have whatever granularity you want. Have a single file for your entire application if you want.

Changing a value in the default language requires editing all other languages, very messy and prone to errors

True. This is innate to how resx works and not something we control in IStringLocalizer.

Some localized strings are shared between classes

Sounds like a restatement of the first point.

DataAnnotations Validation doesn't read this attribute though

DataAnnotations validation now does read localization from attributes. Working Blazor example: https://github.com/SteveSandersonMS/CarChecker/blob/master/Client/Pages/Index.razor#L38

@ghost ghost locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-localization investigate
Projects
None yet
Development

No branches or pull requests

5 participants