.NET Core - 全球化和本地化 - 类库 [英] .NET Core - Globalization and Localization - Class library

查看:18
本文介绍了.NET Core - 全球化和本地化 - 类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循关于如何实施全球化和使用 .NET Core 进行本地化,我的目标是将我的所有资源存储在位于不同项目(类库)中的单个全局资源文件中.

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).

项目 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 - 类库

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

如何在设置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");//外部项目~如何?

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

将您的资源文件移动到文件夹MYCLASSLIBRARY"下.只需确保在定义 ResourcesPath 时不要删除前导/".

Move your resource files under the folder "MYCLASSLIBRARY". Just make sure you don't remove the leading '/' when you define the ResourcesPath.

希望能帮到你.

这篇关于.NET Core - 全球化和本地化 - 类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