我已经将项目从.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
{
}
除了在github上的讨论似乎还没有解决办法之外,我在网上找不到任何有关如何解决此警告的信息。
还有其他人偶然发现此警告并找到解决方案吗?