Files
neovim/src/nvim/os/lang.c
Marco Hinz e038625b87 fix(mac): use same $LANG fallback mechanism as Vim
In a locale "en_US", "en" is the language and "US" is the region.

Before this change, we were too clever for our own good and tried to handle the
region as well. But if the macOS primary language is set to "English" and the
region to "Norway", we would end up with "en_NO", which is a locale that does
not exist.

Now we only take the language into account. Taking the example from above would
yield "en_US", which is a sensible fallback.

If the region is important to you, set $LANG and the more specific LC_*
variables in your shell config or alternatively use `:help :language`.

References https://github.com/neovim/neovim/issues/18292
2022-04-28 23:36:12 +02:00

46 lines
1.4 KiB
C

// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#ifdef __APPLE__
# define Boolean CFBoolean // Avoid conflict with API's Boolean
# define FileInfo CSFileInfo // Avoid conflict with API's Fileinfo
# include <CoreServices/CoreServices.h>
# undef Boolean
# undef FileInfo
#endif
#include "auto/config.h"
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
#include "nvim/os/lang.h"
#include "nvim/os/os.h"
void lang_init(void)
{
#ifdef __APPLE__
if (os_getenv("LANG") == NULL) {
char buf[50] = { 0 };
// $LANG is not set, either because it was unset or Nvim was started
// from the Dock. Query the system locale.
if (LocaleRefGetPartString(NULL,
kLocaleLanguageMask | kLocaleLanguageVariantMask |
kLocaleRegionMask | kLocaleRegionVariantMask,
sizeof(buf) - 10, buf) == noErr && *buf) {
if (strcasestr(buf, "utf-8") == NULL) {
xstrlcat(buf, ".UTF-8", sizeof(buf));
}
os_setenv("LANG", buf, true);
setlocale(LC_ALL, "");
// Make sure strtod() uses a decimal point, not a comma.
setlocale(LC_NUMERIC, "C");
} else {
ELOG("$LANG is empty and the macOS primary language cannot be inferred.");
}
}
#endif
}