LocalizableCommandAttribute

Namespace: Digi21.DigiNG.Plugin.Command
Assembly: Digi21.DigiNG.Plugin

Marks a class as a Digi3D.AI command and reads its name, description and help URL from the extension's resources. This allows the command name to change with the interface language.

[AttributeUsage(AttributeTargets.Class)]
public class LocalizableCommandAttribute : CommandAttribute

Inherits from: CommandAttribute. Digi3D.AI treats both classes alike.

Resource provider

The resourceManagerProvider parameter is a type with a non-public static property of type System.Resources.ResourceManager. This is the class Visual Studio generates for a .resx file with the ResXFileCodeGenerator tool, whose ResourceManager property is internal. The class generated by PublicResXFileCodeGenerator declares the property as public and cannot be used.

The constructor finds the first non-public static property of type ResourceManager and calls GetString for each key, using the thread's user interface culture. Values are read when Digi3D.AI reads the class attributes while loading the extension.

The constructor throws ArgumentException:

Parameter When
resourceManagerProvider The type has no non-public static property of type ResourceManager.
resourceKey The resource contains no string with the specified key. The message includes the key.

If the constructor throws the exception, Digi3D.AI cannot read the class attributes: it displays an error message when loading the extension and registers no more commands from that extension.

Constructors

public LocalizableCommandAttribute(Type resourceManagerProvider, string nameResource)
public LocalizableCommandAttribute(Type resourceManagerProvider, string nameResource, string descriptionResource)
public LocalizableCommandAttribute(Type resourceManagerProvider, string nameResource, string descriptionResource, string helpUrlResource)
Parameter Description
resourceManagerProvider Type providing the ResourceManager.
nameResource Resource key containing the command name. The resource string is assigned to Name.
descriptionResource Resource key containing the description. The resource string is assigned to Description, which the Search command dialog displays and searches for the user's text. With the two-parameter constructor, Description is null and the command has no description in Search command.
helpUrlResource Resource key containing the help URL. The resource string is assigned to HelpUrl.

Properties without a parameter in the overload used remain null.

Example

This extension has a Recursos.resx file with the keys CuentaLineasNombre, CuentaLineasDescripcion, CuentaLineasAyuda, CuentaLineasMenu and BuscarLineasCerradas. The Recursos class does the same as the class generated by Visual Studio. The same file uses LocalizableCommandInMenuAttribute and LocalizableSearcherAttribute.

using System.Resources;
using System.Windows.Forms;
using Digi21.Digi3D;
using Digi21.DigiNG.Entities;
using Digi21.DigiNG.Plugin.Command;
using Digi21.DigiNG.Plugin.Menu;
using Digi21.DigiNG.Plugin.Search;

namespace Localizables;

// Resource provider: a non-public static property of type ResourceManager.
// This is what Visual Studio generates for a .resx using ResXFileCodeGenerator.
internal static class Recursos
{
    internal static ResourceManager ResourceManager { get; } =
        new("Localizables.Recursos", typeof(Recursos).Assembly);
}

[LocalizableCommand(typeof(Recursos), "CuentaLineasNombre", "CuentaLineasDescripcion", "CuentaLineasAyuda")]
[LocalizableCommandInMenu(typeof(Recursos), "CuentaLineasMenu", MenuItemGroup.ToolsGroup1)]
public class OrdenCuentaLineas : Command
{
    public OrdenCuentaLineas()
    {
        Initialize += (sender, e) =>
        {
            var líneas = Digi21.DigiNG.DigiNG.DrawingFile.OfType<ReadOnlyLine>().Count(l => !l.Deleted);
            Digi3D.OutputWindow.Show();
            Digi3D.OutputWindow.WriteLine($"{líneas} líneas.");
            Dispose();
        };
    }
}

[LocalizableSearcher(typeof(Recursos), "BuscarLineasCerradas")]
public class BuscadorLineasCerradas : ISearcher
{
    public Form Form => null;

    public IEnumerable<Entity> Search(IEnumerable<Entity> entities) =>
        entities.OfType<ReadOnlyLine>().Where(l => l.Closed);
}