XCAD.NET
XCAD.NET
Framework for .NET (C# and VB.NET) to create modern application for CAD systems (SOLIDWORKS, SOLIDWORKS Document Manager, Autodesk Inventor, etc.)
Get technical support Connect to xCAD community on Discord server Explore code and application examples xCAD.NET Templates for Visual Studio and VS Code Access source code

Localizing SOLIDWORKS add-ins using xCAD.NET framework


xCAD frameworks supports resources in .NET applications to enable localization of the add-in, e.g. supporting multiple languages.

This technique allows to load localized string at runtime based on the Windows settings in the control panel.

Region and language page in Control Panel
Region and language page in Control Panel

Resources should be added to the corresponding localized .resx file (e.g. Resources.resx for default, Resources.ru.resx for Russian, Resources.fr.resx for French, etc.)

Resource files in the solutions
Resource files in the solutions

In order to reference the string from the resource, use the overloads of the constructors for the TitleAttribute and SummaryAttribute which allows to define title, tooltips, hint string for all elements across xCAD framework (i.e. menu commands, property page controls, macro feature, etc.)

Below is an example which demonstrates this technique. Text is localized as per resources below:

Localized resource files in the Visual Studio
Localized resource files in the Visual Studio

Two commands in menu are localized for Russian and English versions of the add-in.

Localized menu commands
Localized menu commands

[Title(typeof(Resources), nameof(Resources.ToolbarTitle))]
[Summary(typeof(Resources), nameof(Resources.ToolbarHint))]
public enum Commands_e
{
    [Title(typeof(Resources), nameof(Resources.ShowPmpCommandTitle))]
    [Summary(typeof(Resources), nameof(Resources.ShowPmpCommandHint))]
    ShowPmp,

    [Title(typeof(Resources), nameof(Resources.CreateMacroFeatureCommandTitle))]
    [Summary(typeof(Resources), nameof(Resources.CreateMacroFeatureCommandHint))]
    CreateMacroFeature
}

Property Manager Page

Property Manager page title and tooltips for the controls are localized for Russian and English versions of the add-in.

Localized Property Manager Page
Localized Property Manager Page

[Title(typeof(Resources), nameof(Resources.LocalizedPmPageTitle))]
public class LocalizedPmPage
{
    [Title(typeof(Resources), nameof(Resources.TextFieldTitle))]
    [Summary(typeof(Resources), nameof(Resources.TextFieldDescription))]
    public string TextField { get; set; }

    [Title(typeof(Resources), nameof(Resources.NumericFieldTitle))]
    [Summary(typeof(Resources), nameof(Resources.NumericFieldDescription))]
    public double NumericField { get; set; }
}

Macro Feature

Macro feature base name is localized to Russian and English versions of the add-in.

Note. Base name is only assigned while feature creation, feature won't be renamed after locale has changed.

Localized Macro Feature base name
Localized Macro Feature base name

In a similar way it is possible to use strings from the resources to return another data, e.g. text of the error for macro feature.

Localized macro feature error
Localized macro feature error

[Title(typeof(Resources), nameof(Resources.MacroFeatureBaseName))]
[ComVisible(true)]
public class LocalizedMacroFeature : SwMacroFeatureDefinition
{
    public override CustomFeatureRebuildResult OnRebuild(ISwApplication app, ISwDocument model, ISwMacroFeature feature)
    {
        if (!string.IsNullOrEmpty(model.Title))
        {
            return new CustomFeatureRebuildResult()
            {
                Result = true
            };
        }
        else 
        {
            return new CustomFeatureRebuildResult()
            {
                Result = true,
                ErrorMessage = Resources.MacroFeatureError
            };
        }
    }
}


Powered by Docify