This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a sample demonstrating culture based selection for Razor Pages
- Loading branch information
Showing
9 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
samples/Mvc.LocalizedRazorPages/Mvc.LocalizedRazorPages.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using Microsoft.AspNetCore.Localization; | ||
using Microsoft.AspNetCore.Mvc.ActionConstraints; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
|
||
namespace LocalizedRazorPages | ||
{ | ||
public class PageCultureConvention : IPageRouteModelConvention | ||
{ | ||
public void Apply(PageRouteModel model) | ||
{ | ||
var path = model.ViewEnginePath; | ||
var lastSeparator = path.LastIndexOf('/'); | ||
Debug.Assert(lastSeparator != -1); | ||
var lastDot = path.LastIndexOf('.', path.Length - 1, path.Length - lastSeparator); | ||
|
||
// /SomeFolder/MyPage.fr-FR -> fr-FR | ||
if (lastDot != -1) | ||
{ | ||
var cultureName = path.Substring(lastDot + 1); | ||
var constraint = new CultureConstraint(new CultureInfo(cultureName)); | ||
for (var i = model.Selectors.Count - 1; i >= 0; i--) | ||
{ | ||
var selector = model.Selectors[i]; | ||
|
||
selector.ActionConstraints.Add(constraint); | ||
var template = selector.AttributeRouteModel.Template; | ||
template = template.Substring(0, lastDot - 1); | ||
|
||
selector.AttributeRouteModel.Template = template; | ||
var fileName = template.Substring(lastSeparator); | ||
|
||
if (fileName == "Index") | ||
{ | ||
selector.AttributeRouteModel.SuppressLinkGeneration = true; | ||
|
||
var indexSelector = new SelectorModel(selector); | ||
|
||
template = selector.AttributeRouteModel.Template.Substring(0, lastSeparator); | ||
indexSelector.AttributeRouteModel.Template = template; | ||
model.Selectors.Add(indexSelector); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class CultureConstraint : IActionConstraint | ||
{ | ||
private readonly CultureInfo _culture; | ||
|
||
public CultureConstraint(CultureInfo culture) | ||
{ | ||
_culture = culture; | ||
} | ||
|
||
public int Order => 0; | ||
|
||
public bool Accept(ActionConstraintContext context) | ||
{ | ||
var feature = context.RouteContext.HttpContext.Features.Get<IRequestCultureFeature>(); | ||
if (feature == null) | ||
{ | ||
// The page is associated with a culture but the request is not. | ||
return false; | ||
} | ||
|
||
return string.Equals(feature.RequestCulture.Culture.Name, _culture.Name, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@page | ||
@model IndexModel | ||
|
||
Neutral culture |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace LocalizedRazorPages.Pages | ||
{ | ||
public class IndexModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@page | ||
@model IndexModel | ||
|
||
French culture |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@using LocalizedRazorPages | ||
@namespace LocalizedRazorPages.Pages | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace LocalizedRazorPages | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateWebHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.HttpsPolicy; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace LocalizedRazorPages | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1) | ||
.AddRazorPagesOptions(options => | ||
{ | ||
options.Conventions.Add(new PageCultureConvention()); | ||
}); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Error"); | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseRequestLocalization(options => | ||
{ | ||
var supportedCultures = new[] { "en-US", "fr-FR" }; | ||
options.AddSupportedCultures(supportedCultures); | ||
options.AddSupportedUICultures(supportedCultures); | ||
}); | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
app.UseCookiePolicy(); | ||
|
||
app.UseMvc(); | ||
} | ||
} | ||
} |
7a53120
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pranavkm how this different from using
RouteRequestCultureProvider
?7a53120
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's attempting to select a Razor page path based on the request culture. Not entirely sure how it's similar to the culture provider
7a53120
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing
RouteRequestCultureProvider
attempting to select a proper view based on the culture provided by route