[rkward-cvs] SF.net SVN: rkward-code:[4758] trunk/rkward/rkward/misc

tfry at users.sf.net tfry at users.sf.net
Mon Jun 24 11:50:23 UTC 2013


Revision: 4758
          http://sourceforge.net/p/rkward/code/4758
Author:   tfry
Date:     2013-06-24 11:50:22 +0000 (Mon, 24 Jun 2013)
Log Message:
-----------
Start adding framework for translating plugin messages.

Modified Paths:
--------------
    trunk/rkward/rkward/misc/CMakeLists.txt

Added Paths:
-----------
    trunk/rkward/rkward/misc/rkmessagecatalog.cpp
    trunk/rkward/rkward/misc/rkmessagecatalog.h

Modified: trunk/rkward/rkward/misc/CMakeLists.txt
===================================================================
--- trunk/rkward/rkward/misc/CMakeLists.txt	2013-05-24 19:06:44 UTC (rev 4757)
+++ trunk/rkward/rkward/misc/CMakeLists.txt	2013-06-24 11:50:22 UTC (rev 4758)
@@ -23,6 +23,7 @@
    celleditor.cpp
    editlabelsdialog.cpp
    editformatdialog.cpp
+   rkmessagecatalog.cpp
    )
 
 QT4_AUTOMOC(${misc_STAT_SRCS})

Added: trunk/rkward/rkward/misc/rkmessagecatalog.cpp
===================================================================
--- trunk/rkward/rkward/misc/rkmessagecatalog.cpp	                        (rev 0)
+++ trunk/rkward/rkward/misc/rkmessagecatalog.cpp	2013-06-24 11:50:22 UTC (rev 4758)
@@ -0,0 +1,68 @@
+/***************************************************************************
+                          rkmessagecatalog  -  description
+                             -------------------
+    begin                : Mon Jun 24 2013
+    copyright            : (C) 2013 by Thomas Friedrichsmeier
+    email                : tfry at users.sourceforge.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "rkmessagecatalog.h"
+
+#include <libintl.h>
+#include <QFile>
+
+#include "../debug.h"
+
+RKMessageCatalog::RKMessageCatalog (const QString &name, const QString& path) {
+	RK_TRACE (MISC);
+
+	bound = false;
+	catalog_path = path;
+	catalog_name = QFile::encodeName (name);
+}
+
+RKMessageCatalog::~RKMessageCatalog () {
+	RK_TRACE (MISC);
+}
+
+// Adopted from KDE's gettext.h
+/* The separator between msgctxt and msgid in a .mo file. */
+#define GETTEXT_CONTEXT_GLUE "\004"
+
+QString RKMessageCatalog::translate (const QString &msgctxt, const QString &msgid) const {
+	RK_TRACE (MISC);
+	if (!bound) const_cast<RKMessageCatalog *> (this)->setup ();
+
+	QByteArray key = (msgctxt + GETTEXT_CONTEXT_GLUE + msgid).toUtf8 ();
+	const char *trans = dgettext (catalog_name, key);
+	if (trans == key) return msgid;
+	return QString::fromUtf8 (trans);
+}
+
+QString RKMessageCatalog::translate (const QString &msgid) const {
+	RK_TRACE (MISC);
+	if (!bound) const_cast<RKMessageCatalog *> (this)->setup ();
+
+	return QString::fromUtf8 (dgettext (catalog_name, msgid.toUtf8 ()));
+}
+
+void RKMessageCatalog::setup () {
+	RK_TRACE (MISC);
+
+	setup_mutex.lock ();
+	if (!bound) {
+		bindtextdomain (catalog_name, QFile::encodeName (catalog_path));
+		bind_textdomain_codeset (catalog_name, "UTF-8");
+		bound = true;
+	}
+	setup_mutex.unlock ();
+}

Added: trunk/rkward/rkward/misc/rkmessagecatalog.h
===================================================================
--- trunk/rkward/rkward/misc/rkmessagecatalog.h	                        (rev 0)
+++ trunk/rkward/rkward/misc/rkmessagecatalog.h	2013-06-24 11:50:22 UTC (rev 4758)
@@ -0,0 +1,44 @@
+/***************************************************************************
+                          rkmessagecatalog  -  description
+                             -------------------
+    begin                : Mon Jun 24 2013
+    copyright            : (C) 2013 by Thomas Friedrichsmeier
+    email                : tfry at users.sourceforge.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef RKMESSAGECATALOG_H
+#define RKMESSAGECATALOG_H
+
+#include <QString>
+#include <QMutex>
+
+/** This class - heavily inspired by KCatalog - wraps a gettext message catalog. Contrary to KCatalog, this does not currently support using a language other than
+ * the system language, or switching languages at runtime. It allows (the base directory of) message catalogs to be at an arbitrary location.
+ * 
+ * Also, msgids are passed as QStrings, since in our use cases, that's the source data format, anyway.
+ */
+class RKMessageCatalog {
+public:
+	RKMessageCatalog (const QString &name, const QString &path);
+	~RKMessageCatalog ();
+
+	QString translate (const QString &msgctxt, const QString &msgid) const;
+	QString translate (const QString &msgid) const;
+private:
+	void setup ();
+	QString catalog_path;
+	QByteArray catalog_name;
+	bool bound;
+	QMutex setup_mutex;
+};
+
+#endif





More information about the rkward-tracker mailing list