]> repos.lx-s.de Git - winmute.git/commitdiff
#26 Save and load languages
authorlx-s <lx-s@users.noreply.github.com>
Fri, 3 Nov 2023 08:16:10 +0000 (09:16 +0100)
committerlx-s <lx-s@users.noreply.github.com>
Fri, 3 Nov 2023 08:16:10 +0000 (09:16 +0100)
.gitignore
WinMute/Settings_GeneralDlg.cpp
WinMute/WMi18n.cpp
WinMute/WMi18n.h
WinMute/WinMain.cpp
WinMute/WinMute.rc
WinMute/resource.h

index 18615fe7e41b49d935d649e7d85418f71bbb2814..6677d5121fb4be5ce9b70c79522a1e048ccda7b3 100644 (file)
@@ -11,6 +11,7 @@ Debug/
 *.exp
 *.pdb
 *.lib
+*.ods#
 
 *.dll
 *.exe
index 09e2d7dbbde63a8ee687b4cf58c1166f796e6f25..dff661251db546ef4e2568d8e851b2a020090d76 100644 (file)
@@ -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<const wchar_t *>(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);
             }
          }
       }
index 59acd2da5c14d6f94ad115173ae1bcea8eae77a7..ce46411c6a8423aef6af52430ba5017f4a87a484 100644 (file)
@@ -88,7 +88,7 @@ std::vector<LanguageModule> 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;
index 962249d2578e70b68f0856eac48bf593e8afc1a6..5c284584dc6925483af3980fc67d1e6877da7fb6 100644 (file)
@@ -49,7 +49,7 @@ public:
 
    std::optional<fs::path> GetLanguageModulesPath() const;
    std::wstring GetCurrentLanguageModule() const;
-   std::wstring GetcurrentLanguageName() const;
+   std::wstring GetCurrentLanguageName() const;
 
    std::wstring GetString(UINT id) const;
 
index 80615e8c60636a200781ee3eec8f6c5854188219..c83d0f2393867ba9968dee2f7056c4880917cdf5 100644 (file)
@@ -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);
index e100965cfff32b91534ab68c997d971a7fafabf1..2da7905b32863048267681fe29490da0b30d49d6 100644 (file)
@@ -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
 /////////////////////////////////////////////////////////////////////////////
 
 
index b6fe63eefde4bdcbd1f9accd707fba5738c4bdb1..5db195ab069c3bd66adfe68882835333e7d53eb8 100644 (file)
@@ -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