<span> shows this in UTF-8 encoding. Net Core 6.

Volk Volk 571 Reputation points
2022-09-11T09:49:47.257+00:00

Hi!

I have an input form for authentication, but in other languages (except latin unicode) I see strange symbols in @language.Getkey("required"). In other places of the site everything works correctly - the problem is only when I put the text in the <span> tag. How do I see the correct text in other cultures besides en-GB?

Thanks!

<head>  
 <meta charset="utf-8">  
</head>  


<div class="form-group row">  
  
                    <div class="col-2">  
                        <label asp-for="Email">@language.Getkey("email*")</label>  
                    </div>  
  
                    <div class="col-10">  
                        <input asp-for="Email" class="form-control"/>  
                    </div>              
  
                    <div class="col-10">  
                        <span asp-validation-for="Email" class="text-danger">@language.Getkey("required")</span>  
  
                    </div>  
  
                </div>  

239842-simple.png

239807-utf-8.png

Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
687 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,526 questions
{count} votes

Accepted answer
  1. JasonPan - MSFT 5,456 Reputation points Microsoft Vendor
    2022-09-20T04:56:09.293+00:00

    Hi @Volk Volk

    I see that you are using asp-validation-for in span tag, and you also use jquery validate to overwrite the error message, and it not working.

    Your code has some issue. You are missing '' It should be like :

    messages:{  
        Login: '@language.Getkey("unique_login_is_required")',  
        ....  
      
    }  
    

    And I have a workaround for you, you can remove asp-validation-for="" in your span tag, and it will fix the issue.


    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,
    Jason

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Volk Volk 571 Reputation points
    2022-09-15T14:26:34.17+00:00

    I don't understand, what else is there to explain? Everything is simple.
    I use this authentication (if it even plays a role in this problem).

    241516-identity11.png

    Any code where <span> is used together with asp-validation-form and class="text-danger" gives out all sorts of nonsense instead of Cyrillic letters.
    In other types of nodes and generally everywhere (even on the same page), Cyrillic shows correctly.

    For example:

    241517-a.png241486-b.png

    The code of this page is as follows:

    @model Project.Models.ApplicationUser  
    @{  
        ViewData["Title"] = "Create";  
    }  
    <br />  
      
    @inject LanguageService language  
      
    <h2 class="text-info">@language.Getkey("registration")</h2>  
      
    <form asp-action="Create" method="post" id="user-form" enctype="multipart/form-data">  
        <div class="p-4 rounded border">  
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
            <h3>@ViewBag.message</h3>  
      
            <div class="form-group row">  
                <div class="col-2">  
      
                    <label asp-for="Login">@language.Getkey("login_name*")</label>  
      
                </div>  
                <div class="col-5">  
                    <input asp-for="Login" class="form-control" exp/>  
                </div>  
      
                <span asp-validation-for="Login" class="text-danger">@language.Getkey("login_requires")</span>  
                 
            </div>  
      
            <div class="form-group row">  
                <div class="col-2">  
      
                    <label asp-for="Email">@language.Getkey("email*")</label>  
      
                </div>  
                <div class="col-5">                  
                    <input asp-for="Email" class="form-control"/>  
                </div>              
      
                <span asp-validation-for="Email" class="text-danger">@language.Getkey("required")</span>  
      
            </div>  
      
            <br></br>  
      
            <div class="form-group row">  
                <div class="col-2">  
      
                    <label asp-for="PasswordHash">@language.Getkey("password*")</label>  
      
                </div>  
                <div class="col-5">  
      
                    <form action="">  
                    <div class="form-item">  
                        <input asp-for="PasswordHash" type="password" class="form-control form-control-lg" id="password" placeholder="password"/>  
                        <i class="fas fa-eye" id="eye"></i>  
                    </div>                          
                    </form>  
      
                </div>  
      
                <span asp-validation-for="PasswordHash" class="text-danger">@language.Getkey("password_requires")</span>  
      
            </div>  
      
            <br></br>  
      
            <div class="form-group row">  
                <div class="col-2">  
                      
                    <label asp-for="FirstName">@language.Getkey("first_name*")</label>  
      
                </div>  
                <div class="col-5">  
                    <input asp-for="FirstName" class="form-control"/>  
                </div>  
      
                <span asp-validation-for="FirstName" class="text-danger">@language.Getkey("required")</span>  
      
            </div>  
      
            <div class="form-group row">  
                <div class="col-2">  
      
                    <label asp-for="LastName">@language.Getkey("last_name*")</label>  
      
                </div>  
                <div class="col-5">  
                    <input asp-for="LastName" class="form-control" />  
                </div>  
      
                <span asp-validation-for="LastName" class="text-danger">@language.Getkey("required")</span>  
      
            </div>  
      
            <br></br>  
      
            <div class="form-group">  
      
                <a asp-area="Customer" asp-action="Index" asp-controller="Home" class="button large">@language.Getkey("general")</a>  
      
                <input type="submit" class="button large" value="@language.Getkey("register")"/>  
                  
            </div>  
        </div>  
    </form>  
      
    @section Scripts{  
      
        @{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
         
        <script>  
            $(document).ready(function () {  
                $("#user-form").validate({  
                    rules: {  
                         Login : {  
                            required: true  
                         },  
                         Email : {  
                            required: true  
                         },  
                         PasswordHash : {  
                            required: true  
                         },  
                         FirstName : {  
                            required: true  
                         },  
                         LastName : {  
                            required: true  
                         }  
                    },  
                    messages: {  
                          Login: @language.Getkey("unique_login_is_required"),  
                          Email: @language.Getkey("email_is_required"),  
                          PasswordHash: @language.Getkey("password_is_required"),  
                          FirstName: @language.Getkey("first_name_is_required"),  
                          LastName: @language.Getkey("last_name_is_required")  
                    }  
                });            
            });  
        </script>  
    }  
    

    My code responsible for changing the language is as follows:

    services.AddControllersWithViews();  
          
                 services.AddSingleton<LanguageService>();  
                 services.AddLocalization(options => options.ResourcesPath = "Resources");  
          
                 services.AddMvc()  
                    .AddViewLocalization()  
                    .AddDataAnnotationsLocalization(options =>  
                    {  
                        options.DataAnnotationLocalizerProvider = (type, factory) =>  
                        {  
                            var assemblyName = new AssemblyName(typeof(ShareResource).GetTypeInfo().Assembly.FullName);  
                            return factory.Create("ShareResource", assemblyName.Name);  
                        };  
                    });  
          
                 services.Configure<RequestLocalizationOptions>(  
                     options =>  
                     {  
                         var supportedCultures = new List<CultureInfo>  
                             {                             
                                 new CultureInfo("ru-RU"),  
                                 new CultureInfo("en-En")  
                             };  
          
                         options.DefaultRequestCulture = new RequestCulture(culture: "ru-RU", uiCulture: "ru-RU");  
          
                         options.SupportedCultures = supportedCultures;  
                         options.SupportedUICultures = supportedCultures;  
          
                         options.RequestCultureProviders.Insert(0, new QueryStringRequestCultureProvider());                     
                     });  
      
      
     using Microsoft.Extensions.Localization;  
     using System.Reflection;  
          
     namespace Project.Models  
     {  
         public class LanguageService  
      {  
             private IStringLocalizer _localizer;  
          
             public LanguageService(IStringLocalizerFactory factory)  
             {  
                 var type = typeof(ShareResource);  
                 var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);  
                 _localizer = factory.Create("SharedResource", assemblyName.Name);  
             }  
          
             public LocalizedString Getkey(string key)  
             {               
                 return _localizer[key];  
             }  
         }  
     }  
    

    Well, there are, as always, files of the .resx
    SharedResource.en-GB.resx
    SharedResource.ru-RU.resx
    SharedResource.de-DE.resx
    SharedResource.el-GR.resx

    241563-z.png

    That's it! There 's nothing more to tell.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.