問題描述
我正在開發一個 VSTO 插件,并希望根據辦公產品的語言版本對其進行本地化.理論上是這樣的:
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);
為此,我當然需要初始化 Application
.因此,我可以執行此代碼的最早點是在 Startup 事件處理程序中.然而,此時 CreateRibbonExtensibilityObject()
已經被調用,所以至少我的自定義功能區選項卡的標題將以 Windows 語言顯示,這可能會有所不同.在功能區類中,我有一個 onLoad 事件的處理程序,我在其中存儲 IRibbonUI
的一個實例以供以后使用.我可以將此實例交給插件類并讓它調用 IRibbonUI.Invalidate()
.但這似乎有點奇怪 - 創建一個功能區只是為了在幾微秒后使其無效.所以我想知道 - 并在這里詢問 - 是否有更優雅的方式來根據辦公產品的語言版本本地化 vsto 插件的功能區.
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.)
推薦答案
您始終可以覆蓋 CreateRibbonExtensibilityObject
方法或可能覆蓋其他一些 AddInBase
方法(BeginInit、Initialize 等)掛鉤到加載項加載中的正確位置生命周期.
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.
我之前重寫了 CreateRibbonExtensibilityObject
以確保在加載功能區之前運行初始化代碼.我注意到 CreateRibbonExtensibilityObject
和 Startup
事件是隨機觸發的.有時 Startup
先發生 - 有時 CreateRibbonExtensibilityObject
先觸發.我必須手動同步這兩個事件,以確保在創建功能區之前執行任何初始化代碼.如果 CreateRibbonExtensibilityObject
先觸發 - Application 對象尚未創建.
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);
這將為您檢索對 Application
實例的引用 - 無論它是否已加載到 Initialize
中.
This will retrieve a reference to the Application
instance for you - regardless if it has been loaded in the Initialize
yet.
這篇關于根據辦公產品的語言本地化 VSTO 插件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!