【问题标题】:ASP.NET Core localize .cs file in Razor PagesASP.NET Core 本地化 Razor 页面中的 .cs 文件
【发布时间】:2021-07-04 01:16:54
【问题描述】:

在 ASP.NET 5.0 上有一个带有 Identity 的项目。 在如何翻译 .cs 文件 Razor Page 中的消息方面需要帮助。

Startup.cs 看起来像这样

namespace localizeTest
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
            services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization();

            services.Configure<RequestLocalizationOptions>(
            opt =>
            {
                var suppoortedCulteres = new List<CultureInfo>
                {
                    new CultureInfo("ru"),
                    new CultureInfo("en")
                };
                opt.DefaultRequestCulture = new RequestCulture("ru");
                opt.SupportedCultures = suppoortedCulteres;
                opt.SupportedUICultures = suppoortedCulteres;
            }
            );

            services.AddDNTCaptcha(options =>
            {
                options.UseCookieStorageProvider();
            });

            string connection = Configuration["ConnectionStrings:DefaultConnection"];
            ServerVersion vesrion = ServerVersion.AutoDetect(connection);

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySql(connection, vesrion));
            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddControllersWithViews();

            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();
            app.UseRequestLocalization(app.ApplicationServices.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);


            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

在 Resources 文件夹中创建了两个文件:

/Areas/Identity/Pages/Account/Login.en.resx

/Areas/Identity/Pages/Account/Login.ru.resx

示例页面 区域/身份/页面/帐户/Login.cshtml

@page
@model LoginModel
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer localizer

@{
    ViewData["Title"] = localizer["Login"];
}

<div class="container">
    <div class="row h-100">
        <div class="col-12 col-md-10 mx-auto my-auto">
            <div class="card auth-card">
                <div class="card-body">
                    <h1 class="mb-4">@ViewData["Title"]</h1>
                    <h4 class="pb-2">@localizer["Welcome"]</h4>
                    <form id="account" method="post">
                        <div asp-validation-summary="All" class="text-danger"></div>
                        <div class="form-group">
                            <label asp-for="Input.Email">@localizer["Email"]</label>
                            <input asp-for="Input.Email" class="form-control" placeholder="@localizer["YourEmail"]" required="">
                            <span asp-validation-for="Input.Email" class="text-danger"></span>
                        </div>

                        <div class="form-group">
                            <label asp-for="Input.Password">@localizer["Password"]</label>
                            <input asp-for="Input.Password" class="form-control" placeholder="@localizer["YourPassword"]" required="">
                            <span asp-validation-for="Input.Password" class="text-danger"></span>
                        </div>

                        <div class="form-group">
                            <div class="custom-control custom-checkbox">
                                <input class="custom-control-input" asp-for="Input.RememberMe">
                                <label class="custom-control-label" asp-for="Input.RememberMe">@localizer["RememberMe"]</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">@localizer["SignIn"]</button>
                        </div>

                        <div class="form-group">
                            <p>
                                <a id="forgot-password" asp-page="./ForgotPassword">@localizer["ForgotPass"]</a>
                            </p>
                            <p>
                                <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">@localizer["ЗарегестрироватьсяКакНовый"]</a>
                            </p>
                            <p>
                                <a id="resend-confirmation" asp-page="./ResendEmailConfirmation">@localizer["EmailConf"]</a>
                            </p>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

好的,在页面视图中进行本地化!

但是,页面模型中的本地化不起作用。 文件区域/身份/页面/帐户/Login.cshtml.cs

namespace localizeTest.Areas.Identity.Pages.Account
{
    [AllowAnonymous]
    public class LoginModel : PageModel
    {
        private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;
        private readonly ILogger<LoginModel> _logger;
        private readonly IStringLocalizer<LoginModel> _stringLocalizer;

        public LoginModel(SignInManager<IdentityUser> signInManager, 
            ILogger<LoginModel> logger,
            UserManager<IdentityUser> userManager, 
            IStringLocalizer<LoginModel> stringLocalizer)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
            _stringLocalizer = stringLocalizer;
        }

        [BindProperty]
        public InputModel Input { get; set; }

        public IList<AuthenticationScheme> ExternalLogins { get; set; }

        public string ReturnUrl { get; set; }

        [TempData]
        public string ErrorMessage { get; set; }

        public class InputModel
        {
            [Display(Name = "Email")]
            [Required(ErrorMessage = "{0} is required")]
            [EmailAddress]
            public string Email { get; set; }

            [Display(Name = "Password")]
            [Required(ErrorMessage = "{0} is required")]
            [DataType(DataType.Password)]
            public string Password { get; set; }

            [Display(Name = "RememberMe")]
            public bool RememberMe { get; set; }
        }

        public async Task OnGetAsync(string returnUrl = null)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                ModelState.AddModelError(string.Empty, ErrorMessage);
            }

            returnUrl ??= Url.Content("~/Books/List");

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            ReturnUrl = returnUrl;
        }

        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/Books/List");

            string WrongLoggin = _stringLocalizer["НеВерныйЛогин"].Value;

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);
                }
                if (result.RequiresTwoFactor)
                {
                    return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return RedirectToPage("./Lockout");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, WrongLoggin);
                    return Page();
                }
            }

            // If we got this far, something failed, redisplay form
            return Page();
        }
    }
}

如何翻译.cs文件中的文本,如何使其工作?

[Display(Name = "如何翻译文本?")]

[Required(ErrorMessage = "{0} 如何翻译文本?")]

Img Login Ru

Img Login En

我尝试了控制器页面的示例。

创建了名为 LoginModel+InnerModel.en.resxLoginModel+InnerModel.ru.resx 的资源。

但它没有给出结果。

附:抱歉谷歌翻译,但我需要帮助。

附注 2。对这个video的例子做了本地化

【问题讨论】:

    标签: asp.net-core localization asp.net-core-mvc


    【解决方案1】:

    首先您需要在您的Stratup 中添加AddDataAnnotationsLocalization

     services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddRazorPages().AddDataAnnotationsLocalization();
    

    然后你需要将你的资源文件命名为

    Areas.Identity.Pages.Account.LoginModel+InputModel.en-US.resx
    

    对于LoginModel中的任何嵌套模型,您需要使用+而不是.

    有关详细信息,您可以查看文档: DataAnnotations localization.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-16
      • 2019-07-07
      • 2019-08-02
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2018-02-24
      相关资源
      最近更新 更多