soc.2009.transport: 24f1569e: Added my own localization class based on...

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Wed Jun 3 03:30:28 EDT 2009


-----------------------------------------------------------------
Revision: 24f1569e66cc7f9a925a5be641f5778ebf1e109f
Ancestor: 22c6d8c176c85b7fb393feae0f2746458f1ef0da
Author: hanzz at soc.pidgin.im
Date: 2009-06-03T07:27:04
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/24f1569e66cc7f9a925a5be641f5778ebf1e109f

Added files:
        localization.cpp localization.h
Added directories:
        locales
Modified files:
        CMakeLists.txt adhochandler.cpp main.cpp main.h

ChangeLog: 

Added my own localization class based on libgettextpo to allow translating transport messages according to xml_lang in future.

-------------- next part --------------
============================================================
--- localization.cpp	7bc66473c80a1c8cfc22ce91702914a5fc4998f8
+++ localization.cpp	7bc66473c80a1c8cfc22ce91702914a5fc4998f8
@@ -0,0 +1,92 @@
+/**
+ * XMPP - libpurple transport
+ *
+ * Copyright (C) 2009, Jan Kaluza <hanzz at soc.pidgin.im>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ */
+
+#include "localization.h"
+#include "log.h"
+
+Localization::Localization() {
+	m_locales = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy);
+	loadLocale("cs");
+}
+
+Localization::~Localization() {
+	g_hash_table_destroy(m_locales);
+}
+
+const char * Localization::translate(const char *lang, const char *key) {
+	const char *ret = key;
+	GHashTable *l;
+	if ((l = (GHashTable*) g_hash_table_lookup(m_locales, lang))) {
+		char *col = g_utf8_collate_key(key, -1);
+		if (!(ret = (const char *) g_hash_table_lookup(l, col))) {
+			ret = key;
+		}
+		g_free(col);
+	}
+	return ret;
+}
+
+bool Localization::loadLocale(const std::string &lang) {
+	po_file_t pofile = NULL;
+	po_xerror_handler_t error_handle;
+	const char * const *domains;
+	const char * const *domainp;
+	const char *msgstr = NULL;
+	const char *msgid = NULL;
+
+	// Already loaded
+	if (g_hash_table_lookup(m_locales, lang.c_str()))
+		return true;
+
+	pofile = po_file_read (std::string("locales/" + lang + ".po").c_str(), error_handle);
+	if (pofile != NULL) {
+		GHashTable *locale = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+		Log().Get("Localization") << lang  << " locale found";
+		domains = po_file_domains (pofile);
+		for (domainp = domains; *domainp; domainp++) {
+			const char *domain = *domainp;
+			po_message_t message;
+			po_message_iterator_t iterator = po_message_iterator (pofile,
+									      domain);
+			message = po_next_message (iterator);
+			for (;message;) {
+				gchar *key;
+				gchar *value;
+				msgstr = po_message_msgstr (message);
+				msgid = po_message_msgid (message);
+				if (msgstr && msgid) {
+					if (g_utf8_collate (msgid, "") != 0) {
+						key = g_utf8_collate_key (msgid, -1);
+						value = g_strdup (msgstr);
+						g_hash_table_replace(locale, key, value);
+					}
+				}
+				message = po_next_message (iterator);
+			}
+			po_message_iterator_free (iterator);
+		}
+		g_hash_table_replace(m_locales, g_strdup("cs"), locale);
+		po_file_free(pofile);
+		return true;
+	}
+	return false;
+}
+
+Localization localization;
============================================================
--- localization.h	094f6506cc163319c482410e1874c87c80156435
+++ localization.h	094f6506cc163319c482410e1874c87c80156435
@@ -0,0 +1,40 @@
+/**
+ * XMPP - libpurple transport
+ *
+ * Copyright (C) 2009, Jan Kaluza <hanzz at soc.pidgin.im>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ */
+#ifndef _HI_LOCALIZATION_HANDLER_H
+#define _HI_LOCALIZATION_HANDLER_H
+
+#include <string>
+#include <map>
+#include "glib.h"
+#include <gettext-po.h>
+
+class Localization {
+	public:
+		Localization();
+		~Localization();
+		
+		bool loadLocale(const std::string &lang);
+		const char * translate(const char *lang, const char *key);
+		
+	private:
+		GHashTable *m_locales;		// xml_lang, hash table with localizations
+};
+
+#endif
============================================================
--- CMakeLists.txt	b7242181433456cef8cd61b9e0546f2f59b058fe
+++ CMakeLists.txt	81807ec3cb46f2ab7c1fff0da6cefbbf171e5d68
@@ -25,6 +25,14 @@
 set(glib_DIR ${CMAKE_MODULE_PATH})
 find_package(glib REQUIRED)
 
