Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

  • Fix parent culture fallback in JsonResourceManager
  • Add unit tests for parent culture fallback behavior
  • Replace deprecated WebHostBuilder with modern WebApplication pattern

Latest Changes

Replaced the deprecated WebHostBuilder with the modern WebApplication.CreateBuilder() pattern in the CultureBasedResourcesUsesIStringLocalizer test:

  • Replaced WebHostBuilder with WebApplication.CreateBuilder()
  • Updated to use minimal API pattern with app.MapGet()
  • Changed method signature from async void to async Task (fixing xUnit warning)
  • Updated imports: removed Microsoft.AspNetCore.Hosting, added Microsoft.Extensions.Hosting
  • Used app.GetTestClient() instead of creating a TestServer directly
  • Removed unnecessary explicit StopAsync() call (handled by await using)

All 52 unit tests and 5 functional tests pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>Fallback to the default culture resource</issue_title>
<issue_description>The default implementation of IStringLocalizer returns a string from the default culture resource if the localizer can't find the localization in the current resource.

However, the JsonStringLocalizer returns the localization string key value by default.

I've seen the PR Culture fallback to the rooted resource.
Are you going to merge it?</issue_description>

Comments on the Issue (you are @copilot in this section)

@hishamco Do you mean this PR #41? @hishamco Seems the PR is too old, I can confirm that culture fallback is working

[Theory]
[InlineData("fr-FR", "Hello", "Bonjour")]
[InlineData("fr-FR", "Hello, {0}", "Bonjour, {0}")]
[InlineData("fr-FR", "Yes", "Oui")]
[InlineData("fr-FR", "No", "No")]
[InlineData("fr", "Hello", "Bonjour")]
[InlineData("fr", "Yes", "Oui")]
[InlineData("fr", "No", "No")]
public void GetTranslationWithCultureFallback(string culture, string name, string expected)
{
// Arrange
LocalizationHelper.SetCurrentCulture(culture);
// Act
string translation = _localizer[name];
// Assert
Assert.Equal(expected, translation);
}
</comment_new>
<comment_new>@hishamco

Typically, with the standard IStringLocalizer, if a string isn't found in es.json, it falls back to en-US.json. However, with JsonStringLocalizer, it shows the string key instead.

Please show me the localization setup in your code</comment_new>
<comment_new>@hishamco
As I understand from you the fallback culture in the same culture set is working fine, but the fallback to the default culture is not working, right?</comment_new>
<comment_new>@hishamco
I will create a unit test, and then check</comment_new>
<comment_new>@hishamco
Please check one case before I move further make sure that the requested resource is available in the default culture JSON file in your case en-US.json otherwise showing the resource name itself it make sense to me because it's not localized</comment_new>
<comment_new>@hishamco
Let me double-check or write a functional test for this case</comment_new>
<comment_new>@hishamco
@sivashkevych seems you confused with the culture fallback. If the culture is not exists in the supported culture then the fallback will be applied in your case if the culture is fr it SHOULD work, but if the localization find the resource on the supported culture it will show the same resource name if the value is not found

Hope it solved your problem</comment_new>
<comment_new>@hishamco
Anything to add here @sivashkevych?</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: hishamco <3237266+hishamco@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix fallback to default culture resource in JsonStringLocalizer Fix parent culture fallback in JsonResourceManager Dec 1, 2025
Copilot AI requested a review from hishamco December 1, 2025 01:30
@hishamco
Copy link
Owner

hishamco commented Dec 1, 2025

@copilot can you add unit tests to make sure nothing is broken

Co-authored-by: hishamco <3237266+hishamco@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 1, 2025

@copilot can you add unit tests to make sure nothing is broken

Added unit tests in commit 4efe03c:

  • GetTranslationWithParentCultureFallback_FallsBackToParentCulture - verifies fallback from fr-FR to fr for keys that only exist in parent culture
  • GetTranslation_ResourceNotFound_ReturnsCorrectFlag - verifies ResourceNotFound flag is correctly set for keys found via fallback vs non-existent keys
  • GetAllStrings_WithIncludeParentCultures_IncludesParentCultureStrings - verifies parent culture strings are included

All 40 unit tests and 5 functional tests pass.

@hishamco hishamco marked this pull request as ready for review December 10, 2025 03:19
[Fact]
public async void CultureBasedResourcesUsesIStringLocalizer()
{
var webHostBuilder = new WebHostBuilder()
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot replace the deprecated WebhostBuilder

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced deprecated WebHostBuilder with modern WebApplication.CreateBuilder() pattern in commit f6e4d48. Also changed method signature from async void to async Task and updated to use minimal API pattern with app.MapGet().

Co-authored-by: hishamco <3237266+hishamco@users.noreply.github.com>
@hishamco hishamco merged commit f6e4d48 into main Dec 10, 2025
1 check passed
@hishamco hishamco deleted the copilot/fix-culture-fallback-issue branch December 10, 2025 03:54
@hishamco hishamco added this to the 4.0.0 milestone Dec 10, 2025
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.

Fallback to the default culture resource

2 participants