Ich versuche, Lokalisierung für meine MVC-Ansichten in ASP.NET Core 1.0-Webanwendung zu erhalten.ASP.NET Core 1.0 Benennung von Anwendungsressourcen
Bisher habe ich meine Startup.cs
Datei mit ConfigureServices
Methode ein:
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(configureOptions => configureOptions.LowercaseUrls = true);
services.AddMvc()
.AddViewLocalization(setupAction => setupAction.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
}
Dann ist mein Configure
Methode:
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole();
loggerFactory.AddDebug();
CultureInfo defaultCulture = new CultureInfo("en");
List<CultureInfo> supportedCultures = new List<CultureInfo>
{
defaultCulture,
new CultureInfo("fr")
};
RequestLocalizationOptions requestLocalizationOptions = new RequestLocalizationOptions
{
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
};
RequestCulture defaultRequestCulture = new RequestCulture(defaultCulture);
//Insert this at the beginning of the list since providers are evaluated in order until one returns a not null result
requestLocalizationOptions.RequestCultureProviders.Insert(0, new UrlCultureProvider());
app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture);
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultWithCulture",
template: "{culture}/{controller}/{action}/{id?}",
defaults: new { culture = "en", controller = "Home", action = "Index" },
constraints: new { culture = "en|fr" });
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { culture = "en", controller = "Home", action = "Index" });
});
app.UseStatusCodePages();
}
Und schließlich meine UrlCultureProvider
Klasse, die mir die Kultur zu setzen hilft, bei der Es ist in der URL zur Verfügung gestellt:
public class UrlCultureProvider : IRequestCultureProvider
{
public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
var url = httpContext.Request.Path;
//Quick and dirty parsing of language from url path, which looks like "/api/de-DE/home"
//This wont be needed in RC2 since an extension method GetRouteData has been added to HttpContext
//which means we could just get the the "language" parameter from the route data
var parts = httpContext.Request.Path.Value.Split('/');
if (parts.Length < 2)
{
return Task.FromResult<ProviderCultureResult>(null);
}
var hasCulture = Regex.IsMatch(parts[1], @"^[a-z]{2}$");
if (!hasCulture)
{
return Task.FromResult<ProviderCultureResult>(null);
}
var culture = parts[1];
return Task.FromResult(new ProviderCultureResult(culture));
}
}
Die Kultur wird auf fr oder en gesetzt, wenn sie in der URL angezeigt wird. als ich auf meinem 'Ressourcen' Ordner zwei Ressourcen-Dateien haben:
Views.Home.Index.cshtml.resx
Views.Home.Index.cshtml.fr.resx
Und in meiner _ViewImports.cshtml
Datei, ich habe diesen Code:
@using Microsoft.AspNet.Mvc.Localization
@inject IViewLocalizer Localization
Schließlich, ich habe in meiner Indexansicht ein Ressourcenaufruf:
@Localization["Test"]
Ich habe diese Arbeit mit "en" Kultur, aber nicht "fr" Kultur.
Wissen Sie, was dort vor sich geht?
Vielen Dank.