Skip to content

Commit e2a86e4

Browse files
committed
- Look at the user's previous environment to get a UTF-8 LC_CTYPE locale if possible
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12464 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 2558d0e commit e2a86e4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Compiler/runtime/systemimpl.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,8 @@ void SystemImpl__gettextInit(const char *locale)
17321732
/* We might get sent sv_SE when only sv_SE.utf8 exists, etc */
17331733
char *locale2 = NULL;
17341734
char *locale3 = NULL;
1735+
char *old_ctype = strdup(setlocale(LC_CTYPE, ""));
1736+
int old_ctype_is_utf8 = strcmp(nl_langinfo(CODESET), "UTF-8") == 0;
17351737
asprintf(&locale2, "%s.utf8", locale);
17361738
asprintf(&locale3, "%s.UTF-8", locale);
17371739
int res = *locale == 0 ? setlocale(LC_MESSAGES, "") && setlocale(LC_CTYPE, ""):
@@ -1744,15 +1746,26 @@ void SystemImpl__gettextInit(const char *locale)
17441746
}
17451747
free(locale2);
17461748
free(locale3);
1749+
clocale = setlocale(LC_CTYPE, NULL);
1750+
int have_utf8 = strcmp(nl_langinfo(CODESET), "UTF-8") == 0;
17471751
/* We succesfully forced a new non-system locale; let's clear some variables */
17481752
if (*locale) {
17491753
unsetenv("LANG");
17501754
unsetenv("LANGUAGE");
17511755
}
17521756
/* Try to make sure we force UTF-8; else gettext will fail */
1753-
clocale = setlocale(LC_CTYPE, NULL);
1754-
if (!(strstr(clocale, "UTF-8") || strstr(clocale, "UTF8") || strstr(clocale, "utf-8") || strstr(clocale, "utf8")) && !setlocale(LC_CTYPE, "C.UTF-8"))
1757+
if (have_utf8)
1758+
setlocale(LC_CTYPE, clocale);
1759+
else if (old_ctype_is_utf8)
1760+
setlocale(LC_CTYPE, old_ctype);
1761+
else if (!(strstr(clocale, "UTF-8") || strstr(clocale, "UTF8") ||
1762+
strstr(clocale, "utf-8") || strstr(clocale, "utf8")) &&
1763+
!(setlocale(LC_CTYPE, "C.UTF-8") ||
1764+
setlocale(LC_CTYPE, ".UTF-8") ||
1765+
setlocale(LC_CTYPE, "UTF-8"))) {
17551766
fprintf(stderr, gettext("Warning: Failed to set LC_CTYPE to UTF-8 using the chosen locale and C.UTF-8. OpenModelica assumes all input and output it makes is in UTF-8 so you might have some issues.\n"));
1767+
}
1768+
free(old_ctype);
17561769
#endif /* __MINGW32__ */
17571770
omlen = strlen(omhome);
17581771
localedir = (char*) malloc(omlen + 25);

Compiler/runtime/systemimpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef int (*function_t)(type_description*, type_description*);
4343
#else
4444
#include <locale.h>
4545
#include <libintl.h>
46+
#include <langinfo.h>
4647
#endif
4748

4849
#if defined(__MINGW32__) || defined(_MSC_VER)

0 commit comments

Comments
 (0)