Skip to content

Code Quality: Generative resource management using Source Generator #15504

@0x5bfa

Description

@0x5bfa

Description

Instead of pasting raw string code of resources let’s have a source generator to use properties. The source generator will update the class right after resw file changed.

Concerned code

None. Making new robust code.

Gains

  • Robustness

Requirements

App.Strings.Strings partial class.

If comments start with GENERIC or something we can apart the class into Strings.Generic, Strings.Infecrectable, Strings.Settings, etc. this is just in my mind and not official proposal tho.

Here’s code:

// Strings.Inflectsble.cs
/// <auto-generated/>
public partial class Strings
{
    /// <summary>
    /// (generated comments from resw here)
    /// </summary>
    public string AddNewItem
        =>Add new items”;
}
Strings.AddNewItems.GetLocalized(itemCount);

If we want to make localization methods simplify, let's make Strings.Extension class file as partial.

// Strings.Extension.cs
[MarkupExtensionReturnType(ReturnType = typeof(string))]
public partial class Strings : MarkupExtension
{
    public string Name { get; set; }

    protected override object ProvideValue()
        => GetType().GetProperty(Name)?.GetValue(this);
}

Comments

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions