【问题标题】:How to create a SharedResource class for localization with default localization如何使用默认本地化为本地化创建 SharedResource 类
【发布时间】:2018-01-08 05:50:29
【问题描述】:

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"]) 只会返回 "wordToTranslate",即使在默认的 Resources/SharedResource.en.resx 对应于另一个字符串 ("wordToTranslate" = "Success")。我做错了什么?

【问题讨论】:

    标签: c# asp.net-core localization asp.net-core-localization


    【解决方案1】:

    在 ASP.NET Core 本地化中,默认语言是括号内的语言,因此:

    @Localizer["some text"]
    

    将呈现:

    • “一些文字”,因为当前语言是默认语言
    • “一些文本”,因为没有当前语言的资源文件
    • 翻译,假设有当前语言的资源文件

    或者,如果您想使用资源文件,只需从中删除文化,即someresource.resx

    【讨论】:

    • 我确实创建了 Resources/SharedResource.en.resx,只是碰巧当没有 Resources/SharedResource.it.resx 并且当前文化是“它”时它不会读取它;我的期望是,如果我没有某个国家的 ShareResource,默认情况下它总是会读取 Resources/SharedResource.en.resx,目前不会发生。
    • 为什么会这样?本地化在不能提供默认本地化的情况下不会失去意义?
    • 它有效:'D 你让我成为现在最轻松和快乐的开发者,非常感谢你 (◕ᴥ◕)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2019-10-02
    • 2017-01-07
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多