我的网络应用程序从法语资源中获取翻译,我不知道为什么
my web app gets translation from the french resource, idk why
几周前我开始使用 asp.net 核心 3.0 开发 Web 应用程序,现在我到了必须开发本地化服务的阶段,为了简化我的代码,我将只编写新的“部分”所以他们在这里:
startup.cs:
ConfigureServices 方法:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc(option => option.EnableEndpointRouting = false)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("fr-FR"),
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddTransient<SharedResources>();
SharedResources.cs
位于应用根目录
Configure
方法
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
_ViewImports.cshtml:
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Backoffice.SharedResources> LangDefault
@addTagHelper *, Localisation
一个视图:
@using Microsoft.AspNetCore.Mvc.Localization
@model some-model
@inject IViewLocalizer Localizer
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">@Localizer["Sede"]</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">@Localizer["FirstName"]</a>
</li>
</ul>
</div>
我的应用仅从 Views.[SomeController].[SomeAction].fr.resx
获取法语翻译,我找不到如何制作 select 列表来更改语言,我找到的是 API应用程序或旧的 .net 核心,我发现 ,但它遗漏了一些要点,你能提供一个改编指南或一些帮助吗?谢谢
这是一个有效的演示:
/Shared/Partial:
@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions
@*@{
ViewData["Title"] = "Partial";
}
<h1>Partial</h1>*@
@{
var requestCulture = Context.Features.Get<IRequestCultureFeature>();
var cultureItems = LocOptions.Value.SupportedUICultures
.Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
.ToList();
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
method="post" class="form-horizontal" role="form">
<label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>
<select name="culture"
onchange="this.form.submit();"
asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
</select>
</form>
</div>
_layout.cshtml:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
<div>
@Localizer["Hello"]
</div>
<div class="col-sm-6 text-right">
@await Html.PartialAsync("Partial")
</div>
</div>
Startup.cs:
配置服务:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR"),
new CultureInfo("de-DE")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
配置:
app.UseHttpsRedirection();
var supportedCultures = new[] { "en-US", "fr-FR", "de-DE" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
app.UseStaticFiles();
家庭控制器:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
资源:
结果:
几周前我开始使用 asp.net 核心 3.0 开发 Web 应用程序,现在我到了必须开发本地化服务的阶段,为了简化我的代码,我将只编写新的“部分”所以他们在这里: startup.cs: ConfigureServices 方法:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc(option => option.EnableEndpointRouting = false)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("fr-FR"),
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddTransient<SharedResources>();
SharedResources.cs
位于应用根目录
Configure
方法
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
_ViewImports.cshtml:
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Backoffice.SharedResources> LangDefault
@addTagHelper *, Localisation
一个视图:
@using Microsoft.AspNetCore.Mvc.Localization
@model some-model
@inject IViewLocalizer Localizer
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">@Localizer["Sede"]</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">@Localizer["FirstName"]</a>
</li>
</ul>
</div>
我的应用仅从 Views.[SomeController].[SomeAction].fr.resx
获取法语翻译,我找不到如何制作 select 列表来更改语言,我找到的是 API应用程序或旧的 .net 核心,我发现
这是一个有效的演示:
/Shared/Partial:
@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions
@*@{
ViewData["Title"] = "Partial";
}
<h1>Partial</h1>*@
@{
var requestCulture = Context.Features.Get<IRequestCultureFeature>();
var cultureItems = LocOptions.Value.SupportedUICultures
.Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
.ToList();
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
method="post" class="form-horizontal" role="form">
<label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>
<select name="culture"
onchange="this.form.submit();"
asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
</select>
</form>
</div>
_layout.cshtml:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
<div>
@Localizer["Hello"]
</div>
<div class="col-sm-6 text-right">
@await Html.PartialAsync("Partial")
</div>
</div>
Startup.cs:
配置服务:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR"),
new CultureInfo("de-DE")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
配置:
app.UseHttpsRedirection();
var supportedCultures = new[] { "en-US", "fr-FR", "de-DE" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
app.UseStaticFiles();
家庭控制器:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
资源:
结果: