Using Localization with DataAnnotation where do you put the ressources file and how to access it

Boucourt 85 Reputation points
2024-07-02T14:57:31.1766667+00:00

I am using .Net Core 8

I succeed to use StringLocalizer and HtmlstringLocalizer

I want to localized DataAnnotation the

[Display(Name = "PhotoEpisode")] and the error message

[Required(ErrorMessage = "Le sous-titre de l'épisode est requis.")]

In program.cs I add

builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services
	.AddMvc()
	.AddDataAnnotationsLocalization(
		options =>
		{
			options.DataAnnotationLocalizerProvider =
				(type, factory) =>
				{
					var assemblyName =
						new AssemblyName(
							typeof(SharedResources)
									.GetTypeInfo()
									.Assembly.FullName!);
					return factory.Create("SharedResource", assemblyName.Name!);
				};
		});

I I add the ressource file there

User's image

In my .ress file

PhotoEpisode Photo depicting the episode

In my DTO

		/// <summary>
		/// Le record contenant la photo associé à l'épisode
		/// </summary>
		[Display(Name = "PhotoEpisode")]
		public Photo? PhotoEpisode { get; set; }

In my Razor view page

				
<div class="w-25">
	<label asp-for="PhotoEpisode" class="form-label pt"></label>
</div>


ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,640 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 3,455 Reputation points Microsoft Vendor
    2024-07-03T05:06:42.1+00:00

    Hi @Boucourt,

    Firstly, you may misspell the word, it should be typeof(SharedResource) instead of typeof(SharedResources).

    Be sure configure the localization in Program.cs like below:

    
    builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
    builder.Services
        .AddMvc()
        .AddDataAnnotationsLocalization(
            options =>
            {
                options.DataAnnotationLocalizerProvider =
                    (type, factory) =>
                    {
                        var assemblyName =
                            new AssemblyName(
                                typeof(SharedResource)
                                        .GetTypeInfo()
                                        .Assembly.FullName!);
                        return factory.Create("SharedResource", assemblyName.Name!);
                    };
            });
    var app = builder.Build();
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    var supportedCultures = new[]
    {
        new CultureInfo("en-US"), // Default culture
        new CultureInfo("fr-CA"), // French culture
        // Add more cultures as needed
    };
    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("en-US"),
        SupportedCultures = supportedCultures,
        SupportedUICultures = supportedCultures
    });
    app.UseAuthorization();
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.Run();
    

    If the.resx file content which you shared in the question is SharedResource.fr-CA.resx. You need send request https://localhost:portNumber/xxx?culture=fr-CA. The label will display the value:Photo depicting the episode.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena


0 additional answers

Sort by: Most helpful