Why does changing the thread language and back fail for known folders?
See the question and my original answer on StackOverflowA file/directory name cannot be "localized". But Windows has a specific Known Folders API that you can use to determine a known folder's localized name in any supported/installed Windows language.
Once you get an IKnownFolder COM reference, you can use the IKnownFolder::GetFolderDefinition method to get its pszLocalizedName. It's usually in a form like this: @%SystemRoot%\system32\windows.storage.dll,-21790
From this string you can use the SHLoadIndirectString function which will automatically determine the localized value using the current thread's UI language.
Here is a complete sample code:
#include <windows.h>
#include <stdio.h>
#include <propkey.h>
#include <shlwapi.h>
#include <Shlobj.h>
#pragma comment(lib, "propsys.lib")
#pragma comment(lib, "shlwapi.lib")
void ShowPropName(REFPROPERTYKEY key)
{
IPropertyDescription* desc;
PSGetPropertyDescription(key, IID_PPV_ARGS(&desc));
if (desc)
{
WCHAR* name;
desc->GetDisplayName(&name);
if (name)
{
wprintf(L"Property Name <%s>\n", name);
CoTaskMemFree(name);
}
desc->Release();
}
}
void ShowKnownFolderName(REFKNOWNFOLDERID id)
{
IKnownFolderManager* mgr;
CoCreateInstance(CLSID_KnownFolderManager, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&mgr));
if (mgr)
{
IKnownFolder* folder;
mgr->GetFolder(id, &folder);
if (folder)
{
KNOWNFOLDER_DEFINITION def;
if (SUCCEEDED(folder->GetFolderDefinition(&def)) && def.pszLocalizedName)
{
WCHAR localizedName[256];
SHLoadIndirectString(def.pszLocalizedName, localizedName, _countof(localizedName), nullptr);
wprintf(L"Folder Name <%s>\n", localizedName);
}
folder->Release();
}
mgr->Release();
}
}
int main()
{
LANGID nLangPC;
LANGID nLang2;
CoInitialize(nullptr);
nLang2 = 0x040C; // french
nLangPC = GetThreadUILanguage();
wprintf(L"\n------- 0x%04X ------------\n", nLang2);
SetThreadUILanguage(nLang2);
ShowPropName(PKEY_FileName);
ShowKnownFolderName(FOLDERID_Music);
wprintf(L"\n------- 0x%04X ------------\n", nLangPC);
SetThreadUILanguage(nLangPC);
ShowPropName(PKEY_FileName);
ShowKnownFolderName(FOLDERID_Music);
CoUninitialize();
return 0;
}