gen*_*ser 14 c# globalization asp.net-core-mvc asp.net-core
在尝试本地化我的应用程序时,我遵循了以下步骤:https: //docs.asp.net/en/latest/fundamentals/localization.html
这是我的代码:
Startup.cs
public List<IRequestCultureProvider> RequestCultureProviders { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.AddOptions();
services.AddTransient<IViewRenderingService, ViewRenderingService>();
services.AddTransient<IEmailSender, EmailSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory())),
EnableDirectoryBrowsing = true
});
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider
{
QueryStringKey = "culture",
UIQueryStringKey = "ui-culture"
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
MyController.cs
public class MyController : Controller
{
private readonly IViewRenderingService _viewRenderingService;
private IStringLocalizer<MyController> _localizer;
private MyOptions _options;
//Constructor for dependency injection principle
public MyController(IViewRenderingService viewRenderingService, IStringLocalizer<MyController> localizer, IOptions<MyOptions> options)
{
_viewRenderingService = viewRenderingService;
_localizer = localizer;
_options = options.Value;
}
[HttpGet]
public string Get()
{
// _localizer["Name"]
return _localizer["Product"];
}
}
Run Code Online (Sandbox Code Playgroud)
该*.resx文件存储在Resources文件夹中,其名称Controllers.MyController.fr.resx(包含"Product"条目).
但是,它无法找到资源文件,并且"Product"永远不会以法语返回.我正在使用查询字符串,所以这里是查询字符串:
localhost:3333/my?culture=fr
Run Code Online (Sandbox Code Playgroud)
同样在View中,@Localizer["Product"]返回"Product".
任何人都可以帮我找到丢失的东西吗?
编辑:
经过一番调查,我发现文化正在发生变化,但无法找到资源文件.我正在使用VS2015.有人可以帮忙吗?
小智 27
我有类似的问题.我发现我的项目中缺少" Localization.AspNetCore.TagHelpers "nuget packag.它看起来像是QueryStringRequestCultureProvider的必需包.
小智 5
我在 .net core 2.2 中遇到了同样的问题,我在调试器中看到了 SearchedLocation 属性

所以,我首先创建了文件 Controllers.HomesController.rsx,没有语言扩展名,带有访问修饰符“Public”来访问属性

和 localizer["Property] 我发现了良好的价值。
在我创建了 Controllers.HomeController.fr.resx 之后,他发现了 url 中文化的良好价值。
如果程序集的根命名空间与程序集名称不同:
默认情况下本地化不起作用。由于在程序集中搜索资源的方式,本地化失败。RootNamespace 是一个构建时值,不可用于执行进程。
如果 RootNamespace 与 AssemblyName 不同,请在 AssemblyInfo.cs 中包含以下内容(参数值替换为实际值):
using System.Reflection;
using Microsoft.Extensions.Localization;
[assembly: ResourceLocation("Resource Folder Name")]
[assembly: RootNamespace("App Root Namespace")]
Run Code Online (Sandbox Code Playgroud)
就我而言,问题是我指定:
services.AddLocalization(options => options.ResourcesPath = "Resources");
Run Code Online (Sandbox Code Playgroud)
和:
services.AddLocalization(options => options.ResourcesPath = "Resources");
Run Code Online (Sandbox Code Playgroud)
我的 DataAnnotations.resx 资源文件也位于“Resources”命名空间下。这导致本地化程序在“MyApp.Resources.Resources.DataAnnotations”而不是“MyApp.Resources.DataAnnotations”中搜索字符串。
将第一行更改为:
services.AddLocalization();
Run Code Online (Sandbox Code Playgroud)
帮助解决了问题。
| 归档时间: |
|
| 查看次数: |
7510 次 |
| 最近记录: |