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"
}
}
});
}
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"];
}
}
*.resx
文件存储在Resources
文件夹中,名称为Controllers.MyController.fr.resx
(其中有一个"Product"的条目)。
然而,它无法找到资源文件,因此"Product"从未以法语返回。我正在使用查询字符串,因此这是查询字符串:
localhost:3333/my?culture=fr
在视图中,@Localizer["Product"]
返回 "Product"。
有人能帮我找到缺少的内容吗?
编辑:
经过一些调查,我发现文化正在改变,但无法定位资源文件。 我正在使用VS2015,请问有人能帮忙吗?
fr-FR
。 - JK.