Skip to content

Add custom hostname-based request culture provider#24

Open
ivanbuzyka wants to merge 3 commits intoSitecore:mainfrom
ivanbuzyka:ivanbuzyka/custom-hostname-culture-provider
Open

Add custom hostname-based request culture provider#24
ivanbuzyka wants to merge 3 commits intoSitecore:mainfrom
ivanbuzyka:ivanbuzyka/custom-hostname-culture-provider

Conversation

@ivanbuzyka
Copy link
Copy Markdown

Introduce HostnameRequestCultureProvider to determine culture based on the request hostname, supporting automatic culture selection. Update Program.cs with comments and examples for configuring multiple languages in request localization.

This is an example that highlights the case when head app serving multi-site should set default localization per site hostname. In the same time app should be able to resolve locale from URL prefix or query string parameter.

Example: website testsite.nl should set locale to nl-NL by default, however if visitor switch the language to /en (testsite.nl/en/) - language should be switched to EN.

Program.cs contains only commented put code, therefore default behavior of the started app shouldn't be changed.

Introduce `HostnameRequestCultureProvider` to determine culture
based on the request hostname, supporting automatic culture
selection. Update `Program.cs` with comments and examples
for configuring multiple languages in request localization.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a hostname-based culture provider and illustrates how to plug it into ASP.NET Core request localization without changing default behavior.

  • Introduces HostnameRequestCultureProvider to set culture based on the incoming request’s hostname.
  • Provides commented examples in Program.cs for registering multiple supported cultures and inserting the custom provider.
  • Shows default-en English behavior is preserved by only commenting in new configuration.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
headapps/aspnet-core-starter/Program.cs Added commented examples for supported cultures and provider setup
headapps/aspnet-core-starter/Extensions/HostnameRequestCultureProvider.cs New culture provider mapping hostnames to culture codes
Comments suppressed due to low confidence (3)

headapps/aspnet-core-starter/Program.cs:92

  • Using a hard-coded index ‘4’ to insert the custom culture provider can break if the default provider list changes. Consider finding the index of AcceptLanguageHeaderRequestCultureProvider dynamically or inserting relative to the last provider.
        //options.RequestCultureProviders.Insert(4, new HostnameRequestCultureProvider());

headapps/aspnet-core-starter/Extensions/HostnameRequestCultureProvider.cs:1

  • No unit tests cover HostnameRequestCultureProvider. Consider adding tests for DetermineProviderCultureResult to verify host-to-culture mappings and the default fallback.
using Microsoft.AspNetCore.Localization;

headapps/aspnet-core-starter/Extensions/HostnameRequestCultureProvider.cs:5

  • Add XML doc comments for the HostnameRequestCultureProvider class and its DetermineProviderCultureResult method to explain their responsibilities and usage.
  public class HostnameRequestCultureProvider : RequestCultureProvider

Comment thread headapps/aspnet-core-starter/Extensions/HostnameRequestCultureProvider.cs Outdated
Comment thread headapps/aspnet-core-starter/Extensions/HostnameRequestCultureProvider.cs Outdated
ivanbuzyka and others added 2 commits July 7, 2025 10:32
…Provider.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Provider.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants