[open-ils-commits] r15852 - trunk/Open-ILS/src/c-apps (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Mar 15 21:14:17 EDT 2010


Author: scottmk
Date: 2010-03-15 21:14:16 -0400 (Mon, 15 Mar 2010)
New Revision: 15852

Modified:
   trunk/Open-ILS/src/c-apps/oils_event.c
Log:
Support multiple languages in the lookup for event descriptions.

Capture the current locale, which reflects the message most
recently received, and confine the lookup to messages in that
language.  If you don't find a description, and the language is
different from the default language (hard-coded as "en-US"),
try again with the default language.

Also: eliminate a needless memset().

M    Open-ILS/src/c-apps/oils_event.c


Modified: trunk/Open-ILS/src/c-apps/oils_event.c
===================================================================
--- trunk/Open-ILS/src/c-apps/oils_event.c	2010-03-15 22:49:56 UTC (rev 15851)
+++ trunk/Open-ILS/src/c-apps/oils_event.c	2010-03-16 01:14:16 UTC (rev 15852)
@@ -3,7 +3,10 @@
 #include <libxml/tree.h>
 #include "opensrf/osrf_settings.h"
 
+const char default_lang[] = "en-US";
+
 static void _oilsEventParseEvents();
+static const char* lookup_desc( const char* lang, const char* code );
 
 // The following two osrfHashes are created when we
 // create the first osrfEvent, and are never freed.
@@ -55,7 +58,7 @@
 	if(file)
 		evt->file = strdup(file);
 	else
-		evt->file = NULL;
+		evt->file = strdup( "" );
 
 	evt->line = line;
 	return evt;
@@ -204,19 +207,14 @@
 		return NULL;
 	}
 
-	// Search for the right language
-	char* lang = "en-US"; /* assume this for now */
-	char* desc = NULL;
-	osrfHash* h = osrfHashGet(_oilsEventDescriptions, lang);
-	if(h) {
-		// Within that language, search for the right message
-		osrfLogDebug(OSRF_LOG_MARK, "Loaded event lang hash for %s",lang);
-		desc = osrfHashGet(h, code);
-		osrfLogDebug(OSRF_LOG_MARK, "Found event description %s", desc);
-	}
+	// Look up the text message corresponding the code, preferably in the right language.
+	const char* lang = osrf_message_get_last_locale();
+	const char* desc = lookup_desc( lang, code );
+	if( !desc && strcmp( lang, default_lang ) )    // No luck?
+		desc = lookup_desc( default_lang, code );  // Try the default language
 
-	if(!desc)
-		desc = "";  // Not found?  Message defaults to empty string.
+	if( !desc )
+		desc = "";  // Not found?  Default to an empty string.
 
 	jsonObject* json = jsonNewObject(NULL);
 	jsonObjectSetKey( json, "ilsevent", jsonNewNumberObject(atoi(code)) );
@@ -224,8 +222,7 @@
 	jsonObjectSetKey( json, "desc", jsonNewObject(desc) );
 	jsonObjectSetKey( json, "pid", jsonNewNumberObject(getpid()) );
 
-	char buf[256];
-	memset(buf, '\0', sizeof(buf));
+	char buf[256] = "";
 	snprintf(buf, sizeof(buf), "%s:%d", event->file, event->line);
 	jsonObjectSetKey( json, "stacktrace", jsonNewObject(buf) );
 
@@ -246,6 +243,33 @@
 }
 
 /**
+	@brief Lookup up the descriptive text, in a given language, for a given event code.
+	@param lang The language (a.k.a. locale) of the desired message.
+	@param code The numeric code for the event, as a string.
+	return The corresponding descriptive text if found, or NULL if not.
+
+	The lookup has two stages.  First we look up the language, and then within that
+	language we look up the code.
+*/
+static const char* lookup_desc( const char* lang, const char* code ) {
+	// Search for the right language
+	const char* desc = NULL;
+	osrfHash* lang_hash = osrfHashGet( _oilsEventDescriptions, lang );
+	if( lang_hash ) {
+		// Within that language, search for the right message
+		osrfLogDebug( OSRF_LOG_MARK, "Loaded event lang hash for %s", lang );
+		desc = osrfHashGet( lang_hash, code );
+	}
+
+	if( desc )
+		osrfLogDebug( OSRF_LOG_MARK, "Found event description %s", desc );
+	else
+		osrfLogDebug( OSRF_LOG_MARK, "Event description not found for code %s", code );
+
+	return desc;
+}
+
+/**
 	@brief Parse and load the events file.
 
 	Get the name of the events file from previously loaded settings.  Open it and load



More information about the open-ils-commits mailing list