本地化 ASP.NET Core 8 MVC 中的共享视图

问题描述 投票:0回答:1

我正在尝试本地化我的 ASP.NET Core 8 MVC 应用程序。我已遵循所有步骤,但无法本地化我的共享视图。

这是我的

Program.cs

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

var builder = WebApplication.CreateBuilder(args);

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

string[] supportedLanguages = ["en", "jp"];
IList<CultureInfo> cultures = new List<CultureInfo>();
foreach(string lang in supportedLanguages)
{
    cultures.Add(new CultureInfo(lang));
}

builder.Services.Configure<RequestLocalizationOptions>(opt =>{
    opt.DefaultRequestCulture = new RequestCulture(supportedLanguages[0]);
    opt.SupportedCultures = cultures;
});

var app = builder.Build();
app.UseRequestLocalization();

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

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

app.Run();

Views/ViewImports.cshtml

@using newApp
@using newApp.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

我的资源文件位于

Localization
文件夹中,结构如下:

Localization > Views > Shared > _Layout.en.resx
Localization > Views > Shared > _Layout.jp.resx

我正在使用以下代码来调用本地化

@Localizer["search"]
- 这是我在
HomeController
中的操作:

public IActionResult Lang(string? culture)
{
    if (culture != null)
    {
        Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName, 
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), 
            new CookieOptions{ Expires = DateTime.Now.AddYears(1) }
        );
    }

    string referer = Request.Headers["Referer"].ToString();

    if (string.IsNullOrEmpty(referer))
        referer = "/";

    return Redirect(referer);
}

我仍然看不到我的应用程序更改语言。我到底做错了什么?

c# asp.net-core asp.net-core-mvc asp.net-core-8
1个回答
0
投票

首先,文化

jp
无法识别,
ja
是正确的。

第二,需要

SupportedUICultures

程序.cs

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

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddLocalization(opt => opt.ResourcesPath = "Localization");
builder.Services.AddControllersWithViews()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();

string[] supportedLanguages = ["en", "ja"];    //1.ja represents japanese
IList<CultureInfo> cultures = new List<CultureInfo>();
foreach (string lang in supportedLanguages)
{
    cultures.Add(new CultureInfo(lang));
}

builder.Services.Configure<RequestLocalizationOptions>(opt => {
    opt.DefaultRequestCulture = new RequestCulture(supportedLanguages[0]);
    opt.SupportedCultures = cultures;
    opt.SupportedUICultures = cultures;    //2.supportedui needed
});

var app = builder.Build();
app.UseRequestLocalization();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

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

app.Run();

© www.soinside.com 2019 - 2024. All rights reserved.