From: lx-s Date: Fri, 3 Nov 2023 08:16:10 +0000 (+0100) Subject: #26 Save and load languages X-Git-Tag: 2.4.9.0~77^2~28 X-Git-Url: https://repos.lx-s.de/gitweb/?a=commitdiff_plain;h=211d5e88a55ddb9e3f56be3dee97ab0b36c067aa;p=winmute.git #26 Save and load languages --- diff --git a/.gitignore b/.gitignore index 18615fe..6677d51 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ Debug/ *.exp *.pdb *.lib +*.ods# *.dll *.exe diff --git a/WinMute/Settings_GeneralDlg.cpp b/WinMute/Settings_GeneralDlg.cpp index 09e2d7d..dff6612 100644 --- a/WinMute/Settings_GeneralDlg.cpp +++ b/WinMute/Settings_GeneralDlg.cpp @@ -55,7 +55,7 @@ static void FillLanguageList(HWND hLanguageList, const SettingsGeneralData& dlgD ComboBox_SetItemData(hLanguageList, itemId, lang.fileName.c_str()); } } - ComboBox_SelectString(hLanguageList, 0, WMi18n::GetInstance().GetcurrentLanguageName().c_str()); + ComboBox_SelectString(hLanguageList, 0, WMi18n::GetInstance().GetCurrentLanguageName().c_str()); } INT_PTR CALLBACK Settings_GeneralDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) @@ -145,14 +145,17 @@ INT_PTR CALLBACK Settings_GeneralDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPA const wchar_t *selectedLang = reinterpret_cast(ComboBox_GetItemData(hLanguageSelector, curLangSel)); if (selectedLang != nullptr) { if (!WMi18n::GetInstance().LoadLanguage(selectedLang)) { - TaskDialog(hDlg, - hglobInstance, - PROGRAM_NAME, - L"Failed to load selected language.", - L"Please report this error to the WinMute issue tracker.", - TDCBF_OK_BUTTON, - TD_ERROR_ICON, - nullptr); + TaskDialog( + hDlg, + hglobInstance, + PROGRAM_NAME, + L"Failed to load selected language.", + L"Please report this error to the WinMute issue tracker.", + TDCBF_OK_BUTTON, + TD_ERROR_ICON, + nullptr); + } else { + dlgData->settings->SetValue(SettingsKey::APP_LANGUAGE, selectedLang); } } } diff --git a/WinMute/WMi18n.cpp b/WinMute/WMi18n.cpp index 59acd2d..ce46411 100644 --- a/WinMute/WMi18n.cpp +++ b/WinMute/WMi18n.cpp @@ -88,7 +88,7 @@ std::vector WMi18n::GetAvailableLanguages() const continue; } wchar_t langName[255 + 1]; - if (LoadStringW(hLangFile, IDI_LANG_NAME, langName, ARRAY_SIZE(langName)) <= 0) { + if (LoadStringW(hLangFile, IDS_LANG_NAME, langName, ARRAY_SIZE(langName)) <= 0) { WMLog &log = WMLog::GetInstance(); log.WriteWindowsError(L"LoadStringW", GetLastError()); log.Write(L"Failed to load language %ls", wfd.cFileName); @@ -108,9 +108,9 @@ std::wstring WMi18n::GetCurrentLanguageModule() const return curModuleName_; } -std::wstring WMi18n::GetcurrentLanguageName() const +std::wstring WMi18n::GetCurrentLanguageName() const { - return GetString(IDI_LANG_NAME); + return GetString(IDS_LANG_NAME); } // https://learn.microsoft.com/en-us/windows/win32/intl/creating-a-multilingual-user-interface-application @@ -125,16 +125,16 @@ bool WMi18n::LoadLanguage(const std::wstring &dllName) if (!langModPath.has_value()) { return false; } - fs::path pathDllName{ dllName }; + const fs::path pathDllName{ dllName }; *langModPath /= pathDllName.filename(); // Sanitize if (!fs::exists(*langModPath)) { - // TODO: Error + WMLog::GetInstance().Write(L"Language module \"%s\" does not exist", langModPath->c_str()); } else { auto newLangModule = LoadLibraryExW( langModPath->c_str(), nullptr, LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_AS_DATAFILE); if (newLangModule == nullptr) { - // TODO: Error + WMLog::GetInstance().WriteWindowsError(L"LoadLibraryW", GetLastError()); } else { UnloadLanguage(); langModule_ = newLangModule; diff --git a/WinMute/WMi18n.h b/WinMute/WMi18n.h index 962249d..5c28458 100644 --- a/WinMute/WMi18n.h +++ b/WinMute/WMi18n.h @@ -49,7 +49,7 @@ public: std::optional GetLanguageModulesPath() const; std::wstring GetCurrentLanguageModule() const; - std::wstring GetcurrentLanguageName() const; + std::wstring GetCurrentLanguageName() const; std::wstring GetString(UINT id) const; diff --git a/WinMute/WinMain.cpp b/WinMute/WinMain.cpp index 80615e8..c83d0f2 100644 --- a/WinMute/WinMain.cpp +++ b/WinMute/WinMain.cpp @@ -57,28 +57,46 @@ static void LoadLanguage(WMSettings &settings, WMi18n &i18n) } } -int WINAPI wWinMain(_In_ HINSTANCE hInstance, +static bool InitWindowsComponents() +{ + INITCOMMONCONTROLSEX initComCtrl; + initComCtrl.dwSize = sizeof(INITCOMMONCONTROLSEX); + initComCtrl.dwICC = ICC_LINK_CLASS; + if (InitCommonControlsEx(&initComCtrl) == FALSE) { + WMLog::GetInstance().WriteWindowsError(L"InitCommonControlsEx", GetLastError()); + return FALSE; + } else if (CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED) != S_OK) { + WMLog::GetInstance().WriteWindowsError(L"CoInitializeEx", GetLastError()); + return FALSE; + } + return TRUE; +} + +int WINAPI wWinMain( + _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE, _In_ PWSTR, _In_ int) { + hglobInstance = hInstance; WMSettings settings; WMi18n& i18n = WMi18n::GetInstance(); if (!settings.Init()) { - TaskDialog(nullptr, - nullptr, - PROGRAM_NAME, - L"Failed to initialize settings", - L"Critical error while initializing WinMute", - TDCBF_OK_BUTTON, - TD_ERROR_ICON, - nullptr); + TaskDialog( + nullptr, + nullptr, + PROGRAM_NAME, + i18n.GetString(IDS_MAIN_ERROR_SETTINGS_INIT_TITLE).c_str(), + i18n.GetString(IDS_MAIN_ERROR_SETTINGS_INIT_TEXT).c_str(), + TDCBF_OK_BUTTON, + TD_ERROR_ICON, + nullptr); return FALSE; } LoadLanguage(settings, i18n); - HANDLE hMutex = CreateMutexW(nullptr, TRUE, L"LxSystemsWinMute"); + HANDLE hMutex = CreateMutexW(nullptr, TRUE, L"LxSystemsWinMuteMtx"); if (hMutex == nullptr) { return FALSE; } else if (GetLastError() == ERROR_ALREADY_EXISTS) { @@ -87,9 +105,8 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, nullptr, nullptr, PROGRAM_NAME, - L"WinMute is already running", - L"Please look for a white speaker icon in your Windows " - L" taskbar notification area.", + i18n.GetString(IDS_MAIN_ERROR_WINMUTE_ALREADY_RUNNING_TITLE).c_str(), + i18n.GetString(IDS_MAIN_ERROR_WINMUTE_ALREADY_RUNNING_TEXT).c_str(), TDCBF_OK_BUTTON, TD_INFORMATION_ICON, nullptr); @@ -98,34 +115,16 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, SetWorkingDirectory(); - hglobInstance = hInstance; HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0); - INITCOMMONCONTROLSEX initComCtrl; - initComCtrl.dwSize = sizeof(INITCOMMONCONTROLSEX); - initComCtrl.dwICC = ICC_LINK_CLASS; - if (InitCommonControlsEx(&initComCtrl) == FALSE) { - TaskDialog( - nullptr, - nullptr, - PROGRAM_NAME, - L"Failed to start WinMute.", - L"WinMute encountered a critical error while initializing and must shut down.", - TDCBF_OK_BUTTON, - TD_ERROR_ICON, - nullptr); - WMLog::GetInstance().WriteWindowsError(L"InitCommonControlsEx", GetLastError()); - ReleaseMutex(hMutex); - return FALSE; - } else if (CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED) != S_OK) { - WMLog::GetInstance().WriteWindowsError(L"CoInitializeEx", GetLastError()); + if (!InitWindowsComponents()) { TaskDialog( nullptr, nullptr, PROGRAM_NAME, - L"Failed to start WinMute.", - L"WinMute encountered a critical error while initializing and must shut down.", + i18n.GetString(IDS_MAIN_ERROR_INIT_WINMUTE_TITLE).c_str(), + i18n.GetString(IDS_MAIN_ERROR_INIT_WINMUTE_TEXT).c_str(), TDCBF_OK_BUTTON, TD_ERROR_ICON, nullptr); diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index e100965..2da7905 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -524,6 +524,16 @@ BEGIN END END +#endif // German (Germany) resources +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// English (United Kingdom) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENG) +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK +#pragma code_page(1252) ///////////////////////////////////////////////////////////////////////////// // @@ -532,10 +542,19 @@ END STRINGTABLE BEGIN - IDI_LANG_NAME "English" + IDS_LANG_NAME "English" + IDS_MAIN_ERROR_SETTINGS_INIT_TITLE "Failed to initialize settings" + IDS_MAIN_ERROR_SETTINGS_INIT_TEXT + "Critical error while initializing WinMute" + IDS_MAIN_ERROR_WINMUTE_ALREADY_RUNNING_TITLE "WinMute is already running" + IDS_MAIN_ERROR_WINMUTE_ALREADY_RUNNING_TEXT + "Please look for a white speaker icon in your Windows taskbar notification area." + IDS_MAIN_ERROR_INIT_WINMUTE_TITLE "Failed to start WinMute." + IDS_MAIN_ERROR_INIT_WINMUTE_TEXT + "WinMute encountered a critical error while initializing and must shut down." END -#endif // German (Germany) resources +#endif // English (United Kingdom) resources ///////////////////////////////////////////////////////////////////////////// diff --git a/WinMute/resource.h b/WinMute/resource.h index b6fe63e..5db195a 100644 --- a/WinMute/resource.h +++ b/WinMute/resource.h @@ -4,10 +4,16 @@ // #define IDI_APP 5 #define IDD_ABOUT 9 -#define IDI_LANG_NAME 101 +#define IDS_LANG_NAME 101 +#define IDS_MAIN_ERROR_SETTINGS_INIT_TITLE 102 +#define IDS_MAIN_ERROR_SETTINGS_INIT_TEXT 103 #define IDI_TRAY_BRIGHT 104 +#define IDS_MAIN_ERROR_WINMUTE_ALREADY_RUNNING_TITLE 104 #define IDI_TRAY_DARK 105 +#define IDS_MAIN_ERROR_WINMUTE_ALREADY_RUNNING_TEXT 105 #define IDR_TRAYMENU 106 +#define IDS_MAIN_ERROR_INIT_WINMUTE_TITLE 106 +#define IDS_MAIN_ERROR_INIT_WINMUTE_TEXT 107 #define IDD_SETTINGS 109 #define IDD_SETTINGS_GENERAL 114 #define IDD_SETTINGS_QUIETHOURS 116