Setting OL add in UI language

Add-in Express™ Support Service
That's what is more important than anything else

Setting OL add in UI language
 
Velja Radenkovic




Posts: 39
Joined: 2010-12-23
I found this blog post: https://www.add-in-express.com/creating-addins-blog/2011/05/24/localize-office-addin/?thank=you&t=1479477821#comment-426592

My question is:

How am I supposed to set add in language to match office product language? Last comment is unclear.
What are different ways for add in to be started?
Removing InitializeComponent() call from constructor and waiting for AddinInitialize or OnRibbonBeforeCreate events is not what one can do since those events are assigned in InitializeComponent() procedure so you can not really expect it to be fired before its delegated.


Thanks,
Velja
Posted 18 Nov, 2016 09:37:40 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Velja,

Velja Radenkovic writes:
How am I supposed to set add in language to match office product language?


There are two ways.

If the Office localization is the same as the windows localization, you can just localize the add-in module as described in that blog. In this case, you don't need to do anything - just deploy culture-specific resources to the target machine. When the add-in is loaded by Office, the System.Resources.ResourceManager class detects the localization of the Windows UI and applies the corresponding resources.

If however, you need to use resources so that they always match the localization of Office (whatever the Widows locatlization is), you need to do the following:

- Initialize the controls in the constructor of the add-in module
- In the OnHostApplicationInitialized method you can find out the host application's localization, and modify the localizable properties by setting them directly.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Nov, 2016 07:49:03 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
I should have added that in the second scenario you need to set the Localizable property of the add-in module to false and the Language property to Default.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Nov, 2016 08:13:46 Top
Velja Radenkovic




Posts: 39
Joined: 2010-12-23
I am interested in 2nd scenario. Ribbon controls are not displaying correct language (they are displaying default) when I leave InitializeComponent in constructor and I try to set UICulture and Culture in OnHostApplicationInitialized

System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(this.OutlookApp.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
System.Threading.Thread.CurrentThread.CurrentCulture = culture;

I did set localizable to false and lang is default for the add in module.
Posted 21 Nov, 2016 09:35:28 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Velja,

I'll create a sample project and publish it here. I think this will occur today or maybe tomorrow.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Nov, 2016 04:28:34 Top
Velja Radenkovic




Posts: 39
Joined: 2010-12-23
Thank you. I am waiting for it. Just to be clear ... When I implemented your suggestion everything (windows forms) was displaying correct language, except ribbon controls which were displaying default.
Posted 22 Nov, 2016 07:27:09 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Velja,

We've discussed several ways to solve this and chosen the following one. First, you modify the constructor of the add-in module as shown below.

public AddinModule()
{
    Application.EnableVisualStyles();

    string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower();
    System.Diagnostics.Debug.WriteLine("!!! AddinModule.processName=" + processName);
    if (processName == "devenv" ||          // Visual Studio IDE
        processName == "adxregistrator" ||  // Add-in Express registrator tool
        processName == "msbuild")           // MSBuild.exe
    {
        InitializeComponent();
    }

    // Please add any initialization code to the AddinInitialize event handler
}


That is, the code above does NOT create the components on the module if the module is loaded by an Office application. To create the components you override the OnHostApplicationInitialized() method in this way:

protected override void OnHostApplicationInitialized()
{
    base.OnHostApplicationInitialized();

    if (this.ExcelApp != null)
        SwitchLanguage(GetLanguageID(this.ExcelApp));
    else if (this.WordApp != null)
        SwitchLanguage(GetLanguageID(this.WordApp));
}

private int GetLanguageID(dynamic app)
{
    Office.LanguageSettings languageSettings = app.LanguageSettings;
    if (languageSettings != null)
        try
        {
            return languageSettings.LanguageID[Office.MsoAppLanguageID.msoLanguageIDUI];
        }
        finally { Marshal.ReleaseComObject(languageSettings); }
    return 0;
}

private void SwitchLanguage(int officeLanguageID)
{
    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(officeLanguageID);
    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(officeLanguageID);

    InitializeComponent();
}


Find the example at http://temp.add-in-express.com/support/AddinLocalizationExample-CS.zip.

The approach above allows using all the localization strings you create on the module. Alternative approaches create extra difficulties.

Say, you might choose to call the InitializeComponent both in the constructor and in the OnHostApplicationInitialized method but this requires that before calling InitializeComponent for the second time, you a) delete all components from the module, and b) disconnect from all event of the add-in module.

Another possible way would be to extract resource manager related calls form the InitializeComponent method and repeat them in the OnHostApplicationInitialized method (after setting CurrentCulture and CurrentUICulture). This approach requires duplicating the code; I believe you understand potential problems with doing this.


Andrei Smolin
Add-in Express Team Leader
Posted 23 Nov, 2016 04:55:30 Top
Velja Radenkovic




Posts: 39
Joined: 2010-12-23
I got it working.

I think that Localizable property has to be set to true. If its false, InitializeComponent method doesn't use resources at all i.e. it sets properties to strings like adxRibbonButton1.Caption = "Caption" .
Posted 24 Nov, 2016 18:42:30 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Velja,

Thanks for confirming!

Velja Radenkovic writes:
I think that Localizable property has to be set to true.


Correct.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Nov, 2016 09:46:02 Top