繁体   English   中英

.NET 核心本地化与 Class 库

[英].NET Core Localization with Class Library

当资源文件夹位于 webUI 上时它工作正常。我如何将我的视图的资源文件存储在 class 库中。 感谢帮助

[网页界面 ] [类库 ]

程序.cs

builder.Services.AddLocalization(options =>
{
    options.ResourcesPath = "Resources";
});

builder.Services.AddControllersWithViews()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, option =>
    {
        option.ResourcesPath = "Resources";
    })
    .AddDataAnnotationsLocalization()
     .AddRazorRuntimeCompilation();

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new(builder.Configuration["DefaultLanguage"]);

    CultureInfo[] cultures = new CultureInfo[]
    {
        new("en-US"),
        new("tr-TR"),
        new("fr-FR")
    };
        options.RequestCultureProviders = new List<IRequestCultureProvider>
        {
            new CookieRequestCultureProvider(),
        };
    options.SupportedCultures = cultures;
    options.SupportedUICultures = cultures;
});

这是我的工作演示,你可以参考:

资源库:

在此处输入图像描述

SharedResource.cs,放在项目根目录下,不需要包含任何数据,只需要class声明即可。

MVC 项目,Program.cs

using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddControllersWithViews().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization(); ;

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
var supportedCultures = new[]
         {
                new CultureInfo("en-US"),
                new CultureInfo("de"),//you can add more language as you want...
            };

app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("en-US"),
    // Formatting numbers, dates, etc.
    SupportedCultures = supportedCultures,
    // UI strings that we have localized.
    SupportedUICultures = supportedCultures
});
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Controller汉化,先添加对ResourceLibrary的引用。

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
        public HomeController(ILogger<HomeController> logger, IStringLocalizer<SharedResource> sharedLocalizer)
        {
            _logger = logger;
            _sharedLocalizer = sharedLocalizer;
        }

        public IActionResult Index()
        {
            var value = _sharedLocalizer["Privacy"];
            return View();
        }
    }

索引本地化

@using Microsoft.Extensions.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using ResourceLibrary

@inject IStringLocalizer<SharedResource> Localizer
@inject IHtmlLocalizer<SharedResource> HtmlLocalizer

@{
    ViewData["Title"] = @Localizer["Home"];
}

<div class="text-center">   
    <p>
        @HtmlLocalizer["Privacy Policy"]
    </p>
</div>

结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM