Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

JsonStringLocalizerFactory was using assembly name to calculate resource paths, which fails when root namespace differs from assembly name (configured via <RootNamespace> in csproj).

Changes

  • Added GetRootNamespace() method that checks for RootNamespaceAttribute on the assembly, falling back to assembly name
  • Updated Create(Type resourceSource) to use root namespace instead of assembly name when computing type name
  • Updated Create(string baseName, string location) to use root namespace when trimming the base name prefix
  • Added comprehensive test infrastructure with two dedicated test class libraries:
    • ResourcesClassLibraryNoAttribute: Tests the fallback behavior when RootNamespaceAttribute is not present
    • ResourcesClassLibraryWithAttribute: Tests the attribute behavior with [assembly: RootNamespace("MyCustomNamespace")] where the assembly name differs from the root namespace
  • Added unit tests to verify both scenarios with and without the attribute for both factory methods

This matches the behavior of Microsoft.Extensions.Localization.ResourceManagerStringLocalizerFactory.

Usage

// In assembly with [assembly: RootNamespace("MyCompany.MyProduct")]
// where assembly name is "MyCompany.MyProduct.Web"

// Before: looked for Resources/Web/Controllers/HomeController.json
// After:  looks for Resources/Controllers/HomeController.json
var localizer = factory.Create(typeof(HomeController));

Testing

  • ✅ All 37 unit tests pass, including new tests for RootNamespaceAttribute support
  • ✅ All 5 functional tests pass
  • ✅ Tests verify behavior both with and without RootNamespaceAttribute
Original prompt

This section details on the original issue you should resolve

<issue_title>Support RootNamespaceAttribute</issue_title>
<issue_description></issue_description>

<agent_instructions>Support Microsoft.Extensions.Localization.RootNamespaceAttribute</agent_instructions>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add support for RootNamespaceAttribute in localization Support RootNamespaceAttribute Dec 1, 2025
Copilot AI requested a review from hishamco December 1, 2025 01:31
@hishamco hishamco marked this pull request as ready for review December 9, 2025 19:23
Copy link
Owner

@hishamco hishamco left a comment

Choose a reason for hiding this comment

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

@copilot add two class libraries in the test named ResourcesClassLibraryNoAttribute and ResourcesClassLibraryWithAttribute, then rewrite the unit tests to examine the RootNamespaceAttribute

Copy link
Contributor Author

Copilot AI commented Dec 9, 2025

@copilot add two class libraries in the test named ResourcesClassLibraryNoAttribute and ResourcesClassLibraryWithAttribute, then rewrite the unit tests to examine the RootNamespaceAttribute

I've added the two test class libraries as requested:

  1. ResourcesClassLibraryNoAttribute: Test library without RootNamespaceAttribute (assembly name equals root namespace)
  2. ResourcesClassLibraryWithAttribute: Test library with [assembly: RootNamespace("MyCustomNamespace")] where the assembly name differs from the root namespace

Both libraries have:

  • TestModel class with test resources
  • ResourceLocationAttribute to separate resources into Resources/NoAttr and Resources/WithAttr subdirectories
  • JSON resource files for en and fr-FR cultures

The unit tests have been rewritten to properly examine the RootNamespaceAttribute support with these scenarios:

  • Without attribute: factory uses assembly name as root namespace
  • With attribute: factory uses the specified root namespace instead of assembly name

All 37 unit tests pass. Commit: 222ee51

Copilot AI requested a review from hishamco December 9, 2025 19:40
Copilot AI and others added 2 commits December 10, 2025 03:23
- Created ResourcesClassLibraryNoAttribute: test library without RootNamespaceAttribute
- Created ResourcesClassLibraryWithAttribute: test library with [assembly: RootNamespace("MyCustomNamespace")]
- Added ResourceLocationAttribute to both libraries for proper resource separation
- Rewrote unit tests to verify RootNamespaceAttribute behavior:
  - CreateLocalizerWithType_WithoutRootNamespaceAttribute_UsesAssemblyName
  - CreateLocalizerWithType_WithRootNamespaceAttribute_UsesRootNamespace
  - CreateLocalizerWithBasenameAndLocation_WithoutRootNamespaceAttribute
  - CreateLocalizerWithBasenameAndLocation_WithRootNamespaceAttribute

Co-authored-by: hishamco <[email protected]>
@hishamco hishamco force-pushed the copilot/support-rootnamespaceattribute branch from 222ee51 to 75ad1c1 Compare December 10, 2025 00:23
@hishamco
Copy link
Owner

@copilot fix the warnings

@hishamco hishamco merged commit 81f8588 into main Dec 10, 2025
1 of 2 checks passed
@hishamco hishamco deleted the copilot/support-rootnamespaceattribute branch December 10, 2025 01:11
@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.

Support RootNamespaceAttribute

2 participants