Hi, one of the problem we find every so often in KDE-land is that our 
libraries properly extract their translation messages to a .po file but then 
the application using those libraries forget they have to add a line like

KGlobal::locale()->insertCatalog("libkdepim")

Which means the application is unfortunately shown untranslated to the user.

Here comes a patch that tries to fix that by moving the catalog loading 
responsability to the library, that is in my opinion where it should live.

Which this patch, your library would just have somewhere

K_CATALOG_LOADER(libkdepim);

and then all the programs linking to your library would automagically get the 
libkdepim catalog loaded.

It achieves that by creating a static that instructs KGlobal to load a given 
translation catalog on global locale creation.

What do you think about the patch?

Is there any way i can convince you it's safe enough for KDE 4.6 inclusion?

Albert
Index: kernel/kglobal.cpp
===================================================================
--- kernel/kglobal.cpp	(revision 1198789)
+++ kernel/kglobal.cpp	(working copy)
@@ -100,6 +100,7 @@
         KLocale *locale;
         KCharsets *charsets;
         bool localeIsFromFakeComponent;
+        QStringList catalogsToInsert;
 
         /**
          * This component may be used in applications that doesn't have a
@@ -149,6 +150,16 @@
     return d->mainComponent.isValid();
 }
 
+void KGlobal::insertCatalog(const QString& catalog)
+{
+    PRIVATE_DATA;
+    if (d->locale) {
+        d->locale->insertCatalog(catalog);
+    } else {
+        d->catalogsToInsert.append(catalog);
+    }
+}
+
 KLocale *KGlobal::locale()
 {
     PRIVATE_DATA;
@@ -168,6 +179,9 @@
                 QCoreApplication::installTranslator(new KDETranslator(coreApp));
             }
         }
+        foreach(const QString &catalog, d->catalogsToInsert)
+            d->locale->insertCatalog(catalog);
+        d->catalogsToInsert.clear();
     }
     return d->locale;
 }
Index: kernel/kglobal.h
===================================================================
--- kernel/kglobal.h	(revision 1198789)
+++ kernel/kglobal.h	(working copy)
@@ -300,6 +300,31 @@
 } NAME;
 
 /**
+ * This macro is useful in libraries where you want to make sure that
+ * anyone that uses your library will get the correct catalog loaded.
+ *
+ * @param CATALOGNAME The name of your catalog
+ *
+ * Example:
+ * @code
+ * K_CATALOG_LOADER(libkdepim)
+ * @endcode
+ * 
+ * @ingroup KDEMacros
+ */
+#define K_CATALOG_LOADER(CATALOGNAME)                               \
+class KCatalogLoader##CATALOGNAME                                   \
+{                                                                   \
+    public:                                                         \
+        KCatalogLoader##CATALOGNAME();                              \
+};                                                                  \
+static KCatalogLoader##CATALOGNAME catalogLoader##CATALOGNAME;      \
+KCatalogLoader##CATALOGNAME::KCatalogLoader##CATALOGNAME()          \
+{                                                                   \
+    KGlobal::insertCatalog(QLatin1String(#CATALOGNAME));            \
+}
+
+/**
  * Access to the KDE global objects.
  * KGlobal provides you with pointers of many central
  * objects that exist only once in the process. It is also
@@ -338,6 +363,12 @@
     KDECORE_EXPORT KSharedConfigPtr config();
 
     /**
+     * Inserts the catalog in the main locale object if it exists.
+     * Otherwise the catalog name is stored and added once the main locale gets created
+     */
+    KDECORE_EXPORT void insertCatalog(const QString& catalog);
+
+    /**
      * Returns the global locale object.
      * @return the global locale object
      *

Reply via email to