Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core Blazor定位_Asp.net Core_Blazor_Blazor Server Side - Fatal编程技术网

Asp.net core Blazor定位

Asp.net core Blazor定位,asp.net-core,blazor,blazor-server-side,Asp.net Core,Blazor,Blazor Server Side,我试图在Blazor应用程序中实现本地化,但在试图通过视图中的键获取资源值时遇到了一些问题 我怎样才能做到这一点 以下是我目前的代码: Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddLocalization(options => { options.ResourcesPath = "Resources"; }); services.Configure&

我试图在Blazor应用程序中实现本地化,但在试图通过视图中的键获取资源值时遇到了一些问题

我怎样才能做到这一点

以下是我目前的代码:

Startup.cs

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

    services.Configure<RequestLocalizationOptions>(
        options =>
        {
            List<CultureInfo> supportedCultures =
                new List<CultureInfo>
                {
                    new CultureInfo("bg-BG"),
                    new CultureInfo("en-US")
                };

            options.DefaultRequestCulture = new RequestCulture("bg-BG");

            // Formatting numbers, dates, etc.
            options.SupportedCultures = supportedCultures;

            // UI string 
            options.SupportedUICultures = supportedCultures;

        });

    services.AddRazorPages();

    services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });

    services.AddApplicationRepositoryServices();

    services.AddSingleton<WeatherForecastService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     app.UseRequestLocalization( app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);

    if (env.IsDevelopment())
    {
       app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");

        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
   }

   app.UseHttpsRedirection();

    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseRouting();

    app.UseEndpoints(endpoints =>
   {            
       endpoints.MapRazorPages();

       endpoints.MapBlazorHub();

        endpoints.MapFallbackToPage("/_Host");
    });
}
public void配置服务(IServiceCollection服务)
{
AddLocalization(options=>{options.ResourcesPath=“Resources”;});
服务。配置(
选项=>
{
列出支持的文化=
新名单
{
新文化资讯(“bg”),
新文化信息(“en-US”)
};
options.DefaultRequestCulture=新的RequestCulture(“bg”);
//格式化数字、日期等。
options.SupportedCultures=SupportedCultures;
//用户界面字符串
options.supportedCultures=supportedCultures;
});
services.AddRazorPages();
services.AddServerSideBlazor().AddCircuitOptions(选项=>{options.DetailedErrors=true;});
AddApplicationRepositoryServices();
services.AddSingleton();
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
app.UseRequestLocalization(app.ApplicationServices.GetService().Value);
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseRouting();
app.UseEndpoints(端点=>
{            
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage(“/_主机”);
});
}

Login.razor

@page "/login"

@using Microsoft.Extensions.Localization

@inject IStringLocalizer<Login> Localizer

<button type="submit" class="btn btn-primary w-100">@Localizer["LoginButtonText"]</button>
@page”/login
@使用Microsoft.Extensions.Localization
@注入IStringLocalizer定位器
@定位器[“登录按钮文本”]

页面位置

计划

--页数

----帐目

------登录剃须刀


资源文件位置

计划

--资源

----页数

------帐目

--------Login.bg bg.resx

这对我有用:

Startup.cs

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

    services.Configure<RequestLocalizationOptions>(
        options =>
        {
            List<CultureInfo> supportedCultures =
                new List<CultureInfo>
                {
                    new CultureInfo("bg-BG"),
                    new CultureInfo("en-US")
                };

            options.DefaultRequestCulture = new RequestCulture("bg-BG");

            // Formatting numbers, dates, etc.
            options.SupportedCultures = supportedCultures;

            // UI string 
            options.SupportedUICultures = supportedCultures;

        });

    services.AddRazorPages();

    services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });

    services.AddApplicationRepositoryServices();

    services.AddSingleton<WeatherForecastService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     app.UseRequestLocalization( app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);

    if (env.IsDevelopment())
    {
       app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");

        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
   }

   app.UseHttpsRedirection();

    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseRouting();

    app.UseEndpoints(endpoints =>
   {            
       endpoints.MapRazorPages();

       endpoints.MapBlazorHub();

        endpoints.MapFallbackToPage("/_Host");
    });
}
public void配置服务(IServiceCollection服务)
{
...
AddLocalization(options=>{options.ResourcesPath=“Resources”;});
...
}
Index.razor

@page "/login"

@using Microsoft.Extensions.Localization

@inject IStringLocalizer<Login> Localizer

<button type="submit" class="btn btn-primary w-100">@Localizer["LoginButtonText"]</button>
@page/“
@使用系统。全球化
本地化测试
本地化字段:@(定位器[“字段1]”)
@代码{
[Inject]公共Microsoft.Extensions.Localization.IStringLocalizer本地化程序{get;set;}
}
我的
resx
文件存储如下:

Resources\Pages\Index.resx
Resources\Pages\Index.cs.resx
...

然后,根据UICulture,调用
本地化程序[“Field1”]
时将返回正确的本地化字符串

在代码中,我通过以下方式更改UICulture:


CultureInfo.DefaultThreadCurrentUICulture=new CultureInfo(“cs CZ”);

看看这个库-。discalimer-不知道这是否有什么好处,但在中提到:本文档描述了如何本地化Blazor应用程序。知道如何将
IStringLocalizer
与提供本地化的卫星程序集一起使用吗?您还可以将
@inject Microsoft.Extensions.localization.IStringLocalizer
添加到剃须刀头。对我来说,重要的是将其存储在正确的资源文件夹中。我忘记了页面的子文件夹.CultureInfo.DefaultThreadCurrentUICulture=new CultureInfo(“cs CZ”);这个是为我做的。非常感谢。