title: Create multilingual translated routes
body: <p>I would like to make a route system in ASP.NET Core in which urls are translated as same as this question <a href="https://stackoverflow.com/questions/25082154/how-to-create-multilingual-translated-routes-in-laravel">How to create multilingual translated routes in Laravel</a></p>
  
  <p>The goal is to translate the part of the url path which is after the language code, and generate urls in the user language.</p>
  
  <p>For example:</p>
  
  <pre><code>/fr/produit
  </code></pre>
  
  <p>Will become in english:</p>
  
  <pre><code>/en/product
  </code></pre>
  
  <p>I have the following configuration:</p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      public void ConfigureServices(IServiceCollection services)
      {   
          services.Configure&lt;RequestLocalizationOptions&gt;(options =&gt;
          {
              var translations = services.BuildServiceProvider().GetService&lt;TranslationsDbContext&gt;();
              var supportedCultures = translations.Cultures
                  .ToList()
                  .Select(x =&gt; new CultureInfo(x.Name))
                  .ToArray();
  
              options.DefaultRequestCulture = new RequestCulture(culture: "fr", uiCulture: "fr");
              options.SupportedCultures = supportedCultures;
              options.SupportedUICultures = supportedCultures;
  
              options.RequestCultureProviders = new RequestCultureProvider[]
              {
                  new RouteDataRequestCultureProvider()
                  {
                      RouteDataStringKey = "lang",
                      UIRouteDataStringKey = "lang",
                      Options = options
                  },
                  new CookieRequestCultureProvider(),
                  new AcceptLanguageHeaderRequestCultureProvider()
              }; 
          });
  
          services.Configure&lt;RouteOptions&gt;(options =&gt;
          {
              options.ConstraintMap.Add("lang", typeof(LanguageRouteConstraint));
          }); 
  
          services.AddDbContextPool&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseSqlServer(
                  Configuration.GetConnectionString("DefaultConnection")));
  
          services.AddDbContext&lt;TranslationsDbContext&gt;(options =&gt;
              options.UseSqlServer(Configuration.GetConnectionString("TranslationsConnection")));
  
          services.AddIdentity&lt;User, IdentityRole&lt;Guid&gt;&gt;()
              .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  
          services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
          services.AddSingleton&lt;IStringLocalizerFactory, EFStringLocalizerFactory&gt;();
          services.AddScoped&lt;IStringLocalizer, StringLocalizer&gt;();
  
          services
              .AddMvc()
              .AddDataAnnotationsLocalization();
  
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env,
          ApplicationDbContext dbContext, TranslationsDbContext translationsDbContext,
          UserManager&lt;User&gt; userManager, RoleManager&lt;IdentityRole&lt;Guid&gt;&gt; roleManager)
      { 
          app.UseRequestLocalization();
  
          app.UseWhen(
              context =&gt; !context.Request.Path.StartsWithSegments("/api"),
              a =&gt; a.UseLocalizedStatusCodePagesWithReExecute("/{0}/error/{1}")
          );
  
          app.UseResponseCompression();
  
          app.UseCookiePolicy();
  
          app.UseAuthentication();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "localized",
                  template: "{lang:lang}/{controller=Home}/{action=Index}/{id?}"
              );
              routes.MapRoute(
                  name: "catchAll",
                  template: "{*catchall}",
                  defaults: new { controller = "Home", action = "RedirectToDefaultLanguage" }
              );
          });
      }
  }
  
  public class LanguageRouteConstraint : IRouteConstraint
  {
      public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
      {
          if (!values.ContainsKey("lang"))
          {
              return false;
          }
  
          var lang = values["lang"].ToString();
  
          return lang == "fr" || lang == "en" || lang == "de";
      }
  }
  </code></pre>
  
  <p>The language code is correctly handled by the application.</p>
  
  <p>However, after many research I see that I can use <code>IApplicationModelConvention</code> to customize how routing works. But I don't understand how I can use this interface  to work with my existing configuration.</p>