Skip to content

Add format methods - #13

Open
LowieDelneste wants to merge 4 commits into
ycanardeau:mainfrom
LowieDelneste:add-format-methods
Open

LowieDelneste wants to merge 4 commits into
ycanardeau:mainfrom
LowieDelneste:add-format-methods

Conversation

@LowieDelneste

Copy link
Copy Markdown

Summary
This PR adds support for generating format-aware overloads for localizer members when a resource contains placeholders such as {0} or {1}.

Changes

  • Updated localizer generation to emit parameterized methods when GenerateFormatMethods is enabled.
  • Adjusted the generated API so format-based resources produce overloads like HiUser(object arg0) instead of the previous property-plus-overload pattern.
  • Exposed the GenerateFormatMethods option through the build props so it can be configured from project metadata.
  • Added and updated tests to cover the new behavior.
  • Documented the option in the README and quick-start guide.

Why
This makes generated localizers more useful for string formatting scenarios and avoids generating redundant members for format-based resources.

Notes
The behavior is opt-in and controlled by the GenerateFormatMethods setting.

@Dresel

Dresel commented Oct 7, 2026

Copy link
Copy Markdown

We use this together with #15 in a local build, and it works well for us. There are two cases where the generated code doesn't fit the text. A fix with tests is on Dresel/ResXGenerator format-methods-fixes, one commit on top of this PR.

1. A placeholder with a format, such as {0:g} or {0,-10}, isn't detected.
<value>Last seen {0:g}</value> gets no method, only the property LastSeen, so the caller has to format the text by hand.

2. A text whose placeholders skip an index gets a method that throws.
<value>Moved {1} to {2}</value>, for example after {0} was removed from the English text while translations still use it, generates

public string Moved(object arg1, object arg2) => stringLocalizer["Moved", arg1, arg2];

string.Format receives two arguments, which fill {0} and {1}, and has nothing for {2}, so the call throws a FormatException. The fix generates a parameter for every index up to the highest, Moved(object arg0, object arg1, object arg2), and arg0 simply goes unused. A build warning for skipped indices would be an alternative.

The fix also ignores escaped braces: {{0}} is literal text and no longer gets a parameter.

One more thing: the README says the option generates "both the existing property-style accessor and a parameterized overload". It generates only the method, since a property and a method can't share a name.

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.

2 participants