问题描述
按照
项目1-控制器
使用MYCLASSLIBRARY;//外部项目[Route("api/[controller]")]公共类HelloController:控制器{私有只读IStringLocalizer< Test>_localizer;//外部项目类公共OrganisationController(IStringLocalizer< Test>本地化器){_mapper =映射器;_localizer =本地化程序;}[HttpGet("GetResource")]公共字符串GetResource(){返回_localizer ["Help"];}}
设置ResourcesPath时如何引用外部项目?
services.AddLocalization(lo => lo.ResourcesPath ="/MYCLASSLIBRARY");
不确定是否已经弄清楚了,以防万一,这是您可以在当前设置中完成的简单操作.只需替换下面的行
services.AddLocalization(lo => lo.ResourcesPath ="/MYCLASSLIBRARY");//外部项目〜怎么样?
与
services.AddLocalization();//删除目标程序集中的根文件夹,因此它将在您的MYCLASSLIBRARY程序集的根目录中查找文件
OR
将资源文件移动到文件夹"MYCLASSLIBRARY"下.只需确保在定义ResourcesPath时不要删除开头的"/"即可.
希望对您有帮助.
Following this documentation on how to implement globalization and localization using .NET Core, my goal is to store all my resources in a single global resource file located in a different project, (class library).
Project 1 - Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY"); //External project ~ How?
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("sv-SE")
};
opts.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
opts.SupportedCultures = supportedCultures;
opts.SupportedUICultures = supportedCultures;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
Project 2 - Class library
Project 1 - Controller
using MYCLASSLIBRARY; //External project
[Route("api/[controller]")]
public class HelloController : Controller
{
private readonly IStringLocalizer<Test> _localizer; //External project class
public OrganisationController(IStringLocalizer<Test> localizer)
{
_mapper = mapper;
_localizer = localizer;
}
[HttpGet("GetResource")]
public string GetResource()
{
return _localizer["Help"];
}
}
How can I reference an external project when setting ResourcesPath?
services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY");
Not sure you have already figured it out, just in case if you did not, here is the simple thing you could do in your current setup. Just replace the line below
services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY"); //External project ~ How?
with
services.AddLocalization(); //Removing the root folder in the target assembly hence it will look for the file in the root of the assembly of your MYCLASSLIBRARY
OR
Move your resource files under the folder "MYCLASSLIBRARY". Just make sure you don't remove the leading '/' when you define the ResourcesPath.
I hope it helps.
这篇关于.NET Core-全球化和本地化-类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!