Localize Office add-in based on Office language pack in use rather than Windows' current language

穿精又带淫゛_ 提交于 2019-12-05 12:09:43

I found that the value of Thread.CurrentThread.CurrentCulture corresponded to my system culture, and the value of Thread.CurrentThread.CurrentUICulture corresponded to the Office UI.

So I simply assigned one to the other on add-in startup. Seems to work.

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

This was my approach on fixing this issue. I basically read the registry keys that Ron suggested and forced the culture into the installed language culture. I only support Office 2007 and Office 2010. It sucks that we have to look at CU and LM registry entries for each of the versions of the office, and there is no single internal variable pointing us to the correct registry path. The solution is as follow:

int languageCode = 1033; //Default to english

const string keyEntry = "UILanguage";

if (IsOutlook2010)
{
    const string reg = @"Software\Microsoft\Office\14.0\Common\LanguageResources";
    try
    {
        RegistryKey k = Registry.CurrentUser.OpenSubKey(reg);
        if (k != null && k.GetValue(keyEntry) != null) languageCode = (int)k.GetValue(keyEntry);

    } catch { }

    try
    {
        RegistryKey k = Registry.LocalMachine.OpenSubKey(reg);
        if (k != null && k.GetValue(keyEntry) != null) languageCode = (int)k.GetValue(keyEntry);
    } catch { }
}
else
{
    const string reg = @"Software\Microsoft\Office\12.0\Common\LanguageResources";
    try
    {
        RegistryKey k = Registry.CurrentUser.OpenSubKey(reg);
        if (k != null && k.GetValue(keyEntry) != null) languageCode = (int)k.GetValue(keyEntry);
    } catch { }

    try
    {
        RegistryKey k = Registry.LocalMachine.OpenSubKey(reg);
        if (k != null && k.GetValue(keyEntry) != null) languageCode = (int)k.GetValue(keyEntry);
    } catch { }
}

Resource1.Culture = new CultureInfo(languageCode);

Resource1 is my resource dictionary, and the culture parameter forces all strings to be overridden with that culture when used.

Matthias

There is a MSDN page on Loading Resources Based on Office User Interface Language . The code sample given there works for me. It's using the LanguageSettings from the Application object to determine the current language of the Office UI. I've tested it so far with Word 2010 and Outlook 2010, and I'm pretty sure it runs with the other Office 2010 products as well. I can't say anything on Office 2007, but I would give it a try, since it's so much easier than querying the registry.

For some detail question on how to employ this approach I've just got an answer here by some helpful SO user.

read a bit into http://technet.microsoft.com/en-us/library/cc179091%28office.12%29.aspx

you could read the "HKCU\Software\Microsoft\Office\12.0\Common\LanguageResources\UILanguage" registry key to determine which language the UI is.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!