根据办公产品的语言本地化 VSTO 插件 [英] localize VSTO addin according to the language of the office product

查看:11
本文介绍了根据办公产品的语言本地化 VSTO 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 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 以确保在加载功能区之前运行初始化代码.我注意到 CreateRibbonExtensibilityObjectStartup 事件是随机触发的.有时 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 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