question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Localized views with Razor Pages

See original GitHub issue

Is 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

@DamianEdwards @rynowak @pranavkm

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
TheObliteratorcommented, Dec 13, 2022

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:

if (pageCulture.Name == currentCulture.Name)
    exactMatchIndex = i;
else if (pageCulture.Name == currentCulture.Parent.Name)
    parentMatchIndex = i;
else if (pageCulture == CultureInfo.InvariantCulture)
    invariantMatchIndex = i;

And then picked the best candidate based upon that:

if (exactMatchIndex >= 0)
    candidates.SetValidity(exactMatchIndex, true);
else if (parentMatchIndex >= 0)
    candidates.SetValidity(parentMatchIndex, true);
else if (invariantMatchIndex >= 0)
    candidates.SetValidity(invariantMatchIndex, true);

Hope its helpful. Huge thanks to @DamianEdwards - I wouldn’t have known where to start without the example.

1reaction
DamianEdwardscommented, Jun 15, 2022

@msschl no, but I’ll try and take a look soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

View Localization with Razor Pages
We can localize Razor Pages by injecting the IViewLocalizer . This is very helpful, but for Razor Pages with a lot of content...
Read more >
Localisation in ASP.NET Core Razor Pages - Cultures
Localisation is the process of adapting the site content for different countries, regions or cultures. The starting point to globalisation of a ...
Read more >
Globalization and localization in ASP.NET Core
Learn how ASP.NET Core provides services and middleware for localizing content into different languages and cultures.
Read more >
ASP.NET Core Localization and Internationalization
Razor Pages do not currently support localized page files, so using the localizer is the only option. Web API does not even use...
Read more >
Localizing ASP.NET Applications - .NET Tools Guide
The Razor view utilizes the DisplayAttribute to determine a <label> tag's value. ASP.NET uses the RequiredAttribute for validation, and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found