.NET Core - 全球化和本地化 - 类库

Ref*_*eft 5 .net c# localization .net-core asp.net-core

按照有关如何使用 .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();
    }
}
Run Code Online (Sandbox Code Playgroud)

项目2-类库

在此输入图像描述

项目 1 - 控制器

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"];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY");
Run Code Online (Sandbox Code Playgroud)

小智 2

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

services.AddLocalization(lo => lo.ResourcesPath = "/MYCLASSLIBRARY"); //External project ~ How?

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

或者

将资源文件移至“MYCLASSLIBRARY”文件夹下。只要确保在定义 ResourcesPath 时没有删除前导“/”即可。

我希望它有帮助。