gpt4 book ai didi

c# - .NET Core - 全局化和本地化 - 类库

转载 作者:行者123 更新时间:2023-11-30 17:34:04 25 4
gpt4 key购买 nike

正在关注 this关于如何使用 .NET Core 实现全局化和本地化的文档,我的目标是将我的所有资源存储在位于不同项目(类库)中的单个全局资源文件中。

项目 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();
}
}

项目 2 - 类库

enter image description here

项目 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"];
}
}

设置ResourcesPath时如何引用外部项目?

services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY");

最佳答案

不确定您是否已经弄明白了,以防万一您还没有弄清楚,这是您可以在当前设置中执行的简单操作。只需替换下面的行

services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY");//外部项目~怎么样?

services.AddLocalization();//删除目标程序集中的根文件夹,因此它将在您的 MYCLASSLIBRAY 程序集的根目录中查找文件

将您的资源文件移动到“MYCLASSLIBRAY”文件夹下。只需确保在定义 ResourcesPath 时不删除前导“/”即可。

希望对你有帮助。

关于c# - .NET Core - 全局化和本地化 - 类库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com