アプリケーションをローカライズする際に、次の手順に従いました。https://docs.asp.net/en/latest/fundamentals/localization.html
これが私のコードです:
スタートアップ.cs
public List<IRequestCultureProvider> RequestCultureProviders { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.AddOptions();
services.AddTransient<IViewRenderingService, ViewRenderingService>();
services.AddTransient<IEmailSender, EmailSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory())),
EnableDirectoryBrowsing = true
});
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider
{
QueryStringKey = "culture",
UIQueryStringKey = "ui-culture"
}
}
});
}
マイコントローラ.cs
public class MyController : Controller
{
private readonly IViewRenderingService _viewRenderingService;
private IStringLocalizer<MyController> _localizer;
private MyOptions _options;
//Constructor for dependency injection principle
public MyController(IViewRenderingService viewRenderingService, IStringLocalizer<MyController> localizer, IOptions<MyOptions> options)
{
_viewRenderingService = viewRenderingService;
_localizer = localizer;
_options = options.Value;
}
[HttpGet]
public string Get()
{
// _localizer["Name"]
return _localizer["Product"];
}
}
ファイルは、名前(「製品」のエントリがある)でフォルダー*.resx
に保存されます。Resources
Controllers.MyController.fr.resx
ただし、リソース ファイルが見つからないため、フランス語で「Product」が返されることはありません。クエリ文字列を使用しているので、クエリ文字列は次のとおりです。
localhost:3333/my?culture=fr
また、ビューでは、@Localizer["Product"]
「Product」を返します。
何が足りないのか探すのを手伝ってくれる人はいませんか?
編集:
調査の結果、カルチャが変更されていることがわかりましたが、リソース ファイルを見つけることができません。VS2015 を使用しています。どなたか助けていただけませんか?
ベストアンサー1
私も同じような問題を抱えていました。そして、私は「ローカリゼーション.AspNetCore.タグヘルパー「 nuget パッケージがプロジェクトにありませんでした。QueryStringRequestCultureProvider に必要なパッケージのようです。