Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
852 views
in Technique[技术] by (71.8m points)

.net - localize VSTO addin according to the language of the office product

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can always override the CreateRibbonExtensibilityObject method or possibly override some of the other AddInBase methods (BeginInit, Initialize, etc.) to hook into the proper location in the AddIn load lifecycle.

I have overridden the CreateRibbonExtensibilityObject before to ensure that initialization code is run before the Ribbon is loaded. I have noticed that CreateRibbonExtensibilityObject and Startup events are triggered at random times. Sometimes Startup happens first - sometimes CreateRibbonExtensibilityObject fires first. I had to manually synchronize the two events to ensure any initialization code is executed prior to Ribbon creation. If CreateRibbonExtensibilityObject fires first - the Application object has not yet been created.

Try this approach in CreateRibbonExtensibility:

 Outlook.Application app = this.GetHostItem<Outlook.Application>(typeof(Outlook.Application), "Application");
 int lcid = app.LanguageSettings.get_LanguageID(Office.MsoAppLanguageID.msoLanguageIDUI);
 Thread.CurrentThread.CurrentUICulture = new CultureInfo(lcid);

This will retrieve a reference to the Application instance for you - regardless if it has been loaded in the Initialize yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...