summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-04-08 12:57:12 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-04-08 16:29:46 +0200
commit27902c01cc74d5d50378aa31eb6c59730d9287ab (patch)
treedd57813dd8c504b4e518a46733ad8a88d6a76038 /src/main.cpp
parentd8acd00dff8b89d5a01cf3a24c58b1c7ea5ee99d (diff)
downloadmana-27902c01cc74d5d50378aa31eb6c59730d9287ab.tar.gz
mana-27902c01cc74d5d50378aa31eb6c59730d9287ab.tar.bz2
mana-27902c01cc74d5d50378aa31eb6c59730d9287ab.tar.xz
mana-27902c01cc74d5d50378aa31eb6c59730d9287ab.zip
Fixed choosing preferred language on Windows
The linker could not find _nl_locale_name_default. Maybe it could be fixed by linking to another library, but it would likely not yield the preferred behavior. Instead, we now use GetUserPreferredUILanguages to set the LANG environment variable with one or more languages and let gettext choose the best fitting available translation. Also fixed the directory from which the translations are loaded to match the installation directory,
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp43
1 files changed, 32 insertions, 11 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 7b9e921e..68a6b0c3 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -33,6 +33,9 @@
#ifdef __MINGW32__
#include <windows.h>
#endif
+#if ENABLE_NLS && defined(_WIN32)
+#include <winnls.h>
+#endif
static void printHelp()
{
@@ -178,24 +181,42 @@ static void parseOptions(int argc, char *argv[], Client::Options &options)
}
}
-#ifdef _WIN32
-//extern "C" char const *_nl_locale_name_default(void);
-#endif
-
static void initInternationalization()
{
#if ENABLE_NLS
#ifdef _WIN32
-// SetEnvironmentVariable("LANG", _nl_locale_name_default());
- // mingw doesn't like LOCALEDIR to be defined for some reason
- bindtextdomain("mana", "translations/");
-#else
- bindtextdomain("mana", LOCALEDIR);
-#endif
+ // On Windows we need to set the LANG environment variable to get the
+ // correct translation, because this isn't set by default.
+ ULONG numLanguages = 0;
+ ULONG bufferSize = 0;
+ if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, nullptr, &bufferSize))
+ return;
+ if (numLanguages == 0 || bufferSize < 2)
+ return;
+
+ std::wstring localeNamesW(bufferSize, L'\0');
+ if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, localeNamesW.data(), &bufferSize))
+ return;
+
+ // Replace the null characters used as separators with a colon, except for
+ // the last two. Also replace - with _, since gettext expects de_DE rather
+ // than de-DE.
+ for (size_t i = 0; i < localeNamesW.size() - 2; ++i) {
+ auto &c = localeNamesW[i];
+ if (c == L'\0')
+ c = L':';
+ else if (c == L'-')
+ c = L'_';
+ }
+
+ _wputenv_s(L"LANG", localeNamesW.c_str());
+#endif // _WIN32
+
setlocale(LC_MESSAGES, "");
+ bindtextdomain("mana", LOCALEDIR);
bind_textdomain_codeset("mana", "UTF-8");
textdomain("mana");
-#endif
+#endif // ENABLE_NLS
}