問(wèn)題描述
我正在開(kāi)發(fā)一個(gè) VSTO 插件,并希望根據(jù)辦公產(chǎn)品的語(yǔ)言版本對(duì)其進(jìn)行本地化.理論上是這樣的:
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);
為此,我當(dāng)然需要初始化 Application
.因此,我可以執(zhí)行此代碼的最早點(diǎn)是在 Startup 事件處理程序中.然而,此時(shí) CreateRibbonExtensibilityObject()
已經(jīng)被調(diào)用,所以至少我的自定義功能區(qū)選項(xiàng)卡的標(biāo)題將以 Windows 語(yǔ)言顯示,這可能會(huì)有所不同.在功能區(qū)類中,我有一個(gè) onLoad 事件的處理程序,我在其中存儲(chǔ) IRibbonUI
的一個(gè)實(shí)例以供以后使用.我可以將此實(shí)例交給插件類并讓它調(diào)用 IRibbonUI.Invalidate()
.但這似乎有點(diǎn)奇怪 - 創(chuàng)建一個(gè)功能區(qū)只是為了在幾微秒后使其無(wú)效.所以我想知道 - 并在這里詢問(wèn) - 是否有更優(yōu)雅的方式來(lái)根據(jù)辦公產(chǎn)品的語(yǔ)言版本本地化 vsto 插件的功能區(qū).
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.
(我見(jiàn)過(guò) 這個(gè)類似的問(wèn)題,但是那里提供的方法這個(gè)答案對(duì)我來(lái)說(shuō)看起來(lái)更糟.)
(I've seen this similar question, but the approach offered there by this answer looks even worse to me.)
推薦答案
您始終可以覆蓋 CreateRibbonExtensibilityObject
方法或可能覆蓋其他一些 AddInBase
方法(BeginInit、Initialize 等)掛鉤到加載項(xiàng)加載中的正確位置生命周期.
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.
我之前重寫(xiě)了 CreateRibbonExtensibilityObject
以確保在加載功能區(qū)之前運(yùn)行初始化代碼.我注意到 CreateRibbonExtensibilityObject
和 Startup
事件是隨機(jī)觸發(fā)的.有時(shí) Startup
先發(fā)生 - 有時(shí) CreateRibbonExtensibilityObject
先觸發(fā).我必須手動(dòng)同步這兩個(gè)事件,以確保在創(chuàng)建功能區(qū)之前執(zhí)行任何初始化代碼.如果 CreateRibbonExtensibilityObject
先觸發(fā) - Application 對(duì)象尚未創(chuàng)建.
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.
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);
這將為您檢索對(duì) Application
實(shí)例的引用 - 無(wú)論它是否已加載到 Initialize
中.
This will retrieve a reference to the Application
instance for you - regardless if it has been loaded in the Initialize
yet.
這篇關(guān)于根據(jù)辦公產(chǎn)品的語(yǔ)言本地化 VSTO 插件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!