I’m developing a VSTO addin and want it to be localized according to the language version of the office product. In theory, that’s how to do it:
int lcid = Application.LanguageSettings.get_LanguageID(Office.MsoAppLanguageID.msoLanguageIDUI);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lcid);
For this to work I need Application to be initialized, of course. So the earliest point where I can execute this code is in the Startup event handler. At this point, however, CreateRibbonExtensibilityObject() already has been called, so at least the title of my custom ribbon tab is going to be displayed in the Windows language, which might be different.
In the ribbon class I have a handler for the onLoad event, where I store an instance of IRibbonUI for later use. I could hand over this instance to the addin class and let it call IRibbonUI.Invalidate() on it. But this seems to be a bit strange – creating a ribbon just to invalidate it a couple of microseconds later. So I wonder – and ask here – whether there is a more elegant way to localize the ribbon of a vsto addin according to the language version of the office product.
(I’ve seen this similar question, but the approach offered there by this answer looks even worse to me.)
You can always override the
CreateRibbonExtensibilityObjectmethod or possibly override some of the otherAddInBasemethods (BeginInit, Initialize, etc.) to hook into the proper location in the AddIn load lifecycle.I have overridden the
CreateRibbonExtensibilityObjectbefore to ensure that initialization code is run before the Ribbon is loaded. I have noticed thatCreateRibbonExtensibilityObjectandStartupevents are triggered at random times. SometimesStartuphappens first – sometimesCreateRibbonExtensibilityObjectfires first. I had to manually synchronize the two events to ensure any initialization code is executed prior to Ribbon creation. IfCreateRibbonExtensibilityObjectfires first – the Application object has not yet been created.Try this approach in
CreateRibbonExtensibility:This will retrieve a reference to the
Applicationinstance for you – regardless if it has been loaded in theInitializeyet.