修改国际化配置提前

IRC_NewDev
hang 2024-10-17 10:35:56 +08:00
parent 8a3eb61a34
commit 9e057ff815
3 changed files with 30 additions and 7 deletions

View File

@ -207,7 +207,7 @@ app.UseExceptionHandler(o => { });
app.UseIRacisHostStaticFileStore(env); app.UseIRacisHostStaticFileStore(env);
//本地化 //本地化
app.UseLocalization(); app.UseLocalization(app.Services);
app.UseForwardedHeaders(); app.UseForwardedHeaders();
@ -249,9 +249,7 @@ app.MapMasaMinimalAPIs();
SerilogExtension.AddSerilogSetup(enviromentName, app.Services); SerilogExtension.AddSerilogSetup(enviromentName, app.Services);
//设置国际化I18n
var localizer = app.Services.GetRequiredService<IStringLocalizer>();
I18n.SetLocalizer(localizer);
var hangfireJobService = app.Services.GetRequiredService<IIRaCISHangfireJob>(); var hangfireJobService = app.Services.GetRequiredService<IIRaCISHangfireJob>();

View File

@ -1,5 +1,9 @@
using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -8,7 +12,7 @@ namespace IRaCIS.Core.API
public static class LocalizationConfig public static class LocalizationConfig
{ {
public static void UseLocalization(this IApplicationBuilder app) public static void UseLocalization(this IApplicationBuilder app,IServiceProvider serviceProvider)
{ {
var supportedCultures = new List<CultureInfo> var supportedCultures = new List<CultureInfo>
{ {
@ -30,6 +34,10 @@ namespace IRaCIS.Core.API
//options.RequestCultureProviders.RemoveAt(1); //options.RequestCultureProviders.RemoveAt(1);
app.UseRequestLocalization(options); app.UseRequestLocalization(options);
//设置国际化I18n
var localizer = serviceProvider.GetRequiredService<IStringLocalizer>();
I18n.SetLocalizer(localizer);
} }
} }
} }

View File

@ -20,12 +20,29 @@ public static class I18n
public static string T(string key) public static string T(string key)
{ {
return _localizer[key]; if(_localizer is null)
{
return key;
}
else
{
return _localizer[key];
}
} }
public static string T(string key, params object[] arguments) public static string T(string key, params object[] arguments)
{ {
return _localizer[key, arguments]; if (_localizer is null)
{
return key;
}
else
{
return _localizer[key, arguments];
}
} }
} }