简体   繁体   中英

ASP.NET Core get Resource string from explicit culture

On my ASP.NET Core 3.1 website I'm trying to generate alternate links for different supported languages. These links are stored in resources. I'm using IStringLocalizer<> as a dependency in my class to get the translations. There used to be an extension method .WithCulture() that allowed string localizer to lookup in different culture. However this is marked obsolete (and removed in .NET 5).

What is the best approach to load single key from multiple available cultures? I'm currently "hacking" it using this approach

var originalCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureCode);
var localized = localizer[myKey];
Thread.CurrentThread.CurrentUICulture = originalCulture;

for each supported language. However this simply doesn't seem right to me.

Is there a better approach?

Also I think there is some kind of race condition as I encountered some texts being randomly rendered in incorrect culture - because I¨M changing some global state..

为什么不将它存储在 appSettings.json 或其他 json 文件中?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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