ASP.NET Core 3.1 MVC中的本地化 [英] Localization in ASP.NET Core 3.1 MVC
问题描述
我正在尝试将本地化添加到我的ASP.NET Core 3.1 MVC项目中,不幸的是,我找不到任何文章或教程来展示如何轻松地做到这一点.
每个人都有一些我无法理解的问题.
有人可以告诉我一种简单的方法吗?好的,我尝试做syncfusion.com/blogs/post/…但我遇到了一个问题(options.RequestCultureProviders = new [] {new RouteDataRequestCultureProvider {IndexOfCulture = 1,IndexofUICulture = 1}};)它说RequestCultureProviders没有没有IndexofUICulture ...
我已经达到这个水平:
<代码> @使用Microsoft.AspNetCore.Localization@using Microsoft.Extensions.Localization@using System.Resources@model索引@inject IStringLocalizer< Index>定位器@inject IHtmlLocalizer< Index>htmlLocalizer@ {ViewData ["Title"] ="M-POS";//Microsoft.AspNetCore.Localization.IRequestCultureFeature requestCultureFeature;var requestCulture = CultureInfo.CurrentCulture;}< div class =文本中心">< h1 class ="display-4"> @localizer ["Welcome"]/h1< p> @localizer [学习"]/p<表类=表文化表">< tr>< td样式="宽度:50%;"文化</td>< td> @ requestCulture.DisplayName {@ requestCulture.Name}</td></tr>< tr>< td> UI文化</td>< td> @ requestCulture.Name</td></tr>< tr>< td> UICulture父</td>< td> @ requestCulture.Parent</td></tr>< tr>< td>日期</td>< td> @ DateTime.Now.ToLongDateString()</td></tr>< tr>< td>货币</td>< td>@(12345.00.ToString("c")))</td></tr>< tr>< td>货币</td>< td>@(12345.00.ToString("c")))</td></tr>< tr>< td>编号</td>< td>@(123.45m.ToString("F2")))</td></tr></table></div>
,然后在该文件夹中有一个名为Resources的文件夹,我有一个名为Resource.resx和Resource.en-US.resx的文件和StartUp.cs文件是这样的:
使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Threading.Tasks;使用Microsoft.AspNetCore.Builder;使用Microsoft.AspNetCore.Identity;使用Microsoft.AspNetCore.Identity.UI;使用Microsoft.AspNetCore.Hosting;使用Microsoft.AspNetCore.HttpsPolicy;使用Microsoft.EntityFrameworkCore;使用POS3.Data;使用Microsoft.Extensions.Configuration;使用Microsoft.Extensions.DependencyInjection;使用Microsoft.Extensions.Hosting;使用模型;使用Microsoft.AspNetCore.Identity.UI.Services;使用Microsoft.AspNetCore.Authorization;使用Microsoft.AspNetCore.Mvc.Authorization;使用Microsoft.AspNetCore.Mvc.Razor;使用System.Globalization;使用Microsoft.AspNetCore.Localization;使用Microsoft.Extensions.Options;使用Microsoft.AspNetCore.Localization.Routing;使用Microsoft.AspNetCore.Routing;使用Microsoft.CodeAnalysis.Options;使用Microsoft.AspNetCore.Http;使用System.Xml.Linq;命名空间POS3{公共类创业{公共启动(IConfiguration配置){配置=配置;}公共IConfiguration配置{}//此方法由运行时调用.使用此方法将服务添加到容器.公共无效ConfigureServices(IServiceCollection服务){services.AddDbContext< ApplicationDbContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))));services.AddDefaultIdentity< UserAccount>().AddRoles< IdentityRole>().AddEntityFrameworkStores< ApplicationDbContext>();services.AddRazorPages();//本地化启动services.AddSingleton< IHttpContextAccessor,HttpContextAccessor>();services.AddLocalization(options => options.ResourcesPath ="Resources");services.AddMvc().AddViewLocalization();services.AddMvc().AddViewLocalization(options => options.ResourcesPath =资源").AddDataAnnotationsLocalization();services.Configure< RequestLocalizationOptions>(options =>{varsupportedCultures = new []{新的CultureInfo("ar-SA"),新的CultureInfo("en-US")};options.DefaultRequestCulture = new RequestCulture("en-US");options.SupportedCultures = supportCultures;options.SupportedUICultures = supportCultures;});//services.Configure<RequestLocalizationOptions>(options =>//{//varsupportedCultures = new []//{//new CultureInfo("en-US"),//new CultureInfo("ar-SA"),//新的CultureInfo("es"),//};//options.DefaultRequestCulture = new RequestCulture("en-US");//options.SupportedCultures = supportedCultures;//options.SupportedCultures = supportedCultures;////options.RequestCultureProviders = new [] {new RouteDataRequestCultureProvider {IndexOfCulture = 1,IndexOfCulture = 1}};//});services.AddMvcCore();services.AddAuthorization(options => {options.AddPolicy("readonlypolicy",builder =>builder.RequireRole("Admin","Manager","Cashier","User","Super User")));options.AddPolicy("writepolicy",builder =>builder.RequireRole("Admin","Manager","Super User")));});services.Configure< IdentityOptions>(options =>{//默认密码设置.options.Password.RequireDigit = false;options.Password.RequiredLength = 6;options.Password.RequiredUniqueChars = 1;options.Password.RequireNonAlphanumeric = false;options.Password.RequireUppercase = false;options.Password.RequireLowercase = false;});services.AddControllersWithViews();services.AddRazorPages();services.AddControllers(config =>{var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().建造();config.Filters.Add(new AuthorizeFilter(policy));});services.Configure< PasswordHasherOptions>(options =>options.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2);//services.AddSingleton<IEmailSender,EmailSender>();}//此方法由运行时调用.使用此方法来配置HTTP请求管道.公共无效配置(IApplicationBuilder应用程序,IWebHostEnvironment env){如果(env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseDatabaseErrorPage();}别的{app.UseExceptionHandler("/Home/Error");//HSTS的默认值为30天.您可能要针对生产方案更改此设置,请参见https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();//配置器本地化var localizationOptions = app.ApplicationServices.GetService< IOptions< RequestLocalizationOptions>>>().Value;app.UseRequestLocalization(localizationOptions);app.UseAuthentication();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(名称:默认",模式:"{controller = Home}/{action = Index}/{id?}");endpoints.MapRazorPages();});}}}
现在我看不到resource.en-US.resx文件值,我想我缺少了一些东西.
问题1:与此相关的问题(options.RequestCultureProviders = new [] {new RouteDataRequestCultureProvider {IndexOfCulture = 1,IndexofUICulture = 1}};)它表示RequestCultureProviders没有IndexofUICulture ...
下载
Index.cshtml:
测试屏幕截图:
ASP.NET中的全球化和本地化核心
向ASP.NET Core应用程序添加本地化
如何在ASP.NET Core Web API中使用本地化
I'm trying to add localization to my ASP.NET Core 3.1 MVC project, unfortunately I could not find any article or tutorial that shows how to do it in an easy way.
Each and everyone is having some issues that I could not understand.
Could anyone please show me an easy way to do this? ok, I tried to do syncfusion.com/blogs/post/… but I faced an issue with this (options.RequestCultureProviders = new[] { new RouteDataRequestCultureProvider { IndexOfCulture = 1, IndexofUICulture = 1 } };) it says that RequestCultureProviders doesn't have IndexofUICulture......
I have reached this level:
@using Microsoft.AspNetCore.Localization
@using Microsoft.Extensions.Localization
@using System.Resources
@model Index
@inject IStringLocalizer<Index> localizer
@inject IHtmlLocalizer<Index> htmlLocalizer
@{
ViewData["Title"] = "M-POS";
//Microsoft.AspNetCore.Localization.IRequestCultureFeature requestCultureFeature;
var requestCulture = CultureInfo.CurrentCulture;
}
<div class="text-center">
<h1 class="display-4">@localizer["Welcome"]</h1>
<p>@localizer["Learn"]</p>
<table class="table culture-table">
<tr>
<td style="width:50%;">Culture</td>
<td>@requestCulture.DisplayName {@requestCulture.Name}</td>
</tr>
<tr>
<td>UI Culture</td>
<td>@requestCulture.Name</td>
</tr>
<tr>
<td>UICulture Parent</td>
<td>@requestCulture.Parent</td>
</tr>
<tr>
<td>Date</td>
<td>@DateTime.Now.ToLongDateString()</td>
</tr>
<tr>
<td>Currency</td>
<td>
@(12345.00.ToString("c"))
</td>
</tr>
<tr>
<td>Currency</td>
<td>
@(12345.00.ToString("c"))
</td>
</tr>
<tr>
<td>Number</td>
<td>
@(123.45m.ToString("F2"))
</td>
</tr>
</table>
</div>
and then I have a folder called Resources inside that folder I have a file called Resource.resx and Resource.en-US.resx and the StartUp.cs file is like that:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using POS3.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Models;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Razor;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Localization.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.CodeAnalysis.Options;
using Microsoft.AspNetCore.Http;
using System.Xml.Linq;
namespace POS3
{
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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<UserAccount>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
//localization startup
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc().AddViewLocalization();
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("ar-SA"),
new CultureInfo("en-US")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
//services.Configure<RequestLocalizationOptions>(options =>
//{
// var supportedCultures = new[]
// {
// new CultureInfo("en-US"),
// new CultureInfo("ar-SA"),
// new CultureInfo("es"),
//};
// options.DefaultRequestCulture = new RequestCulture("en-US");
// options.SupportedCultures = supportedCultures;
// options.SupportedCultures = supportedCultures;
// //options.RequestCultureProviders = new[] { new RouteDataRequestCultureProvider { IndexOfCulture = 1, IndexOfCulture = 1 } };
//});
services.AddMvcCore();
services.AddAuthorization(options => {
options.AddPolicy("readonlypolicy",
builder => builder.RequireRole("Admin", "Manager", "Cashier", "User", "Super User"));
options.AddPolicy("writepolicy",
builder => builder.RequireRole("Admin", "Manager", "Super User"));
});
services.Configure<IdentityOptions>(options =>
{
// Default Password settings.
options.Password.RequireDigit = false;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
});
services.AddControllersWithViews();
services.AddRazorPages();
services.AddControllers(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
services.Configure<PasswordHasherOptions>(options =>
options.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2
);
//services.AddSingleton<IEmailSender, EmailSender>();
}
// 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.UseDatabaseErrorPage();
}
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();
//configer localization
var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(localizationOptions);
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
now I can't see resource.en-US.resx file values I think I'm missing something.
Q1: an issue with this (options.RequestCultureProviders = new[] { new RouteDataRequestCultureProvider { IndexOfCulture = 1, IndexofUICulture = 1 } };) it says that RequestCultureProviders doesn't have IndexofUICulture......
Download the samples you will find RouteDataRequestCultureProvider
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
namespace LocalizationSampleSingleResxFile
{
public class RouteDataRequestCultureProvider : RequestCultureProvider
{
public int IndexOfCulture;
public int IndexofUICulture;
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
if (httpContext == null)
throw new ArgumentNullException(nameof(httpContext));
string culture = null;
string uiCulture = null;
culture = uiCulture = httpContext.Request.Path.Value.Split('/')[IndexOfCulture]?.ToString();
var providerResultCulture = new ProviderCultureResult(culture, uiCulture);
return Task.FromResult(providerResultCulture);
}
}
}
Q2: Change name of your resource
You already named en-US
, so your resource file name must format with en-US
.
Index.cshtml:
Screenshots of test:
Globalization and localization in ASP.NET Core
Adding Localisation to an ASP.NET Core application
How to Use Localization in an ASP.NET Core Web API
这篇关于ASP.NET Core 3.1 MVC中的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!