资源文件中的本地化字符串

时间:2018-04-02 10:11:48

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

我想在我的视图中添加本地化字符串。

资源/ Views.Account.Login.en-US.resx

enter image description here

查看/帐户/ Login.cshtml

public void printVertices(PrintWriter os) {
    for(int i = 0; i < vert.size(); i++) {
        os.print(vert.get(i) + " ");
    }
    os.flush();
}

Startup.cs

    // DataTable could instead be a collection whose type properties match the report

    ReportDocument rd;
   //  Make the name of the datatable match what the report expects
    dt.TableName = "DataTable1";
    rd = new ReportDocument();
    rd.Load("blaa.rpt");
    rd.SetDataSource(dt);

    rd.Refresh();
    CrystalReportViewer1.ReportSource = rd;
    CrystalReportViewer1.RefreshReport();
    CrystalReportViewer1.DisplayGroupTree = false;

生成的HTML

@using Microsoft.AspNetCore.Mvc.Localization    
@inject IViewLocalizer Localizer

<div>
    @System.Threading.Thread.CurrentThread.CurrentCulture.Name                   
    @System.Threading.Thread.CurrentThread.CurrentUICulture.Name
    @Localizer["Key"]
</div>

预期的HTML

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(options => options.ResourcesPath = "Resources");

    services.AddMvc()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{

    var options = new RequestLocalizationOptions
    {
        SupportedCultures = new List<CultureInfo>
        {
            new CultureInfo("en-US")
        },
        SupportedUICultures = new List<CultureInfo>
        {
            new CultureInfo("en-US")
        },
        DefaultRequestCulture = new RequestCulture("en-US")
    };

    app.UseRequestLocalization(options);
}

我忘记了一步吗?

修改

如果名为en-US en-US Key 的资源正常工作。为什么?

4 个答案:

答案 0 :(得分:0)

您需要注入IStringLocalizer

在_ViewImports.cshtml中使用@inject IStringLocalizer<Your resources class name> _localizer;

答案 1 :(得分:0)

我在ConfigureServices

中有这段代码
var georgianCultureInfo = new CultureInfo("ka-GE");
var englishCultureInfo = new CultureInfo("en-US");
var russianCultureInfo = new CultureInfo("ru-RU");

var supportedCultures = new List<CultureInfo>
{
    georgianCultureInfo,
    englishCultureInfo,
    russianCultureInfo
};

services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new RequestCulture(georgianCultureInfo);
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

这在Configure

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

答案 2 :(得分:0)

您可以在Viewbag中存储键值对,并在jquery&amp;中使用$ .parse.JSON循环通过它&amp;获得所需的值

答案 3 :(得分:0)

<强>问题:

如果资源名为Resources / Views.Account.Login.resx正常工作。为什么?

<强>答案:

最后一部分en-US用于本地化,如果省略,则该资源文件将被选为默认值。 localizer正在检查CurrentCulture,然后尝试查找名称中包含相同结尾部分的资源文件。如果找不到文件,它将采用默认资源文件。

还尝试从services.AddLocalization(options => options.ResourcesPath = "Resources");移除参数并离开services.AddLocalization();