2

私は、セットアップのローカライズには、このページの指示に従ってきた。ローカライズ

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

しかし、私にとってはそれだけで動作するようには思えません。

Startup.cs:

public class Startup 
{ 
    public IConfigurationRoot Configuration { get; } 

    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", true, true); 

     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 
    } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddLocalization(o => o.ResourcesPath = "Resources"); 
     services.AddMvc(o => 
     { 
      o.Filters.Add(typeof(GlobalExceptionFilter)); 
     } 
     ).AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization(); 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     var supportedCultures = new[] 
     { 
      new CultureInfo("es-MX"), 
      new CultureInfo("en-US") 
     }; 

     app.UseRequestLocalization(new RequestLocalizationOptions 
     { 
      DefaultRequestCulture = new RequestCulture("es-MX"), 
      SupportedCultures = supportedCultures, 
      SupportedUICultures = supportedCultures 
     }); 

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

     app.UseStaticFiles(); 
     app.UseAuthentication(); 

     app.UseMvcWithDefaultRoute(); 
     //app.UseMvc(routes => 
     //{ 
     // routes.MapRoute(
     //  "default", "{controller=Home}/{action=Index}/{id?}"); 
     //}); 

     app.UseRewriter(new RewriteOptions().AddRedirectToHttps()); 
    } 
} 

マイLayout.cshtmlには以下が含まれています。

@await Html.PartialAsync("_SelectLanguagePartial") 

と私の_SelectLanguagePartial.cshtmlが含まれています

@using Microsoft.AspNetCore.Builder 
@using Microsoft.AspNetCore.Localization 
@using Microsoft.AspNetCore.Mvc.Localization 
@using Microsoft.Extensions.Options 

@inject IViewLocalizer Localizer 
@inject IOptions<RequestLocalizationOptions> LocOptions 

@{ 
    var requestCulture = Context.Features.Get<IRequestCultureFeature>(); 
    var cultureItems = LocOptions.Value.SupportedUICultures 
     .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName }) 
     .ToList(); 
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}"; 
} 

<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name"> 
    <form id="selectLanguage" asp-controller="Home" asp-action="SetLanguage" asp-route-returnUrl="@returnUrl" 
      method="post" role="form"> 
     <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label> <select name="culture" 
      onchange="this.form.submit();" 
      asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"> 
     </select> 
    </form> 
</div> 
をここに私がやっていることです

これに加えて、私は2つのリソースファイルErrorMessages.ex-MX.resxとを作成しました。私にHomeControllerで

私は、この行ViewBag.Test = ErrorMessages.Test;を追加したと私は、言語をドロップダウンを参照してくださいページを見て、それは英語のみがリストされている場合Indexビューに私は<p>@ViewBag.Test</p>

を追加しました。このページに表示されたテキストは、ErrorMessages.resxからのものです。

ステップがありませんか?どのようにスペイン語はどこに拾われていない来る?あなたが下に見るように、私は主な文化としてスペイン語を設定しようと試みましたが、違いはありません。

EDIT:

私はリストと私はドロップダウンはまだそこにいた、それはスペイン語を列挙されていたこの時間だ結果からeglishを削除しようとしました。しかし、テキストはまだ英語であった。

次に、項目に2番目のリストとして英語を追加して、メインの言語として英語を設定しようとしましたが、ドロップダウンは1つの言語しか表示されず、スペイン語は現在なくなっていました。

答えて

1

私は1週間ほど前にこの同じ問題を抱えていました。私はこれでこれを解決した:

もう1つのコメント。 ASP.NETコアでは、既定のカルチャのリソースは、唯一の特定のものを作成すべきではない:だから

を、

Views/Shared/Layout.cshtml 

与えられたあなただけであること、また、通知を行います

Resources/Views/Shared/Layout.es-MX.resx 

を作成これによりスペイン語をスペイン語に限定するので、esフォールバックか、eses-MXの両方を追加する必要があります。

+0

違いはありますか? 'App.UseRequestLocalization(locOptions.Value);' – Bojan

+0

いいえ、 'ConfigureServices'で設定し、' Configure'で使用しています。 –

+0

ああ、ありがとう。私はそれを朝にしようとし、あなたに知らせるでしょう。 – Bojan