Skip to content

Instantly share code, notes, and snippets.

@dougbu
Created October 19, 2016 19:24
Show Gist options
  • Save dougbu/bc4880f0aadce9d94b7df91d53782da0 to your computer and use it in GitHub Desktop.
Save dougbu/bc4880f0aadce9d94b7df91d53782da0 to your computer and use it in GitHub Desktop.
Alternate Startup pattern for option settings. Especially useful when the settings rely on services.
using System.Globalization;
using System.Reflection;
using Localized._1._1_preview.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
namespace Wrap.Localized
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Embedded provider for views in the class library.
services
.AddMvc()
.AddDataAnnotationsLocalization()
.AddViewLocalization();
// Might execute markers e.g. services.Configure<MvcOptions>() as well.
}
public void ConfigureOptions(LocalizationOptions options)
{
options.ResourcesPath = "Resources";
}
public void ConfigureOptions(MvcOptions options, IStringLocalizer<Model> localizer)
{
options.ModelBindingMessageProvider.ValueMustNotBeNullAccessor =
value => localizer["Value '{0}' appears to be null and that's not valid.", value];
}
public void ConfigureOptions(RazorViewEngineOptions options)
{
var embeddedProvider = new EmbeddedFileProvider(typeof(Model).GetTypeInfo().Assembly);
options.FileProviders.Add(embeddedProvider);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
var options = new RequestLocalizationOptions();
foreach (var culture in new[]
{
new CultureInfo("de") ,
new CultureInfo("de-DE"),
new CultureInfo("fr"),
new CultureInfo("fr-CA"),
})
{
options.SupportedCultures.Add(culture);
options.SupportedUICultures.Add(culture);
}
app.UseRequestLocalization(options);
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment