Extract strings from LocalizedString and LocalizedHtmlString constructors and Create calls - #130
MikeAlhayek wants to merge 5 commits into
Conversation
Add a LocalizedStringExtractor for C# and VB that extracts the name argument of a LocalizedString constructor call when it is a string literal. This is in addition to the existing S["Foo"] / H["Foo"] / T["Foo"] conventions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Extract LocalizedString.Create("...") and LocalizedHtmlString.Create("...")
factory calls, including the LocalizedStringExtensions and
LocalizedHtmlStringExtensions forms.
- Move the type names to LocalizedStringTypes in the shared DotNet project.
- Move TryGetString to an internal ExpressionSyntaxExtensions helper.
- Apply README wording suggestions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| LocalizedStringExtensionsTypeName, | ||
| LocalizedHtmlStringExtensionsTypeName |
There was a problem hiding this comment.
Why have these been introduced or is it Claude :)
There was a problem hiding this comment.
To allow LocalizedString.Create("...") and LocalizedHtmlString.Create("...") so we are not typing the same string twice.
There was a problem hiding this comment.
I'm curious about the Create method, AFAIK, LocalizedString doesn't have such a method
There was a problem hiding this comment.
It does not. I added it in OC. OrchardCMS/OrchardCore#19933
There was a problem hiding this comment.
We talked about it during triage and that is a sound approach.
There was a problem hiding this comment.
LocalizedString.Create and LocalizedHtmlString.Create accept format arguments. The first argument is the message ID, and the other arguments are ignored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I think we need to close this after merging OrchardCMS/OrchardCore#19972, right? |
|
Yes. |
|
I have something to add to what you did guys on these PR, hope to join tonight |
Summary
Some code creates localizable strings directly, without a localizer:
Before this change, the extractor found only
S["Foo"],H["Foo"], andT["Foo"]. Strings created with theLocalizedString(Microsoft.Extensions.Localization) orLocalizedHtmlString(Microsoft.AspNetCore.Mvc.Localization) constructors were not added to the POT file.This PR adds a
LocalizedStringExtractorfor C# and VB and registers it inCSharpProjectProcessorandVisualBasicProjectProcessor.Behavior
name) argument as themsgid. This is the key that the string is localized by. The second (value) argument and any other arguments are ignored.nameargument is a string literal.nameof(...), variables, and other expressions are skipped.new LocalizedString("Foo")) is not in .NET today. It is supported so that the extractor is ready for a helper or a future constructor where the name is also the value. A call with no arguments is skipped.global::type names, for examplenew Microsoft.Extensions.Localization.LocalizedString(...).LocalizedString.Create("...")andLocalizedHtmlString.Create("...")calls, and theLocalizedStringExtensions.Create("...")andLocalizedHtmlStringExtensions.Create("...")forms for callers that do not use C# 14. The type and method names are inLocalizedStringTypesinOrchardCoreContrib.PoExtractor.DotNet, so the C# and VB extractors share them."a " + "b") is supported. The concatenation logic moved fromSingularStringExtractorto an internalExpressionSyntaxExtensionshelper, so both extractors use it.new("Foo", "Foo")is not supported, because the type cannot be known from syntax alone.The README "Limitations" section documents the new convention.
Tests
LocalizedStringExtractorTestsin the C# and VB test projects, with positive and negative cases for both types, the single argument form, and theCreatecalls.dotnet test -c Release: all 89 tests pass.new LocalizedString("From constructor", "From constructor")shows in the generated POT file with the correctmsgctxt.LocalizedString.Create("...")andLocalizedHtmlString.Create("...")are also extracted.new LocalizedString(nameof(C), "...")andLocalizedString.Create(nameof(E))are skipped.Related
The
LocalizedString.CreateandLocalizedHtmlString.Createhelpers are proposed for Orchard Core in OrchardCMS/OrchardCore#19933🤖 Generated with Claude Code