From cc801880a5be8902af715ff1722981a4bc35bf6d Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Sat, 20 Dec 2014 01:57:04 -0600 Subject: [PATCH] wmitime: Improve locale support. Previously, wmitime only had support for English, French, and (in Debian only) Hungarian. In addition, the choice was made at compile time. This patch adds run-time support for any language using the Latin alphabet. The locale is determined by the user's environment or may be specified on the command line with the "-l" option. Note that users whose environment specifies a non-Latin locale may wish to use, e.g., "-l C", as otherwise no date will appear. Note that, for simplicity, the month and day are now displayed as "01 JAN" as opposed to "JAN 01". (Previously, the former format was used for English and the latter for French.) --- wmitime/Makefile | 3 +-- wmitime/french.h | 30 --------------------- wmitime/language.h | 35 ------------------------- wmitime/wmitime.c | 65 +++++++++++++++++++++++++++++----------------- 4 files changed, 42 insertions(+), 91 deletions(-) delete mode 100644 wmitime/french.h delete mode 100644 wmitime/language.h diff --git a/wmitime/Makefile b/wmitime/Makefile index c928067..e9029c0 100644 --- a/wmitime/Makefile +++ b/wmitime/Makefile @@ -1,4 +1,3 @@ -#LANG = fr LIBS = -lXpm -lXext -lX11 -lm CFLAGS = -O2 -Wall OBJS = wmitime.o \ @@ -10,7 +9,7 @@ PREFIX = /usr/local BINDIR = $(PREFIX)/bin .c.o: - $(CC) $(CPPFLAGS) $(CFLAGS) -D$(LANG) -c $< -o $*.o + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $*.o wmitime: $(OBJS) $(CC) $(LDFLAGS) -o wmitime $^ $(LIBS) diff --git a/wmitime/french.h b/wmitime/french.h deleted file mode 100644 index 17d86f0..0000000 --- a/wmitime/french.h +++ /dev/null @@ -1,30 +0,0 @@ -// french.h -// Created by Pierre-Marie.Allemand@capway.com -// 08-jan-1999 - -static char daynames[7][3] = -{ - {"Di"}, - {"Lu"}, - {"Ma"}, - {"Me"}, - {"Je"}, - {"Ve"}, - {"Sa"} -}; - -static char monthnames[12][4] = -{ - {"Jan"}, - {"Fev"}, - {"Mar"}, - {"Avr"}, - {"Mai"}, - {"Jun"}, - {"Jui"}, - {"Aou"}, - {"Sep"}, - {"Oct"}, - {"Nov"}, - {"Dec"} -}; diff --git a/wmitime/language.h b/wmitime/language.h deleted file mode 100644 index 88b67a1..0000000 --- a/wmitime/language.h +++ /dev/null @@ -1,35 +0,0 @@ -// language.h - -static char daynames[7][3] = -{ - {"Su"}, - {"Mo"}, - {"Tu"}, - {"We"}, - {"Th"}, - {"Fr"}, - {"Sa"} -}; - -static char monthnames[12][4] = -{ - {"Jan"}, - {"Feb"}, - {"Mar"}, - {"Apr"}, - {"May"}, - {"Jun"}, - {"Jul"}, - {"Aug"}, - {"Sep"}, - {"Oct"}, - {"Nov"}, - {"Dec"} -}; - - - - - - - diff --git a/wmitime/wmitime.c b/wmitime/wmitime.c index e72d16a..c29deaa 100644 --- a/wmitime/wmitime.c +++ b/wmitime/wmitime.c @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include #include #include @@ -31,16 +34,6 @@ #include "wmgeneral/wmgeneral.h" #include "wmgeneral/misc.h" -#ifdef fr_FR -#define fr -#endif - -#ifdef fr -#include "french.h" -#else -#include "language.h" -#endif - #include "wmitime-master.xpm" char wmitime_mask_bits[64*64]; int wmitime_mask_width = 64; @@ -61,6 +54,7 @@ extern char **environ; char *ProgName; char uconfig_file[256]; +char locale[256]; time_t curtime; time_t prevtime; @@ -95,6 +89,7 @@ int main(int argc, char *argv[]) { int i; uconfig_file[0] = 0; + locale[0] = 0; /* Parse Command Line */ @@ -137,6 +132,13 @@ int main(int argc, char *argv[]) { TwelveHour = 1; } break; + case 'l' : + if (argc > (i+1)) + { + strcpy(locale, argv[i+1]); + i++; + } + break; default: usage(); exit(0); @@ -145,6 +147,11 @@ int main(int argc, char *argv[]) { } } + if (setlocale(LC_ALL, locale) == NULL) + fprintf(stderr, + "warning: locale '%s' not recognized; defaulting to '%s'.", + locale, setlocale(LC_ALL, NULL)); + wmitime_routine(argc, argv); return 0; @@ -365,28 +372,37 @@ void DrawStdTime(void) void DrawDate(void) { - char BlitStr[20]; + char OrigBlitStr[20], BlitStr[20]; + char *inbuf, *outbuf; + size_t inbytesleft, outbytesleft; + iconv_t cd; - sprintf(BlitStr, "%s", daynames[clk->tm_wday]); + cd = iconv_open("ASCII//TRANSLIT", nl_langinfo(CODESET)); + + inbuf = OrigBlitStr; + outbuf = BlitStr; + inbytesleft = sizeof OrigBlitStr; + outbytesleft = sizeof BlitStr; + + sprintf(OrigBlitStr, "%s", nl_langinfo(ABDAY_1 + clk->tm_wday)); + iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + BlitStr[2] = 0; BlitString( BlitStr, 6, 50); -#ifdef fr + inbuf = OrigBlitStr; + outbuf = BlitStr; + inbytesleft = sizeof OrigBlitStr; + outbytesleft = sizeof BlitStr; - // French date model - sprintf(BlitStr, "%s", monthnames[clk->tm_mon]); + sprintf(OrigBlitStr, "%s", nl_langinfo(ABMON_1 + clk->tm_mon)); + iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + BlitStr[3] = 0; BlitString( BlitStr, 40, 50); - sprintf(BlitStr, "%02i", clk->tm_mday); - BlitString( BlitStr, 25, 50); -#else - - sprintf(BlitStr, "%s", monthnames[clk->tm_mon]); - BlitString( BlitStr, 25, 50); + iconv_close(cd); sprintf(BlitStr, "%02i", clk->tm_mday); - BlitString( BlitStr, 45, 50); - -#endif + BlitString( BlitStr, 25, 50); } void DrawInetWheel(void) @@ -706,6 +722,7 @@ void usage(void) fprintf(stderr, " -display \n"); fprintf(stderr, " -geometry +XPOS+YPOS initial window position\n"); // fprintf(stderr, " -c use specified config file\n"); + fprintf(stderr, " -l specify locale\n"); fprintf(stderr, " -h this help screen\n"); fprintf(stderr, " -v print the version number\n"); fprintf(stderr, "\n");