根据office产品的语言本地化VSTO addin

发布于 2024-12-21 08:01:44 字数 901 浏览 1 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-12-28 08:01:44

您始终可以覆盖 CreateRibbonExtensibilityObject方法或可能覆盖其他一些 AddInBase 方法(BeginInit、Initialize 等)挂钩到 AddIn 加载生命周期中的正确位置。

我之前已经重写了 CreateRibbonExtensibilityObject,以确保在加载功能区之前运行初始化代码。我注意到 CreateRibbonExtensibilityObject 和 Startup 事件是随机触发的。有时 Startup 首先发生 - 有时 CreateRibbonExtensibilityObject 首先触发。我必须手动同步这两个事件,以确保在创建功能区之前执行任何初始化代码。如果 CreateRibbonExtensibilityObject 首先触发 - 应用程序对象尚未创建。

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

这将为您检索对 Application 实例的引用 - 无论它是否已在 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文