如何使用默认本地化创建 SharedResource class 进行本地化
How to create a SharedResource class for localization with default localization
在 ConfigureServices
我做了:
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
services.AddMvc(options => { options.MaxModelValidationErrors = 10; })
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
opts => opts.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
return factory.Create("SharedResource", assemblyName.Name);
};
});
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("it-IT"),
new CultureInfo("it")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new AcceptLanguageHeaderRequestCultureProvider());
});
//This is the custom service for localization
services.AddSingleton<LocService>();
在 LocService
中创建了本地化服务,例如:
public class LocService : StringLocalizer<string>
{
private readonly IStringLocalizer _localizer;
public LocService(IStringLocalizerFactory factory) : base(factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create("SharedResource", assemblyName.Name);
}
public override LocalizedString this[string name]
{
get => _localizer[name];
}
}
并配置 Configure()
中的所有内容,例如:
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
在视图中我将其注入为:
@inject LocService Loc
@Loc["wordToTranslate"]
我的问题是这种方法在缺少相关资源文件时不起作用,例如,
如果我没有 Resources/SharedResource.it.resx 并且调用 (@Loc["wordToTranslate"]
) 只是 returns "wordToTranslate"
即使在默认情况下 Resources/SharedResource。 en.resx 对应另一个字符串 ("wordToTranslate" = "Success"
)。我做错了什么?
在 ASP.NET 核心本地化中,默认语言是您放在括号内的语言,因此:
@Localizer["some text"]
将呈现:
- "some text",因为当前语言是默认语言
- "some text",因为没有当前语言的资源文件
- 翻译,假设有当前语言的资源文件
或者,如果您想使用资源文件,只需从中删除文化,即 someresource.resx
在 ConfigureServices
我做了:
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
services.AddMvc(options => { options.MaxModelValidationErrors = 10; })
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
opts => opts.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
return factory.Create("SharedResource", assemblyName.Name);
};
});
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("it-IT"),
new CultureInfo("it")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new AcceptLanguageHeaderRequestCultureProvider());
});
//This is the custom service for localization
services.AddSingleton<LocService>();
在 LocService
中创建了本地化服务,例如:
public class LocService : StringLocalizer<string>
{
private readonly IStringLocalizer _localizer;
public LocService(IStringLocalizerFactory factory) : base(factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create("SharedResource", assemblyName.Name);
}
public override LocalizedString this[string name]
{
get => _localizer[name];
}
}
并配置 Configure()
中的所有内容,例如:
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
在视图中我将其注入为:
@inject LocService Loc
@Loc["wordToTranslate"]
我的问题是这种方法在缺少相关资源文件时不起作用,例如,
如果我没有 Resources/SharedResource.it.resx 并且调用 (@Loc["wordToTranslate"]
) 只是 returns "wordToTranslate"
即使在默认情况下 Resources/SharedResource。 en.resx 对应另一个字符串 ("wordToTranslate" = "Success"
)。我做错了什么?
在 ASP.NET 核心本地化中,默认语言是您放在括号内的语言,因此:
@Localizer["some text"]
将呈现:
- "some text",因为当前语言是默认语言
- "some text",因为没有当前语言的资源文件
- 翻译,假设有当前语言的资源文件
或者,如果您想使用资源文件,只需从中删除文化,即 someresource.resx