根据office产品的语言本地化VSTO addin
我正在开发一个 VSTO 插件,并希望根据 Office 产品的语言版本对其进行本地化。理论上,这就是如何做到这一点:
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 插件的功能区。
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以覆盖
CreateRibbonExtensibilityObject
方法或可能覆盖其他一些
AddInBase
方法(BeginInit、Initialize 等)挂钩到 AddIn 加载生命周期中的正确位置。我之前已经重写了 CreateRibbonExtensibilityObject,以确保在加载功能区之前运行初始化代码。我注意到 CreateRibbonExtensibilityObject 和 Startup 事件是随机触发的。有时
Startup
首先发生 - 有时CreateRibbonExtensibilityObject
首先触发。我必须手动同步这两个事件,以确保在创建功能区之前执行任何初始化代码。如果CreateRibbonExtensibilityObject
首先触发 - 应用程序对象尚未创建。在
CreateRibbonExtensibility
中尝试此方法:这将为您检索对
Application
实例的引用 - 无论它是否已在Initialize
中加载。You can always override the
CreateRibbonExtensibilityObject
method or possibly override some of the otherAddInBase
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 thatCreateRibbonExtensibilityObject
andStartup
events are triggered at random times. SometimesStartup
happens first - sometimesCreateRibbonExtensibilityObject
fires first. I had to manually synchronize the two events to ensure any initialization code is executed prior to Ribbon creation. IfCreateRibbonExtensibilityObject
fires first - the Application object has not yet been created.Try this approach in
CreateRibbonExtensibility
:This will retrieve a reference to the
Application
instance for you - regardless if it has been loaded in theInitialize
yet.