macOS: Set LANG to en_US if cocoa reports language as en_* that is not a valid locale
Fixes #3899
This commit is contained in:
@@ -574,12 +574,7 @@ cocoa_get_lang(PyObject UNUSED *self) {
|
||||
locale = [[NSLocale currentLocale] localeIdentifier];
|
||||
}
|
||||
if (!locale) { Py_RETURN_NONE; }
|
||||
// Make sure the locale value is valid, that is it can be used
|
||||
// to construct an actual locale
|
||||
const char* locale_utf8 = [locale UTF8String];
|
||||
locale_t test_locale = newlocale(LC_ALL_MASK, locale_utf8, NULL);
|
||||
if (!test_locale) { Py_RETURN_NONE; }
|
||||
freelocale(test_locale);
|
||||
return Py_BuildValue("s", locale_utf8);
|
||||
|
||||
} // autoreleasepool
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <signal.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#ifdef WITH_PROFILER
|
||||
#include <gperftools/profiler.h>
|
||||
#endif
|
||||
@@ -156,6 +157,15 @@ wcwidth_wrap(PyObject UNUSED *self, PyObject *chr) {
|
||||
return PyLong_FromLong(wcwidth_std(PyLong_AsLong(chr)));
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
locale_is_valid(PyObject *self UNUSED, PyObject *args) {
|
||||
char *name;
|
||||
if (!PyArg_ParseTuple(args, "s", &name)) return NULL;
|
||||
locale_t test_locale = newlocale(LC_ALL_MASK, name, NULL);
|
||||
if (!test_locale) { Py_RETURN_FALSE; }
|
||||
freelocale(test_locale);
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
{"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""},
|
||||
@@ -169,6 +179,7 @@ static PyMethodDef module_methods[] = {
|
||||
{"parse_bytes", (PyCFunction)parse_bytes, METH_VARARGS, ""},
|
||||
{"parse_bytes_dump", (PyCFunction)parse_bytes_dump, METH_VARARGS, ""},
|
||||
{"redirect_std_streams", (PyCFunction)redirect_std_streams, METH_VARARGS, ""},
|
||||
{"locale_is_valid", (PyCFunction)locale_is_valid, METH_VARARGS, ""},
|
||||
#ifdef __APPLE__
|
||||
METHODB(user_cache_dir, METH_NOARGS),
|
||||
METHODB(process_group_map, METH_NOARGS),
|
||||
|
||||
@@ -672,6 +672,10 @@ def cocoa_get_lang() -> Optional[str]:
|
||||
pass
|
||||
|
||||
|
||||
def locale_is_valid(name: str) -> bool:
|
||||
pass
|
||||
|
||||
|
||||
def mark_os_window_for_close(os_window_id: int, cr_type: int = 2) -> bool:
|
||||
pass
|
||||
|
||||
|
||||
+6
-1
@@ -195,10 +195,15 @@ run_app = AppRunner()
|
||||
def ensure_macos_locale() -> None:
|
||||
# Ensure the LANG env var is set. See
|
||||
# https://github.com/kovidgoyal/kitty/issues/90
|
||||
from .fast_data_types import cocoa_get_lang
|
||||
from .fast_data_types import cocoa_get_lang, locale_is_valid
|
||||
if 'LANG' not in os.environ:
|
||||
lang = cocoa_get_lang()
|
||||
if lang is not None:
|
||||
if not locale_is_valid(lang):
|
||||
if lang.startswith('en_'):
|
||||
lang = 'en_US'
|
||||
else:
|
||||
log_error(f'Could not set LANG Cocoa returns language as: {lang}')
|
||||
os.environ['LANG'] = lang + '.UTF-8'
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user