Hello everyone.
I would like to make my add-in bilingual. Is there a way to change the UI culture during execution?
Or after restarting the addin?
I have already tried to localize the MainForm, but without success.
I use VB.Net 2019 with Resx Manager and Inventor 2022.
Solved! Go to Solution.
Solved by Michael.Navara. Go to Solution.
The short answer is
Resources.Culture = CultureInfo.GetCultureInfo("cs-CZ");
The long answer with explanation follows:
How to make add-in localized is a complex task.
At first you need to move all localizable "items" to resources. My best experience is to use single resource file for single assembly (.dll file, project). It is hard to maintain multiple resource files.
At second you need to translate items in resources. It can be strings (the most common) and another resources like icons, images etc. Great tool for maintain resource file is the ResXManager.
If you want to use localized resources in code, there is no problem and you can use them directly.
inventor.CommandManager.PromptMessage(Resources.SampleCommand_ResponseMessage, 0);
If you want to use localized resources in WinForms controls and dialogs, my best experience is to create and use my own localization method and call them when the dialog loads. It is much safer an more maintainable, than "localized form". It is not easy to pass localization language to it.
private void CommandDialog_Load(object sender, System.EventArgs e)
{
LocalizeControl();
}
private void LocalizeControl()
{
Text = Resources.SampleCommand_DisplayName;
Icon = Resources.Flag;
lblText.Text = Resources.CommandDialog_lblText_Text;
btnOk.Text = Resources.CommandDialog_btnOK_Text;
btnCancel.Text = Resources.CommandDialog_btnCancel_Text;
}
How to switch the language of your add-in.
As mentioned above, you can switch the Culture of the Resources class. The best time is in Activate method. You can store the language settings in your config file, and load and apply this settings during add-in activation.
You can change the language settings of the add-in later, but for the best result it is necessary to restart the addin. Because for example ButtonDefinition.DisplayName property is read only, and you need to recreate the button (you can't update them).
You can see the sample add-in with localization in attachment. It is in C#, but the concept is the same in VB.NET and I hope you find them useful.
WARNING
One warning at the end. NEVER use the approach where you change the CurrentUICulture of the thread. It has many side-effects and can corrupt the underlying application or loaded add-ins.
// !!! NEVER USE THIS !!!
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(1033);
Can't find what you're looking for? Ask the community or share your knowledge.