@@ -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 );
0 commit comments