Hello,

Recently I have been working on migrating totem[0] from Intltool to a
modern Gettext version[1], but I have faced a problem with libpeas
plugin files[2], which are a Desktop Entry variant[3].

While there are different workarounds, libpeas is the default library
choosen for plugins regarding GNOME applications, so I've decided to
solve this problem properly and I've working on its support on
gettext.

At the moment, I have only added support for extracting text and I
would like to know if my approach is the best way to do it while I
continue working on it.

You can find attach a patch with those changes. Any suggestion is welcome.

Best regards,

[0] https://wiki.gnome.org/Apps/Videos
[1] https://bugzilla.gnome.org/show_bug.cgi?id=783316
[2] https://bugzilla.gnome.org/show_bug.cgi?id=783316#c10
[3] https://git.gnome.org/browse/libpeas/tree/libpeas/peas-plugin-info.c
From b7da323d4f68c8f85760f6d5c0205a457014f742 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?I=C3=B1igo=20Mart=C3=ADnez?= <inigomarti...@gmail.com>
Date: Mon, 5 Jun 2017 13:16:07 +0200
Subject: [PATCH] Added support for libpeas Plugin file

* gettext-tools/src/x-desktop.c: Added an enum to choose the default set
of keywords.
* gettext-tools/src/x-desktop.h: Added a new extension which is used for
plugin files.
* tests/xgettext-plugin-1: A new test for Plugin files based on Desktop
test.
---
 gettext-tools/src/x-desktop.c         | 59 +++++++++++++++++----
 gettext-tools/src/x-desktop.h         |  8 +++
 gettext-tools/tests/xgettext-plugin-1 | 96 +++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+), 9 deletions(-)
 create mode 100755 gettext-tools/tests/xgettext-plugin-1

diff --git a/gettext-tools/src/x-desktop.c b/gettext-tools/src/x-desktop.c
index 3f382ff50..ba829928e 100644
--- a/gettext-tools/src/x-desktop.c
+++ b/gettext-tools/src/x-desktop.c
@@ -54,11 +54,21 @@
 
    The type of a value is determined by looking at the key associated
    with it.  The list of available keys are listed on:
-   http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s05.html  */
+   http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s05.html
+
+   The syntax of libpeas Plugin file is a Desktop Entry file with a custom
+   set of keys. It is defined at
+   https://git.gnome.org/browse/libpeas/tree/libpeas/peas-plugin-info.c */
 
 static hash_table keywords;
 static bool default_keywords = true;
 
+enum desktop_type
+{
+  desktop_std,    /* standard Desktop Entry file */
+  desktop_plugin  /* Plugin file */
+};
+
 static void
 add_keyword (const char *name, hash_table *keywords, bool is_list)
 {
@@ -80,14 +90,24 @@ x_desktop_keyword (const char *name)
 }
 
 static void
-init_keywords (void)
+init_keywords (enum desktop_type type)
 {
   if (default_keywords)
     {
       if (keywords.table == NULL)
         hash_init (&keywords, 100);
 
-      desktop_add_default_keywords (&keywords);
+      switch (type)
+        {
+        case (desktop_plugin):
+          desktop_add_keyword (&keywords, "Name", false);
+          desktop_add_keyword (&keywords, "Description", false);
+          break;
+        case (desktop_std):
+        default:
+          desktop_add_default_keywords (&keywords);
+          break;
+        }
       default_keywords = false;
     }
 }
@@ -171,17 +191,16 @@ desktop_reader_class_ty extract_methods =
     extract_desktop_handle_blank
   };
 
