我一直试图遵循这两个教程添加本地化到我的.Net核心Razor网络应用程序。
我试着从头开始创建项目。我已经尝试增加我现有的项目。我尝试过使用LocalizeTagHelper和SharedCultureLocalizer选项,但没有成功。
我只是无法让“Home”或“myApp”这样的文本进行更改。
当我在下拉列表中选择一种语言时,语言会在URL中指定(见下面),但是我的文本不会改变。
下拉组件&家庭文本x2:
网址:
My软件包:
Index.cshtml
@page
@model IndexModel
@using LazZiya.ExpressLocalization
@inject ISharedCultureLocalizer _loc
@{
ViewData["Title"] = @_loc["myApp"];
}
<body>
<h1 class="display-4" localize-content>Home</h1>
<header>
<div class="bg-img">
<div class="container-title">
<div class="block-title block-title1">
<language-nav cookie-handler-url="@Url.Page("/Index", "SetCultureCookie", new { area="", cltr="{0}", returnUrl="{1}" })"></language-nav>
<br>
</div>
<div class="block-title block-title2 d-none d-md-block d-lg-block d-xl-block"><img src="/image/title_image.png" class="img-fluid"></div>
</div>
</div>
</header>
<main>
<div class="row_primary">
</div>
</main>
</body>
Index.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace myApp.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
public IActionResult OnGetSetCultureCookie(string cltr, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cltr)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
}
}
Startup.cs
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using LazZiya.ExpressLocalization;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using myApp.wwwroot.LocalizationResources;
namespace myApp
{
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.AddRazorPages();
var cultures = new[]
{
new CultureInfo("de"),
new CultureInfo("fr"),
new CultureInfo("en"),
};
services.AddRazorPages().AddExpressLocalization<ExpressLocalizationResource, ViewLocalizationResource >( ops =>
{
ops.ResourcesPath = "LocalizationResources";
ops.RequestLocalizationOptions = o =>
{
o.SupportedCultures = cultures;
o.SupportedUICultures = cultures;
o.DefaultRequestCulture = new RequestCulture("en");
};
});
}
// 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();
}
else
{
app.UseExceptionHandler("/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.UseAuthorization();
app.UseRequestLocalization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
wwwroot
resx示例
resx性质
发布于 2020-05-02 14:46:41
我的resx文件的Build属性为“Content”,而不是“Embedded Resource”。我的LocSource.cs的Build属性是“嵌入式资源”,而不是“C#编译器”。
真的期待现在使用这个包,看起来它将使生活变得更容易。
发布于 2020-05-01 13:16:18
如果您使用的是V4.0.0,您不需要LazZiya.TagHelpers.Localization
,只需卸载它,因为LocalizeTagHelper
已在最新版本中移动到LazZiya.ExpressLocalization
。
因此,只需将本地化标签助手添加到_ViewImports.cshtml
,如下所示:
@addTagHelper *, LazZiya.ExpressLocalization
并将资源文件中字符串的访问修饰符更改为无代码生成
更多细节可在ExpressLocalization维基中找到
https://stackoverflow.com/questions/61529347
复制相似问题