Updated PR to make LIBRARY default to the environment language, rather than just console tidy.

This commit is contained in:
Jim Derry 2017-10-31 17:41:32 -04:00
parent cc023c26b7
commit 57f623eb7c
5 changed files with 66 additions and 9 deletions

View file

@ -2022,11 +2022,6 @@ int main( int argc, char** argv )
/* Set an atexit handler. */ /* Set an atexit handler. */
atexit( tidy_cleanup ); atexit( tidy_cleanup );
/* Set the locale for tidy's output. This both configures LibTidy to
use the environment's locale as well as the standard library.
*/
tidySetLanguage( setlocale( LC_ALL, "") );
#if defined(_WIN32) #if defined(_WIN32)
/* Force Windows console to use UTF, otherwise many characters will /* Force Windows console to use UTF, otherwise many characters will
* be garbage. Note that East Asian languages *are* supported, but * be garbage. Note that East Asian languages *are* supported, but

View file

@ -336,9 +336,10 @@ TIDY_EXPORT Bool TIDY_CALL tidySetPanicCall( TidyPanic fpanic );
*/ */
/** The primary creation of a document instance. Instances of a TidyDoc are used /** The primary creation of a document instance. Instances of a TidyDoc are used
** throughout the API as a token to represent a particular document. When done ** throughout the API as a token to represent a particular document. You must
** using a TidyDoc instance, be sure to `tidyRelease(myTidyDoc);` in order ** create at least one TidyDoc instance to initialize the library and begin
** to free related memory. ** interaction with the API. When done using a TidyDoc instance, be sure to
** `tidyRelease(myTidyDoc);` in order to free related memory.
** @result Returns a TidyDoc instance. ** @result Returns a TidyDoc instance.
*/ */
TIDY_EXPORT TidyDoc TIDY_CALL tidyCreate(void); TIDY_EXPORT TidyDoc TIDY_CALL tidyCreate(void);

View file

@ -24,6 +24,7 @@
* This structure type provides universal access to all of Tidy's strings. * This structure type provides universal access to all of Tidy's strings.
*/ */
typedef struct { typedef struct {
Bool manually_set;
languageDefinition *currentLanguage; languageDefinition *currentLanguage;
languageDefinition *fallbackLanguage; languageDefinition *fallbackLanguage;
languageDefinition *languages[]; languageDefinition *languages[];
@ -35,6 +36,7 @@ typedef struct {
* `.currentLanguage` to language_en, which is Tidy's default language. * `.currentLanguage` to language_en, which is Tidy's default language.
*/ */
static tidyLanguagesType tidyLanguages = { static tidyLanguagesType tidyLanguages = {
no, /* library language was NOT manually set */
&language_en, /* current language */ &language_en, /* current language */
&language_en, /* first fallback language */ &language_en, /* first fallback language */
{ {
@ -451,6 +453,26 @@ ctmbstr TY_(tidyGetLanguage)()
} }
/**
* Indicates whether or not the current language was set by a
* LibTidy user (yes) or internally by the library (no).
*/
Bool TY_(tidyGetLanguageSetByUser)()
{
return tidyLanguages.manually_set;
}
/**
* Specifies to LibTidy that the user (rather than the library)
* selected the current language.
*/
void TY_(tidySetLanguageSetByUser)( void )
{
tidyLanguages.manually_set = yes;
}
/** /**
* Provides a string given `messageType` in the default * Provides a string given `messageType` in the default
* localization (which is `en`), for single plural form. * localization (which is `en`), for single plural form.

View file

@ -115,6 +115,26 @@ Bool TY_(tidySetLanguage)( ctmbstr languageCode );
*/ */
ctmbstr TY_(tidyGetLanguage)(void); ctmbstr TY_(tidyGetLanguage)(void);
/**
* Indicates whether or not the current language was set by a
* LibTidy user or internally by the library. This flag prevents
* subsequently created instances of TidyDocument from changing the
* user's language.
* @returns Returns yes to indicate that the current language was
* specified by an API user.
*/
Bool TY_(tidyGetLanguageSetByUser)(void);
/**
* Specifies to LibTidy that the user (rather than the library)
* selected the current language. This flag prevents subsequently
* created instances of TidyDocument from changing the user's language.
*/
void TY_(tidySetLanguageSetByUser)( void );
/** /**
* Provides a string given `messageType` in the current * Provides a string given `messageType` in the current
* localization for `quantity`. * localization for `quantity`.

View file

@ -34,6 +34,9 @@
#include "mappedio.h" #include "mappedio.h"
#include "language.h" #include "language.h"
#include "sprtf.h" #include "sprtf.h"
#if SUPPORT_LOCALIZATIONS
# include "locale.h"
#endif
/* Create/Destroy a Tidy "document" object */ /* Create/Destroy a Tidy "document" object */
static TidyDocImpl* tidyDocCreate( TidyAllocator *allocator ); static TidyDocImpl* tidyDocCreate( TidyAllocator *allocator );
@ -108,6 +111,17 @@ TidyDocImpl* tidyDocCreate( TidyAllocator *allocator )
TY_(InitConfig)( doc ); TY_(InitConfig)( doc );
TY_(InitPrintBuf)( doc ); TY_(InitPrintBuf)( doc );
/* Set the locale for tidy's output. This both configures
** LibTidy to use the environment's locale as well as the
** standard library.
*/
#if SUPPORT_LOCALIZATIONS
if ( TY_(tidyGetLanguageSetByUser)() == no )
{
TY_(tidySetLanguage)( setlocale( LC_ALL, "") );
}
#endif
/* By default, wire tidy messages to standard error. /* By default, wire tidy messages to standard error.
** Document input will be set by parsing routines. ** Document input will be set by parsing routines.
** Document output will be set by pretty print routines. ** Document output will be set by pretty print routines.
@ -2474,7 +2488,12 @@ uint TIDY_CALL getNextErrorCode( TidyIterator* iter )
Bool TIDY_CALL tidySetLanguage( ctmbstr languageCode ) Bool TIDY_CALL tidySetLanguage( ctmbstr languageCode )
{ {
return TY_(tidySetLanguage)( languageCode ); Bool result = TY_(tidySetLanguage)( languageCode );
if ( result )
TY_(tidySetLanguageSetByUser)();
return result;
} }
ctmbstr TIDY_CALL tidyGetLanguage() ctmbstr TIDY_CALL tidyGetLanguage()