Repository navigation
Add LocalizedString.Create and LocalizedHtmlString.Create - #19932
Closed
MikeAlhayek wants to merge 2 commits into
Closed
MikeAlhayek wants to merge 2 commits into
MikeAlhayek wants to merge 2 commits into
Conversation
Add C# 14 static extension members that create a LocalizedString or a LocalizedHtmlString where the name is also the value. The extensions live in the same namespaces as the types so they are discoverable without an extra using directive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Set the category of every built-in DeploymentStep in its parameterless constructor with LocalizedString.Create(), and remove the constructors that took an IStringLocalizer<TStep> only to set the category. DeploymentPlanController now translates the category by its name with the step type as the context. This is the same context that IStringLocalizer<TStep> uses, so existing translations continue to work. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
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.
This PR Depends on OrchardCoreContrib/OrchardCoreContrib.PoExtractor#130
Summary
Some APIs need a
LocalizedStringor aLocalizedHtmlString, but there is no localizer where the value is created. Today the code repeats the same text for the name and the value:This PR adds a static
Create(string name)method to both types, as C# 14 extension members, so that the text is written only once:Changes
LocalizedStringExtensionsin theMicrosoft.Extensions.Localizationnamespace, andLocalizedHtmlStringExtensionsin theMicrosoft.AspNetCore.Mvc.Localizationnamespace. Both are inOrchardCore.Localization.Abstractions/Extensions, next to the existingPluralextensions, and use the same namespaces as the types. Code that already uses the types can callLocalizedString.Create(...)with no newusingdirective.Create(name)is the same asnew LocalizedString(name, name)andnew LocalizedHtmlString(name, name). It does not translate the value, andResourceNotFoundisfalse.Code that does not use C# 14 can call
LocalizedStringExtensions.Create(...)orLocalizedHtmlStringExtensions.Create(...).All 53 built-in
DeploymentStepimplementations now set their category in the parameterless constructor withLocalizedString.Create(...). The second constructor, which took anIStringLocalizer<TStep>only to setCategory, is removed:DeploymentPlanControllernow translates the category where it is shown, withIStringLocalizerFactory.Create(step.GetType())[step.Category.Name].IStringLocalizer<TStep>uses the same context (the full name of the step type), so existing PO translations continue to work. Custom steps that still set the category with anIStringLocalizer<TStep>show the same text as before.Docs: a new "Creating a localized string without a localizer" section in
reference/modules/Localize/README.md, an example step with a category in the "Extending deployment" section ofreference/modules/Deployment/README.md, and a 4.0.0 release notes entry.Tests
LocalizedStringExtensionsTestsandLocalizedHtmlStringExtensionsTestsinOrchardCore.Tests.DeploymentStepCategoryTests. They create steps throughDeploymentStepFactory<TStep>, check that the category is set, check that a PO entry with the step type as the context translates it, and check that the result is the same as the oldIStringLocalizer<TStep>result.dotnet build src/OrchardCore.Cms.Web -c Release: no warnings or errors.OrchardCore.Testsrun: 3003 passed, 0 failed, 2 skipped (one Unix-only test, one CI-only test).dotnet test --project test/OrchardCore.Tests -c Release --filter-class "*LocalizedStringExtensionsTests" --filter-class "*LocalizedHtmlStringExtensionsTests" --filter-class "*NullStringLocalizerTests": 31 passed.Related
The PO extractor will pick up
LocalizedString.Create("...")andLocalizedHtmlString.Create("...")when the argument is literal text: OrchardCoreContrib/OrchardCoreContrib.PoExtractor#130🤖 Generated with Claude Code