summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerd Möllmann <gerd@gnu.org>2023-10-05 20:41:54 +0200
committerGerd Möllmann <gerd@gnu.org>2023-10-05 20:41:54 +0200
commitaad8b5d78f306ac9ca0c03734524c9f49585bee8 (patch)
treecf788f7b7c68103a5b006e2d5a4e26503a25b2ae
parent6cf7e676e9d4846a72d48f21168e92e4efcbf95a (diff)
downloademacs-aad8b5d78f306ac9ca0c03734524c9f49585bee8.tar.gz
Handle LANG on macOS differently (bug#65908)
* src/nsterm.m (ns_init_locale): If LANG is set, try to use that, otherwise try to deduce what LANG should be. Check is the result is valid, and use LANG="en_US.UTF-8" if not.
-rw-r--r--src/nsterm.m39
1 files changed, 21 insertions, 18 deletions
diff --git a/src/nsterm.m b/src/nsterm.m
index 7c9fd102a7f..f8aac065b72 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -554,29 +554,32 @@ ns_init_locale (void)
/* macOS doesn't set any environment variables for the locale when run
from the GUI. Get the locale from the OS and set LANG. */
{
- NSLocale *locale = [NSLocale currentLocale];
-
NSTRACE ("ns_init_locale");
- /* If we were run from a terminal then assume an unset LANG variable
- is intentional and don't try to "fix" it. */
- if (!isatty (STDIN_FILENO))
+ /* Either use LANG, if set, or try to construct LANG from
+ NSLocale. */
+ const char *lang = getenv ("LANG");
+ if (lang == NULL || *lang == 0)
{
- char *oldLocale = setlocale (LC_ALL, NULL);
- /* It seems macOS should probably use UTF-8 everywhere.
- 'localeIdentifier' does not specify the encoding, and I can't
- find any way to get the OS to tell us which encoding to use,
- so hard-code '.UTF-8'. */
- NSString *localeID = [NSString stringWithFormat:@"%@.UTF-8",
- [locale localeIdentifier]];
-
- /* Check the locale ID is valid and if so set LANG, but not if
- it is already set. */
- if (setlocale (LC_ALL, [localeID UTF8String]))
- setenv("LANG", [localeID UTF8String], 0);
+ const NSLocale *locale = [NSLocale currentLocale];
+ const NSString *localeID = [NSString stringWithFormat:@"%@.UTF-8",
+ [locale localeIdentifier]];
+ lang = [localeID UTF8String];
+ }
- setlocale (LC_ALL, oldLocale);
+ /* Check if LANG can be used for initializing the locale. If not,
+ use a default setting. Note that Emacs' main will undo the
+ setlocale below, initializing the locale from the
+ environment. */
+ if (setlocale (LC_ALL, lang) == NULL)
+ {
+ const char *const default_lang = "en_US.UTF-8";
+ fprintf (stderr, "LANG=%s cannot be used, using %s instead.\n",
+ lang, default_lang);
+ lang = default_lang;
}
+
+ setenv ("LANG", lang, 1);
}