gpt4 book ai didi

c# - .Net Core 3 IStringLocalizer.WithCulture(CultureInfo) 已过时

转载 作者:行者123 更新时间:2023-12-01 18:06:55 27 4
gpt4 key购买 nike

我已将项目从 .Net Core 2.2 升级到 .Net Core 3.0。

在尝试修复所有警告和错误后,我现在正在尝试找到此警告的解决方案:

'IStringLocalizer.WithCulture(CultureInfo)' is obsolete: 'This method is obsolete.
Use `CurrentCulture` and `CurrentUICulture` instead.'

我使用它来更改每个登录用户的网站语言。我有这个实现来改变每个用户的网站文化:

public class CultureLocalizer : ICultureLocalizer
{
private readonly IStringLocalizer localizer;
public CultureLocalizer(IStringLocalizerFactory factory)
{
var type = typeof(Resources.PageResources);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
localizer = factory.Create("PageResources", assemblyName.Name);
}

// if we have formatted string we can provide arguments
// e.g.: @Localizer.Text("Hello {0}", User.Name)
public LocalizedString Get(string key, params string[] arguments)
{
return arguments == null ? localizer[key] : localizer[key, arguments];
}

public LocalizedString Get(Enum key, params string[] arguments)
{
return arguments == null ? localizer[key.ToString()] : localizer[key.ToString(), arguments];
}

public LocalizedString Get(CultureInfo culture, string key, params string[] arguments)
{
// This is obsolete
return arguments == null ? localizer.WithCulture(culture)[key] : localizer.WithCulture(culture)[key, arguments];
}

public LocalizedString Get(CultureInfo culture, Enum key, params string[] arguments)
{
// This is obsolete
return arguments == null ? localizer.WithCulture(culture)[key.ToString()] : localizer.WithCulture(culture)[key.ToString(), arguments];
}
}

这是一个虚拟类,仅保存用于翻译的 .resx 文件:

// dummy class for grouping localization resources
public class PageResources
{
}

除了 discussion on github 之外,我在网络上找不到任何涉及如何解决此警告的内容。似乎还没有解决方案。

是否有其他人偶然发现此警告并找到解决方案?

最佳答案

源代码中已经提到了 here

    /// <summary>
/// Creates a new <see cref="IStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
/// <returns>A culture-specific <see cref="IStringLocalizer"/>.</returns>
[Obsolete("This method is obsolete. Use `CurrentCulture` and `CurrentUICulture` instead.")]
IStringLocalizer WithCulture(CultureInfo culture);

这是他们在 .Net Core 3.0 中的使用方式

public static void Main()  
{
// Display the name of the current thread culture.
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);

// Change the current culture to th-TH.
CultureInfo.CurrentCulture = new CultureInfo("th-TH", false);
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);

// Display the name of the current UI culture.
Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);

// Change the current UI culture to ja-JP.
CultureInfo.CurrentUICulture = new CultureInfo( "ja-JP", false );
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}

更新:在我们得到 Microsoft 的正式公告之前,此方法是可行的

您可以创建这样的服务

public class LocalizationService
{
private readonly IStringLocalizer _localizer;

public LocalizationService(IStringLocalizerFactory factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create("SharedResource", assemblyName.Name);
}

public LocalizedString GetLocalizedHtmlString(string key)
{
return _localizer[key];
}
}

然后在你的startup.cs中

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<LocalizationService>();
services.AddLocalization(options => options.ResourcesPath = "Resources");

services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("nl")
};

options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
return factory.Create("SharedResource", assemblyName.Name);
};
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

var localizationOption = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(localizationOption.Value);

app.UseStaticFiles();
app.UseCookiePolicy();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}

您可以查看我的完整代码here

关于c# - .Net Core 3 IStringLocalizer.WithCulture(CultureInfo) 已过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58316533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com