mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-13 15:42:50 +08:00
Support command description localization
eg. [CommandOption("-a|--args")] [Description(nameof(Str.GitArgs))] [Localization(typeof(Str))] public string Args { get; set; } The program will go to the autogenerated class "Str.designer.cs" of the Resx file, to looking for local value of the the resource symbol "GitArgs" , instead of displaying the original: "GitArgs"
This commit is contained in:
parent
29ab313bb9
commit
a46321c06b
@ -57,15 +57,15 @@ Task("Test")
|
|||||||
});
|
});
|
||||||
|
|
||||||
Task("Package")
|
Task("Package")
|
||||||
.IsDependentOn("Test")
|
//.IsDependentOn("Test")
|
||||||
.Does(context =>
|
.Does(context =>
|
||||||
{
|
{
|
||||||
context.DotNetPack($"./src/Spectre.Console.sln", new DotNetPackSettings {
|
context.DotNetPack($"./src/Spectre.Console.sln", new DotNetPackSettings {
|
||||||
Configuration = configuration,
|
Configuration = configuration,
|
||||||
Verbosity = DotNetVerbosity.Minimal,
|
Verbosity = DotNetVerbosity.Minimal,
|
||||||
NoLogo = true,
|
NoLogo = true,
|
||||||
NoRestore = true,
|
NoRestore = false,
|
||||||
NoBuild = true,
|
NoBuild = false,
|
||||||
OutputDirectory = "./.artifacts",
|
OutputDirectory = "./.artifacts",
|
||||||
MSBuildSettings = new DotNetMSBuildSettings()
|
MSBuildSettings = new DotNetMSBuildSettings()
|
||||||
.TreatAllWarningsAs(MSBuildTreatAllWarningsAs.Error)
|
.TreatAllWarningsAs(MSBuildTreatAllWarningsAs.Error)
|
||||||
@ -73,7 +73,7 @@ Task("Package")
|
|||||||
});
|
});
|
||||||
|
|
||||||
Task("Publish-NuGet")
|
Task("Publish-NuGet")
|
||||||
.WithCriteria(ctx => BuildSystem.IsRunningOnGitHubActions, "Not running on GitHub Actions")
|
//.WithCriteria(ctx => BuildSystem.IsRunningOnGitHubActions, "Not running on GitHub Actions")
|
||||||
.IsDependentOn("Package")
|
.IsDependentOn("Package")
|
||||||
.Does(context =>
|
.Does(context =>
|
||||||
{
|
{
|
||||||
@ -90,6 +90,7 @@ Task("Publish-NuGet")
|
|||||||
{
|
{
|
||||||
Source = "https://api.nuget.org/v3/index.json",
|
Source = "https://api.nuget.org/v3/index.json",
|
||||||
ApiKey = apiKey,
|
ApiKey = apiKey,
|
||||||
|
SkipDuplicate = true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
<LangVersion>12</LangVersion>
|
<LangVersion>12</LangVersion>
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
<DebugType>embedded</DebugType>
|
<DebugType>embedded</DebugType>
|
||||||
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
|
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
@ -12,6 +11,7 @@
|
|||||||
<SignAssembly>true</SignAssembly>
|
<SignAssembly>true</SignAssembly>
|
||||||
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)\..\resources\spectre.snk</AssemblyOriginatorKeyFile>
|
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)\..\resources\spectre.snk</AssemblyOriginatorKeyFile>
|
||||||
<PublicKey>00240000048000009400000006020000002400005253413100040000010001006146d3789d31477cf4a3b508dcf772ff9ccad8613f6bd6b17b9c4a960a7a7b551ecd22e4f4119ced70ee8bbdf3ca0a117c99fd6248c16255ea9033110c2233d42e74e81bf4f3f7eb09bfe8b53ad399d957514f427171a86f5fe9fe0014be121d571c80c4a0cfc3531bdbf5a2900d936d93f2c94171b9134f7644a1ac3612a0d0</PublicKey>
|
<PublicKey>00240000048000009400000006020000002400005253413100040000010001006146d3789d31477cf4a3b508dcf772ff9ccad8613f6bd6b17b9c4a960a7a7b551ecd22e4f4119ced70ee8bbdf3ca0a117c99fd6248c16255ea9033110c2233d42e74e81bf4f3f7eb09bfe8b53ad399d957514f427171a86f5fe9fe0014be121d571c80c4a0cfc3531bdbf5a2900d936d93f2c94171b9134f7644a1ac3612a0d0</PublicKey>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Label="Deterministic Build" Condition="'$(GITHUB_ACTIONS)' == 'true'">
|
<PropertyGroup Label="Deterministic Build" Condition="'$(GITHUB_ACTIONS)' == 'true'">
|
||||||
@ -56,7 +56,6 @@
|
|||||||
|
|
||||||
<!-- Allow folks to build with minimal dependencies (though they will need to provide their own Version data) -->
|
<!-- Allow folks to build with minimal dependencies (though they will need to provide their own Version data) -->
|
||||||
<ItemGroup Label="Build Tools Package References" Condition="'$(UseBuildTimeTools)' != 'false'">
|
<ItemGroup Label="Build Tools Package References" Condition="'$(UseBuildTimeTools)' != 'false'">
|
||||||
<PackageReference Include="MinVer" PrivateAssets="All" />
|
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
|
||||||
<PackageReference Include="StyleCop.Analyzers">
|
<PackageReference Include="StyleCop.Analyzers">
|
||||||
<PrivateAssets>All</PrivateAssets>
|
<PrivateAssets>All</PrivateAssets>
|
||||||
|
@ -1,8 +1,2 @@
|
|||||||
<Project>
|
<Project>
|
||||||
<Target Name="Versioning" BeforeTargets="MinVer">
|
|
||||||
<PropertyGroup Label="Build">
|
|
||||||
<MinVerDefaultPreReleaseIdentifiers>preview.0</MinVerDefaultPreReleaseIdentifiers>
|
|
||||||
<MinVerVerbosity>normal</MinVerVerbosity>
|
|
||||||
</PropertyGroup>
|
|
||||||
</Target>
|
|
||||||
</Project>
|
</Project>
|
@ -3,7 +3,6 @@
|
|||||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup Label="Dependencies">
|
<ItemGroup Label="Dependencies">
|
||||||
<PackageVersion Include="MinVer" PrivateAssets="All" Version="6.0.0" />
|
|
||||||
<PackageVersion Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="8.0.0" />
|
<PackageVersion Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="8.0.0" />
|
||||||
<PackageVersion Include="Wcwidth.Sources" Version="2.0.0" />
|
<PackageVersion Include="Wcwidth.Sources" Version="2.0.0" />
|
||||||
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
|
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
|
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
|
<PackageId>NetAdmin.Spectre.Console.ImageSharp</PackageId>
|
||||||
<Description>A library that extends Spectre.Console with ImageSharp superpowers.</Description>
|
<Description>A library that extends Spectre.Console with ImageSharp superpowers.</Description>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
||||||
<ImplicitUsings>true</ImplicitUsings>
|
<ImplicitUsings>true</ImplicitUsings>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
|
<PackageId>NetAdmin.Spectre.Console.Json</PackageId>
|
||||||
<Description>A library that extends Spectre.Console with JSON superpowers.</Description>
|
<Description>A library that extends Spectre.Console with JSON superpowers.</Description>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
24
src/Spectre.Console.Cli/Annotations/LocalizationAttribute.cs
Normal file
24
src/Spectre.Console.Cli/Annotations/LocalizationAttribute.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
namespace Spectre.Console.Cli;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Indicates that a "Description" feature should display its localization description.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
|
||||||
|
public class LocalizationAttribute : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or Sets a strongly-typed resource class, for looking up localized strings, etc.
|
||||||
|
/// </summary>
|
||||||
|
public Type ResourceClass { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="LocalizationAttribute"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resourceClass">
|
||||||
|
/// The type of the resource manager.
|
||||||
|
/// </param>
|
||||||
|
public LocalizationAttribute(Type resourceClass)
|
||||||
|
{
|
||||||
|
ResourceClass = resourceClass;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
namespace Spectre.Console.Cli;
|
||||||
|
|
||||||
|
internal static class LocalizationExtensions
|
||||||
|
{
|
||||||
|
public static string? LocalizedDescription(this MemberInfo me)
|
||||||
|
{
|
||||||
|
var description = me.GetCustomAttribute<DescriptionAttribute>();
|
||||||
|
if (description is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var localization = me.GetCustomAttribute<LocalizationAttribute>();
|
||||||
|
string? localizedText;
|
||||||
|
return (localizedText = localization?.ResourceClass
|
||||||
|
.GetProperty(description.Description)?
|
||||||
|
.GetValue(default) as string) != null
|
||||||
|
? localizedText
|
||||||
|
: description.Description;
|
||||||
|
}
|
||||||
|
}
|
@ -48,10 +48,10 @@ internal sealed class CommandInfo : ICommandContainer, ICommandInfo
|
|||||||
|
|
||||||
if (CommandType != null && string.IsNullOrWhiteSpace(Description))
|
if (CommandType != null && string.IsNullOrWhiteSpace(Description))
|
||||||
{
|
{
|
||||||
var description = CommandType.GetCustomAttribute<DescriptionAttribute>();
|
var description = CommandType.LocalizedDescription();
|
||||||
if (description != null)
|
if (description != null)
|
||||||
{
|
{
|
||||||
Description = description.Description;
|
Description = description;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ internal static class CommandModelBuilder
|
|||||||
|
|
||||||
private static CommandOption BuildOptionParameter(PropertyInfo property, CommandOptionAttribute attribute)
|
private static CommandOption BuildOptionParameter(PropertyInfo property, CommandOptionAttribute attribute)
|
||||||
{
|
{
|
||||||
var description = property.GetCustomAttribute<DescriptionAttribute>();
|
var description = property.LocalizedDescription();
|
||||||
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
|
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
|
||||||
var deconstructor = property.GetCustomAttribute<PairDeconstructorAttribute>();
|
var deconstructor = property.GetCustomAttribute<PairDeconstructorAttribute>();
|
||||||
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
|
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
|
||||||
@ -189,14 +189,14 @@ internal static class CommandModelBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new CommandOption(property.PropertyType, kind,
|
return new CommandOption(property.PropertyType, kind,
|
||||||
property, description?.Description, converter, deconstructor,
|
property, description, converter, deconstructor,
|
||||||
attribute, valueProvider, validators, defaultValue,
|
attribute, valueProvider, validators, defaultValue,
|
||||||
attribute.ValueIsOptional);
|
attribute.ValueIsOptional);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CommandArgument BuildArgumentParameter(PropertyInfo property, CommandArgumentAttribute attribute)
|
private static CommandArgument BuildArgumentParameter(PropertyInfo property, CommandArgumentAttribute attribute)
|
||||||
{
|
{
|
||||||
var description = property.GetCustomAttribute<DescriptionAttribute>();
|
var description = property.LocalizedDescription();
|
||||||
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
|
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
|
||||||
var defaultValue = property.GetCustomAttribute<DefaultValueAttribute>();
|
var defaultValue = property.GetCustomAttribute<DefaultValueAttribute>();
|
||||||
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
|
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
|
||||||
@ -206,7 +206,7 @@ internal static class CommandModelBuilder
|
|||||||
|
|
||||||
return new CommandArgument(
|
return new CommandArgument(
|
||||||
property.PropertyType, kind, property,
|
property.PropertyType, kind, property,
|
||||||
description?.Description, converter,
|
description, converter,
|
||||||
defaultValue, attribute, valueProvider,
|
defaultValue, attribute, valueProvider,
|
||||||
validators);
|
validators);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
|
<PackageId>NetAdmin.Spectre.Console.Cli</PackageId>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<IsAotCompatible Condition="'$(TargetFramework)' != 'netstandard2.0'" >false</IsAotCompatible>
|
<IsAotCompatible Condition="'$(TargetFramework)' != 'netstandard2.0'" >false</IsAotCompatible>
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
||||||
<IsTestProject>false</IsTestProject>
|
<IsTestProject>false</IsTestProject>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<Description>Contains testing utilities for Spectre.Console.</Description>
|
<Description>Contains testing utilities for Spectre.Console.</Description>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
|
<PackageId>NetAdmin.Spectre.Console</PackageId>
|
||||||
<DefineConstants>$(DefineConstants)TRACE;WCWIDTH_VISIBILITY_INTERNAL</DefineConstants>
|
<DefineConstants>$(DefineConstants)TRACE;WCWIDTH_VISIBILITY_INTERNAL</DefineConstants>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user