+FIND_LIBRARY(GETTEXTPO_LIBRARY NAMES gettextpo)
+
+if(GETTEXTPO_LIBRARY)
+	message(STATUS "libgettextpo found OK")
+else(GETTEXTPO_LIBRARY)
+	message(FATAL_ERROR "no libgettextpo")
+endif(GETTEXTPO_LIBRARY)
+
 if(GLOOX_FOUND)
 	message(STATUS "gloox found OK")
 	include_directories(${GLOOX_INCLUDE_DIRS})
@@ -80,6 +88,7 @@
 	usermanager.cpp
 	adhochandler.cpp
 	adhocrepeater.cpp
+	localization.cpp
 	protocols/icq.cpp
 	protocols/facebook.cpp
 	protocols/gg.cpp
@@ -107,6 +116,7 @@
 	usermanager.h
 	adhocrepeater.h
 	adhochandler.h
+	localization.h
 	protocols/abstractprotocol.h
 	protocols/icq.h
 	protocols/facebook.h
@@ -117,7 +127,7 @@
 
 FIND_LIBRARY(MYSQL_LIBRARY NAMES mysqlpp)
 
-target_link_libraries(hiicq ${GLOOX_LIBRARIES} ${MYSQL_LIBRARY} ${PURPLE_LIBRARIES} ${GLIB2_LIBRARIES} -export-dynamic)
+target_link_libraries(hiicq ${GLOOX_LIBRARIES} ${MYSQL_LIBRARY} ${PURPLE_LIBRARIES} ${GLIB2_LIBRARIES} ${GETTEXTPO_LIBRARY} -export-dynamic)
 
 # install(FILES
 # 	${CMAKE_CURRENT_SOURCE_DIR}/data/LICENSE.txt
============================================================
--- adhochandler.cpp	7725c11e07e7667ba31c5e5697aac329ee0ae351
+++ adhochandler.cpp	e1e945c79db049389c3cb10be8acdb65e73da830
@@ -152,7 +152,7 @@ bool GlooxAdhocHandler::handleIq( const 
 				GList *l, *ll;
 				PurpleConnection *gc = purple_account_get_connection(user->account());
 				PurplePlugin *plugin = gc && PURPLE_CONNECTION_IS_CONNECTED(gc) ? gc->prpl : NULL;
-				PurplePluginProtocoilInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(plugin);
+				PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(plugin);
 
 				if(!prpl_info || !prpl_info->blist_node_menu)
 					return true;
============================================================
--- main.cpp	fd8864b76aedcde227d20db610dacc105899a9c8
+++ main.cpp	f26e479595dc50fd26fe8e5b3e55b4a52014acb2
@@ -163,7 +163,7 @@ static void * requestInput(const char *t
 		else {
 			if (primaryString=="Authorization Request Message:") {
 				Log().Get("purple") << "accepting this authorization request";
-				((PurpleRequestInputCb) ok_cb)(user_data,_("Please authorize me."));
+				((PurpleRequestInputCb) ok_cb)(user_data,"Please authorize me.");
 			}
 			else if ( primaryString == "Set your Facebook status" ) {
 				Log().Get("purple") << "set facebook status";
@@ -1166,8 +1166,5 @@ int main( int argc, char* argv[] ) {
 GlooxMessageHandler* GlooxMessageHandler::m_pInstance = NULL;
 int main( int argc, char* argv[] ) {
 	GlooxMessageHandler t;
-	setlocale( LC_ALL, "" );
-	bindtextdomain( "jabbimtransport", "/usr/share/locale" );
-	textdomain( "jabbimtransport" );
 }
 
============================================================
--- main.h	36b3fe9647dd758c3df775c11dd0cdb18d8a4a62
+++ main.h	f097dda7f7e779a43fadfa4ddf96d78a1053f4dc
@@ -61,7 +61,7 @@
 #include <locale.h>
 
 #define HIICQ_UI "hiicq"
-#define _(STRING)    gettext(STRING)
+#define _(lang,STRING)    localization.translate(lang,STRING)
 
 #include "registerhandler.h"
 #include "discoinfohandler.h"
@@ -75,10 +75,13 @@
 #include "filetransfermanager.h"
 #include <sys/stat.h>
 #include <sys/types.h>
+#include "localization.h"
 
 
 using namespace gloox;
 
+extern Localization localization;
+
 class GlooxDiscoHandler;
 class GlooxDiscoInfoHandler;
 class GlooxRegisterHandler;


More information about the Commits mailing list