-void
-extract_desktop (FILE *f,
-                 const char *real_filename, const char *logical_filename,
-                 flag_context_list_table_ty *flag_table,
-                 msgdomain_list_ty *mdlp)
+static void
+extract (FILE *f,
+         const char *real_filename, const char *logical_filename,
+         flag_context_list_table_ty *flag_table,
+         msgdomain_list_ty *mdlp)
 {
   desktop_reader_ty *reader = desktop_reader_alloc (&extract_methods);
   extract_desktop_reader_ty *extract_reader =
     (extract_desktop_reader_ty *) reader;
 
-  init_keywords ();
   xgettext_current_source_encoding = po_charset_utf8;
 
   extract_reader->mlp = mdlp->item[0]->messages;
@@ -191,3 +210,25 @@ extract_desktop (FILE *f,
 
   reader = NULL;
 }
+
+void
+extract_desktop (FILE *f,
+                 const char *real_filename, const char *logical_filename,
+                 flag_context_list_table_ty *flag_table,
+                 msgdomain_list_ty *mdlp)
+{
+  init_keywords (desktop_std);
+
+  extract (f, real_filename, logical_filename, flag_table, mdlp);
+}
+
+void
+extract_plugin (FILE *f,
+                const char *real_filename, const char *logical_filename,
+                flag_context_list_table_ty *flag_table,
+                msgdomain_list_ty *mdlp)
+{
+  init_keywords (desktop_plugin);
+
+  extract (f, real_filename, logical_filename, flag_table, mdlp);
+}
diff --git a/gettext-tools/src/x-desktop.h b/gettext-tools/src/x-desktop.h
index 4a820e5dd..68bebe836 100644
--- a/gettext-tools/src/x-desktop.h
+++ b/gettext-tools/src/x-desktop.h
@@ -29,9 +29,11 @@ extern "C" {
 
 #define EXTENSIONS_DESKTOP \
   { "desktop", "Desktop" }, \
+  { "plugin", "Plugin" }, \
 
 #define SCANNERS_DESKTOP \
   { "Desktop", extract_desktop, NULL, NULL, NULL, NULL }, \
+  { "Plugin", extract_plugin, NULL, NULL, NULL, NULL }, \
 
 /* Scan a Desktop Entry file and add its translatable strings to mdlp.  */
 extern void extract_desktop (FILE *fp, const char *real_filename,
@@ -39,6 +41,12 @@ extern void extract_desktop (FILE *fp, const char *real_filename,
                              flag_context_list_table_ty *flag_table,
                              msgdomain_list_ty *mdlp);
 
+/* Scan a Plugin file and add its translatable strings to mdlp.  */
+extern void extract_plugin (FILE *fp, const char *real_filename,
+                            const char *logical_filename,
+                            flag_context_list_table_ty *flag_table,
+                            msgdomain_list_ty *mdlp);
+
 extern void x_desktop_keyword (const char *keyword);
 
 
diff --git a/gettext-tools/tests/xgettext-plugin-1 b/gettext-tools/tests/xgettext-plugin-1
new file mode 100755
index 000000000..ea1f01835
--- /dev/null
+++ b/gettext-tools/tests/xgettext-plugin-1
@@ -0,0 +1,96 @@
+#!/bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test of Plugin support.
+
+: ${XGETTEXT=xgettext}
+
+cat <<\EOF >err1.plugin
+[Plugin]
+This is an invalid line
+Name =Foo
+EOF
+
+(LANGUAGE= LC_ALL=C ${XGETTEXT} --add-comments -o - err1.plugin 2>&1; exit) | grep "missing '=' after" || Exit 1
+
+cat <<\EOF >err2.plugin
+[Plugin
+EOF
+
+(LANGUAGE= LC_ALL=C ${XGETTEXT} --add-comments -o - err2.plugin 2>&1; exit) | grep "unterminated group name" || Exit 1
+
+cat <<\EOF >err3.plugin
+[Plugin]
+  Not a blank line!
+EOF
+
+(LANGUAGE= LC_ALL=C ${XGETTEXT} --add-comments -o - err3.plugin 2>&1; exit) | grep "invalid non-blank line" || Exit 1
+
+cat <<\EOF >err4.plugin
+[Plugin]a
+EOF
+
+(LANGUAGE= LC_ALL=C ${XGETTEXT} --add-comments -o - err4.plugin 2>&1; exit) | grep "invalid non-blank character" || Exit 1
+
+# gettext 0.19.4 issued an baseless warning of this.
+cat <<\EOF >ok4.plugin
+[Plugin]
+EOF
+
+(LANGUAGE= LC_ALL=C ${XGETTEXT} --add-comments -o - ok4.plugin 2>&1; exit) | grep "invalid non-blank character" && Exit 1
+
+cat <<\EOF > xg.plugin
+[Plugin]
+Module=helloworld
+Loader=C
+Name =Foo
+# This is a comment for description
+#   This is a comment for description
+Description= \sThis is a \nmultiline\t description; for testing
+Description[foo]=Already translated description
+Copyright = Copyright (C) 1995-1998, 2000-2016 Free Software Foundation, Inc.
+Website=https://wiki.gnome.org/Projects/Libpeas
+# This is a comment before a blank line
+
+Depends=Keyword1;Keyword2;Key\;word3;
+EOF
+
+${XGETTEXT} --add-comments -o - xg.plugin | grep -v 'POT-Creation-Date' > xg-plugin.pot || Exit 1
+
+cat <<\EOF > xg-plugin.ok
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <l...@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=CHARSET\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: xg.plugin:5
+msgid "Foo"
+msgstr ""
+
+#. This is a comment for description
+#. This is a comment for description
+#: xg.plugin:8
+msgid ""
+" This is a \n"
+"multiline\t description; for testing"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-plugin.ok xg-plugin.pot
+result=$?
+
+exit $result
-- 
2.11.0

Reply via email to