Localized views with Razor Pages
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
Bringing #4873 Localized views with Razor Pages
back to discussion for .net 7.0
In Razor Pages, we can inject IViewLocalizer to work with localized resources - good. But sometimes, especially on content-heavy, rather static pages, it would be nice to have location specific views, i.e. MyPage.en.cshtml and MyPage.de.cshtml etc.
My expection was that once I configure AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, …) Razor Pages also would be resolved by their base name, but I quickly realized there might be a problem with how the corresponding PageModel should be used/resolved. I couldn’t get this to work at all - is this scenario even supported with Razor Pages?
In any case, I think the corresponding docs should be more explicit about what’s possible with Razor Pages and potential limitations.
Describe the solution you’d like
View location expanders should apply to Razor pages too.
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:14 (12 by maintainers)
Top GitHub Comments
No worries, I figured out the problems so will post here for anyone else.
In PageLocalizationConvention it was incorrectly determining the newPageValue.
var newPageValue = page.Substring(0, dotIndex);
I also need to include the Area in the newRouteTemplate:
var newRouteTemplate = StringExtensions.JoinNonEmpty("/", model.AreaName, newPageValue.TrimStart('/'));
and build the new PageRouteMetadata based upon the newRouteTemplate:
selector.EndpointMetadata.Add(new PageRouteMetadata(newRouteTemplate, page));
I updated the policy to determine the pageCulture from the endpoint instead of the metadata:
var pageCulture = GetCulture(candidate.Endpoint.ToString() ?? string.Empty);
I added detection for parent cultures:
And then picked the best candidate based upon that:
Hope its helpful. Huge thanks to @DamianEdwards - I wouldn’t have known where to start without the example.
@msschl no, but I’ll try and take a look soon.