Visual Studio and the .NET framework offer a nice option to localize Windows Forms, Office Ribbons or resources in general. For example, to localize a form, open the form, set
Localizable=True from the properties, choose a language from the
Language property and start changing anything on the form. You'll notice an additional .xx.resx file is being created for each language.
During
InitializeComponent(); the
ResourceManager class is used to localize the form to the current language. To be exact, by current language I mean the current thread's culture. You can change the language of your application or module by changing the
Thread.CurrentThread.CurrentUICulture property to another culture.
So far so good. When developing Office add-ins you will notice that this does not use the current language of Office, but the Windows system language. The trick is to set the
CurrentUICulture to the Office language:
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(this.Application.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));
}
Voila, all dialogs appear in the Office language now, except ribbons. Ribbons are created before
ThisAddin_Startup happens, so you'll have to set the language in the ribbon's constructor, before
InitializeComponent(); of those ribbons. Unfortunally, there is no
Globals.ThisAddin.Application available yet, so you'll have to get the Office object another way.
With the help of genius
Helmut Obertanner from outlooksharp.de I found a rather simple solution: Create a
new Outlook.Application()
Here's the code of my ribbon's constructor:
public MyRibbon() {
Outlook.Application app = new Outlook.Application();
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(app.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));
InitializeComponent();
}
That's it.
Labels: C#, Coding, Office