Add custom hostname-based request culture provider#24
Open
ivanbuzyka wants to merge 3 commits intoSitecore:mainfrom
Open
Add custom hostname-based request culture provider#24ivanbuzyka wants to merge 3 commits intoSitecore:mainfrom
ivanbuzyka wants to merge 3 commits intoSitecore:mainfrom
Conversation
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.
1 task
There was a problem hiding this comment.
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
HostnameRequestCultureProviderto set culture based on the incoming request’s hostname. - Provides commented examples in
Program.csfor 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
AcceptLanguageHeaderRequestCultureProviderdynamically 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 forDetermineProviderCultureResultto 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
HostnameRequestCultureProviderclass and itsDetermineProviderCultureResultmethod to explain their responsibilities and usage.
public class HostnameRequestCultureProvider : RequestCultureProvider
…Provider.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Provider.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce
HostnameRequestCultureProviderto determine culture based on the request hostname, supporting automatic culture selection. UpdateProgram.cswith 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.nlshould 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.cscontains only commented put code, therefore default behavior of the started app shouldn't be changed.