dotmake-build/command-line

Support loading string from ResX

Closed this issue · 2 comments

Could we support some one that can be used for i18n?

Such as System.ComponentModel.DataAnnotations.DisplayAttribute.ResourceType

When I make an i18n app using ResX, I got an error CS0182.

[CliCommand(Description = Resource.CommandDescription)]
internal sealed class Command
{
    // ...
}

Attributes only accept compile-time constants that's why you get CS0182 error. DisplayAttribute has a good solution by accepting a ResourceType but I am thinking of a more elegant solution like this:

[CliCommand(Description = nameof(Resource.CommandDescription))]
internal sealed class Command
{
    // ...
}

nameof operator normally gets the property name as string but our source generator can analyze it and generate resource accessing code automatically. I will implement it soon (may be busy for few days)

FYI, this feature is now ready in v1.6.9:

  • Added support for localizing commands, options and arguments.
    You can now specify a nameof operator expression with a resource property (generated by resx) in the attribute's argument (for string types only)
    and the source generator will smartly use the resource property accessor as the value of the argument so that it can localize at runtime.
    If the property in the nameof operator expression does not point to a resource property, then the name of that property will be used as usual.
[CliCommand(Description = nameof(TestResources.CommandDescription))]
internal class LocalizedCliCommand
{
    [CliOption(Description = nameof(TestResources.OptionDescription))]
    public string Option1 { get; set; } = "DefaultForOption1";

    [CliArgument(Description = nameof(TestResources.ArgumentDescription))]
    public string Argument1 { get; set; }

    public void Run()
    {
        Console.WriteLine($@"Handler for '{GetType().FullName}' is run:");
        Console.WriteLine($@"Value for {nameof(Option1)} property is '{Option1}'");
        Console.WriteLine($@"Value for {nameof(Argument1)} property is '{Argument1}'");
        Console.WriteLine();
    }
}