From 88b99acdd68b7e70a4bc19c0f616dbff5e5e93a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=8D=9A=E4=BB=81=28Buo-ren=20Lin=29?= Date: Mon, 3 Dec 2018 22:13:22 +0800 Subject: [PATCH] Is. #783 - Fix language detection The `setlocale` call doesn't return a single locale name in glibc when any of the locale category variable has a different value, instead it returns a composite locale name which is a concatenation of the entire list of locale name and its values, causing the language detection fail. This patch attempts to set the language via LC_MESSAGES and LANG environment variables which are commonly used in POSIX-like systems, then fallbacks to `setlocale` as the last resort. --- src/tidylib.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/tidylib.c b/src/tidylib.c index 31754ab..73e9f4e 100644 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -36,6 +36,7 @@ #include "attrs.h" #include "sprtf.h" #if SUPPORT_LOCALIZATIONS +# include "stdlib.h" # include "locale.h" #endif @@ -119,7 +120,13 @@ TidyDocImpl* tidyDocCreate( TidyAllocator *allocator ) #if SUPPORT_LOCALIZATIONS if ( TY_(tidyGetLanguageSetByUser)() == no ) { - TY_(tidySetLanguage)( setlocale( LC_ALL, "") ); + if( ! TY_(tidySetLanguage)( getenv( "LC_MESSAGES" ) ) ) + { + if( ! TY_(tidySetLanguage)( getenv( "LANG" ) ) ) + { + TY_(tidySetLanguage)( setlocale( LC_ALL, "" ) ); + } + } } #endif