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.
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.)
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:
Menu
Two commands in menu are localized for Russian and English versions of the add-in.
[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.
[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.
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.
[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 }; } } }