[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-gettext] [PATCHv2 2/2] msgfmt: Add support for Desktop Entry files
From: |
Daiki Ueno |
Subject: |
[bug-gettext] [PATCHv2 2/2] msgfmt: Add support for Desktop Entry files |
Date: |
Mon, 17 Mar 2014 18:38:42 +0900 |
This patch adds a new operation mode to msgfmt to support Desktop
Entry files, which can be used as:
$ msgfmt --desktop --template=foo.desktop --locale=fr fr.po \
-o foo.fr.desktop
This command merges translations from fr.po into foo.desktop. The
intermediate output file can be used as an input to the next call of
the msgfmt command:
$ msgfmt --desktop --template=foo.fr.desktop --locale=de de.po \
-o foo.de.desktop
Here, foo.de.desktop contains both French and German translations.
msgfmt now also supports --keyword option in the same syntax as of
xgettext.
---
gettext-tools/doc/msgfmt.texi | 31 +++++
gettext-tools/src/Makefile.am | 3 +-
gettext-tools/src/msgfmt.c | 82 ++++++++++++-
gettext-tools/src/write-desktop.c | 219 +++++++++++++++++++++++++++++++++++
gettext-tools/src/write-desktop.h | 44 +++++++
gettext-tools/tests/Makefile.am | 1 +
gettext-tools/tests/msgfmt-desktop-1 | 82 +++++++++++++
7 files changed, 459 insertions(+), 3 deletions(-)
create mode 100644 gettext-tools/src/write-desktop.c
create mode 100644 gettext-tools/src/write-desktop.h
create mode 100755 gettext-tools/tests/msgfmt-desktop-1
diff --git a/gettext-tools/doc/msgfmt.texi b/gettext-tools/doc/msgfmt.texi
index 2a0dd6f..8a31bac 100644
--- a/gettext-tools/doc/msgfmt.texi
+++ b/gettext-tools/doc/msgfmt.texi
@@ -60,6 +60,11 @@ Tcl mode: generate a tcl/msgcat @file{.msg} file.
@cindex Qt mode, and @code{msgfmt} program
Qt mode: generate a Qt @file{.qm} file.
address@hidden --desktop
address@hidden address@hidden, @code{msgfmt} option}
address@hidden Desktop Entry mode, and @code{msgfmt} program
+Desktop Entry mode: generate a @file{.desktop} file.
+
@end table
@subsection Output file location
@@ -158,6 +163,32 @@ Specify the base directory of @file{.msg} message catalogs.
The @samp{-l} and @samp{-d} options are mandatory. The @file{.msg} file is
written in the specified directory.
address@hidden Output file location in Desktop Entry mode
+
address@hidden @samp
address@hidden -l @var{locale}
address@hidden address@hidden
address@hidden address@hidden, @code{msgfmt} option}
address@hidden address@hidden, @code{msgfmt} option}
+Specify the locale name, either a language specification of the form @var{ll}
+or a combined language and country specification of the form @var{ll_CC}.
+
address@hidden address@hidden
address@hidden address@hidden, @code{msgfmt} option}
+Specify a .desktop file used as a template.
+
address@hidden address@hidden
address@hidden address@hidden
address@hidden address@hidden, @code{msgfmt} option}
address@hidden address@hidden, @code{msgfmt} option}
+Specify @var{keywordspec} as an additional keyword to be looked for.
+Without a @var{keywordspec}, the option means to not use default keywords.
+
address@hidden table
+
+The @samp{-l} and @samp{--template} options are mandatory. The
address@hidden file is written in the specified directory.
+
@subsection Input file syntax
@table @samp
diff --git a/gettext-tools/src/Makefile.am b/gettext-tools/src/Makefile.am
index e69c00c..25947c3 100644
--- a/gettext-tools/src/Makefile.am
+++ b/gettext-tools/src/Makefile.am
@@ -48,6 +48,7 @@ read-csharp.h write-csharp.h \
read-resources.h write-resources.h \
read-tcl.h write-tcl.h \
write-qt.h \
+read-desktop.h write-desktop.h \
po-time.h plural-table.h lang-table.h format.h filters.h \
xgettext.h x-c.h x-po.h x-sh.h x-python.h x-lisp.h x-elisp.h x-librep.h \
x-scheme.h x-smalltalk.h x-java.h x-properties.h x-csharp.h x-awk.h x-ycp.h \
@@ -160,7 +161,7 @@ msgcmp_SOURCES += msgl-fsearch.c
msgfmt_SOURCES = msgfmt.c
msgfmt_SOURCES += \
write-mo.c write-java.c write-csharp.c write-resources.c write-tcl.c \
- write-qt.c ../../gettext-runtime/intl/hash-string.c
+ write-qt.c write-desktop.c ../../gettext-runtime/intl/hash-string.c
if !WOE32DLL
msgmerge_SOURCES = msgmerge.c
else
diff --git a/gettext-tools/src/msgfmt.c b/gettext-tools/src/msgfmt.c
index 4da999a..dfb60f5 100644
--- a/gettext-tools/src/msgfmt.c
+++ b/gettext-tools/src/msgfmt.c
@@ -45,6 +45,7 @@
#include "write-resources.h"
#include "write-tcl.h"
#include "write-qt.h"
+#include "write-desktop.h"
#include "propername.h"
#include "message.h"
#include "open-catalog.h"
@@ -94,6 +95,13 @@ static const char *tcl_base_directory;
/* Qt mode output file specification. */
static bool qt_mode;
+/* Desktop Entry mode output file specification. */
+static bool desktop_mode;
+static const char *desktop_locale_name;
+static const char *desktop_template_name;
+static hash_table desktop_keywords;
+static bool desktop_default_keywords = true;
+
/* We may have more than one input file. Domains with same names in
different files have to merged. So we need a list of tables for
each output file. */
@@ -157,11 +165,13 @@ static const struct option long_options[] =
{ "check-header", no_argument, NULL, CHAR_MAX + 4 },
{ "csharp", no_argument, NULL, CHAR_MAX + 10 },
{ "csharp-resources", no_argument, NULL, CHAR_MAX + 11 },
+ { "desktop", no_argument, NULL, CHAR_MAX + 14 },
{ "directory", required_argument, NULL, 'D' },
{ "endianness", required_argument, NULL, CHAR_MAX + 13 },
{ "help", no_argument, NULL, 'h' },
{ "java", no_argument, NULL, 'j' },
{ "java2", no_argument, NULL, CHAR_MAX + 5 },
+ { "keyword", required_argument, NULL, 'k' },
{ "locale", required_argument, NULL, 'l' },
{ "no-hash", no_argument, NULL, CHAR_MAX + 6 },
{ "output-file", required_argument, NULL, 'o' },
@@ -172,6 +182,7 @@ static const struct option long_options[] =
{ "strict", no_argument, NULL, 'S' },
{ "stringtable-input", no_argument, NULL, CHAR_MAX + 8 },
{ "tcl", no_argument, NULL, CHAR_MAX + 7 },
+ { "template", required_argument, NULL, CHAR_MAX + 15 },
{ "use-fuzzy", no_argument, NULL, 'f' },
{ "use-untranslated", no_argument, NULL, CHAR_MAX + 12 },
{ "verbose", no_argument, NULL, 'v' },
@@ -268,10 +279,25 @@ main (int argc, char *argv[])
case 'j':
java_mode = true;
break;
+ case 'k':
+ if (optarg == NULL)
+ desktop_default_keywords = false;
+ else
+ {
+ if (desktop_keywords.table == NULL)
+ {
+ hash_init (&desktop_keywords, 100);
+ desktop_default_keywords = false;
+ }
+
+ desktop_add_keyword (&desktop_keywords, optarg, false);
+ }
+ break;
case 'l':
java_locale_name = optarg;
csharp_locale_name = optarg;
tcl_locale_name = optarg;
+ desktop_locale_name = optarg;
break;
case 'o':
output_file_name = optarg;
@@ -353,6 +379,12 @@ main (int argc, char *argv[])
byteswap = endianness ^ ENDIANNESS;
}
break;
+ case CHAR_MAX + 14: /* --desktop */
+ desktop_mode = true;
+ break;
+ case CHAR_MAX + 15: /* --template=TEMPLATE */
+ desktop_template_name = optarg;
+ break;
default:
usage (EXIT_FAILURE);
break;
@@ -391,9 +423,11 @@ There is NO WARRANTY, to the extent permitted by law.\n\
| (csharp_mode ? 2 : 0)
| (csharp_resources_mode ? 4 : 0)
| (tcl_mode ? 8 : 0)
- | (qt_mode ? 16 : 0);
+ | (qt_mode ? 16 : 0)
+ | (desktop_mode ? 32 : 0);
static const char *mode_options[] =
- { "--java", "--csharp", "--csharp-resources", "--tcl", "--qt" };
+ { "--java", "--csharp", "--csharp-resources", "--tcl", "--qt",
+ "--desktop" };
/* More than one bit set? */
if (modes & (modes - 1))
{
@@ -471,6 +505,16 @@ There is NO WARRANTY, to the extent permitted by law.\n\
usage (EXIT_FAILURE);
}
}
+ else if (desktop_mode)
+ {
+ if (desktop_locale_name == NULL)
+ {
+ error (EXIT_SUCCESS, 0,
+ _("%s requires a \"-l locale\" specification"),
+ "--desktop");
+ usage (EXIT_FAILURE);
+ }
+ }
else
{
if (java_resource_name != NULL)
@@ -585,6 +629,25 @@ There is NO WARRANTY, to the extent permitted by law.\n\
domain->domain_name, domain->file_name))
exit_status = EXIT_FAILURE;
}
+ else if (desktop_mode)
+ {
+ if (desktop_default_keywords)
+ {
+ if (desktop_keywords.table == NULL)
+ hash_init (&desktop_keywords, 100);
+ desktop_add_default_keywords (&desktop_keywords);
+ }
+
+ if (msgdomain_write_desktop (domain->mlp, canon_encoding,
+ desktop_locale_name,
+ desktop_template_name,
+ &desktop_keywords,
+ domain->file_name))
+ exit_status = EXIT_FAILURE;
+
+ if (desktop_keywords.table != NULL)
+ hash_destroy (&desktop_keywords);
+ }
else
{
if (msgdomain_write_mo (domain->mlp, domain->domain_name,
@@ -688,6 +751,8 @@ Operation mode:\n"));
--tcl Tcl mode: generate a tcl/msgcat .msg file\n"));
printf (_("\
--qt Qt mode: generate a Qt .qm file\n"));
+ printf (_("\
+ --desktop Desktop Entry mode: generate a .desktop
file\n"));
printf ("\n");
printf (_("\
Output file location:\n"));
@@ -735,6 +800,19 @@ The -l and -d options are mandatory. The .msg file is
written in the\n\
specified directory.\n"));
printf ("\n");
printf (_("\
+Output file location in Desktop Entry mode:\n"));
+ printf (_("\
+ -l, --locale=LOCALE locale name, either language or
language_COUNTRY\n"));
+ printf (_("\
+ --template=TEMPLATE a .desktop file used as a template\n"));
+ printf (_("\
+ -kWORD, --keyword=WORD look for WORD as an additional keyword\n\
+ -k, --keyword do not to use default keywords\n"));
+ printf (_("\
+The -l and --template options are mandatory. The .desktop file is written
in\n\
+the specified directory.\n"));
+ printf ("\n");
+ printf (_("\
Input file syntax:\n"));
printf (_("\
-P, --properties-input input files are in Java .properties syntax\n"));
diff --git a/gettext-tools/src/write-desktop.c
b/gettext-tools/src/write-desktop.c
new file mode 100644
index 0000000..cf3dc32
--- /dev/null
+++ b/gettext-tools/src/write-desktop.c
@@ -0,0 +1,219 @@
+/* Writing Desktop Entry files.
+ Copyright (C) 1995-1998, 2000-2003, 2005-2006, 2008-2009, 2014 Free
Software Foundation, Inc.
+ This file was written by Daiki Ueno <address@hidden>.
+
+ 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 3 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, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification. */
+#include "write-desktop.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "error.h"
+#include "msgl-iconv.h"
+#include "po-charset.h"
+#include "read-catalog.h"
+#include "read-po.h"
+#include "read-desktop.h"
+#include "fwriteerror.h"
+#include "gettext.h"
+
+#define _(str) gettext (str)
+
+typedef struct msgfmt_desktop_reader_ty msgfmt_desktop_reader_ty;
+struct msgfmt_desktop_reader_ty
+{
+ DESKTOP_READER_TY
+ message_list_ty *mlp;
+ const char *locale_name;
+ hash_table *keywords;
+ FILE *output_file;
+};
+
+static void
+msgfmt_desktop_handle_group (struct desktop_reader_ty *reader,
+ const char *group)
+{
+ msgfmt_desktop_reader_ty *msgfmt_reader = (msgfmt_desktop_reader_ty *)
reader;
+
+ fprintf (msgfmt_reader->output_file, "[%s]\n", group);
+}
+
+static void
+msgfmt_desktop_handle_pair (desktop_reader_ty *reader,
+ lex_pos_ty *key_pos,
+ const char *key,
+ const char *locale,
+ const char *value)
+{
+ msgfmt_desktop_reader_ty *msgfmt_reader = (msgfmt_desktop_reader_ty *)
reader;
+ void *keyword_value;
+
+ if (!locale)
+ {
+ /* Write untranslated pair. */
+ fprintf (msgfmt_reader->output_file, "%s=%s\n", key, value);
+
+ if (hash_find_entry (msgfmt_reader->keywords, key, strlen (key),
+ &keyword_value) == 0)
+ {
+ bool is_list = (bool) keyword_value;
+
+ if (is_list)
+ {
+ string_list_ty *slp = desktop_unescape_string_list (value);
+ string_list_ty *newslp = string_list_alloc ();
+ size_t i;
+
+ for (i = 0; i < slp->nitems; i++)
+ {
+ message_ty *mp;
+
+ mp = message_list_search (msgfmt_reader->mlp, NULL,
+ slp->item[i]);
+ if (mp && *mp->msgstr != '\0')
+ string_list_append (newslp, mp->msgstr);
+ }
+ string_list_free (slp);
+
+ if (newslp->nitems > 0)
+ {
+ char *escaped;
+
+ escaped = desktop_escape_string_list (newslp);
+ fprintf (msgfmt_reader->output_file, "%s[%s]=%s\n",
+ key, msgfmt_reader->locale_name, escaped);
+ free (escaped);
+ }
+ string_list_free (newslp);
+ }
+ else
+ {
+ char *unescaped = desktop_unescape_string (value);
+ message_ty *mp;
+
+ mp = message_list_search (msgfmt_reader->mlp, NULL, unescaped);
+ free (unescaped);
+
+ if (mp && *mp->msgstr != '\0')
+ {
+ char *escaped;
+
+ escaped = desktop_escape_string (mp->msgstr);
+ fprintf (msgfmt_reader->output_file,
+ "%s[%s]=%s\n",
+ key, msgfmt_reader->locale_name, escaped);
+ free (escaped);
+ }
+ }
+ }
+ }
+ else
+ fprintf (msgfmt_reader->output_file, "%s[%s]=%s\n", key, locale, value);
+}
+
+static void
+msgfmt_desktop_handle_comment (struct desktop_reader_ty *reader, const char *s)
+{
+ msgfmt_desktop_reader_ty *msgfmt_reader = (msgfmt_desktop_reader_ty *)
reader;
+
+ fputc ('#', msgfmt_reader->output_file);
+ fputs (s, msgfmt_reader->output_file);
+ fputc ('\n', msgfmt_reader->output_file);
+}
+
+static void
+msgfmt_desktop_handle_text (struct desktop_reader_ty *reader, const char *s)
+{
+ msgfmt_desktop_reader_ty *msgfmt_reader = (msgfmt_desktop_reader_ty *)
reader;
+
+ fputs (s, msgfmt_reader->output_file);
+ fputc ('\n', msgfmt_reader->output_file);
+}
+
+desktop_reader_class_ty msgfmt_methods =
+ {
+ sizeof (msgfmt_desktop_reader_ty),
+ NULL,
+ NULL,
+ msgfmt_desktop_handle_group,
+ msgfmt_desktop_handle_pair,
+ msgfmt_desktop_handle_comment,
+ msgfmt_desktop_handle_text
+ };
+
+int
+msgdomain_write_desktop (message_list_ty *mlp,
+ const char *canon_encoding,
+ const char *locale_name,
+ const char *template_file_name,
+ hash_table *keywords,
+ const char *file_name)
+{
+ desktop_reader_ty *reader;
+ msgfmt_desktop_reader_ty *msgfmt_reader;
+ FILE *template_file;
+
+ reader = desktop_reader_alloc (&msgfmt_methods);
+ msgfmt_reader = (msgfmt_desktop_reader_ty *) reader;
+
+ /* Convert the messages to Unicode. */
+ iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
+
+ msgfmt_reader->mlp = mlp;
+ msgfmt_reader->locale_name = locale_name;
+ msgfmt_reader->keywords = keywords;
+
+ if (strcmp (file_name, "-") == 0)
+ msgfmt_reader->output_file = stdout;
+ else
+ {
+ msgfmt_reader->output_file = fopen (file_name, "w");
+ if (msgfmt_reader->output_file == NULL)
+ {
+ desktop_reader_free (reader);
+ error (EXIT_SUCCESS,
+ errno, _("error while opening \"%s\" for writing"),
+ file_name);
+ return 1;
+ }
+ }
+
+ template_file = fopen (template_file_name, "r");
+ if (template_file == NULL)
+ {
+ desktop_reader_free (reader);
+ error (EXIT_SUCCESS,
+ errno, _("error while opening \"%s\" for reading"),
+ template_file_name);
+ return 1;
+ }
+
+ desktop_parse (reader, template_file, template_file_name,
template_file_name);
+
+ /* Make sure nothing went wrong. */
+ if (fwriteerror (msgfmt_reader->output_file))
+ error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
+ file_name);
+
+ desktop_reader_free (reader);
+
+ return 0;
+}
diff --git a/gettext-tools/src/write-desktop.h
b/gettext-tools/src/write-desktop.h
new file mode 100644
index 0000000..837b227
--- /dev/null
+++ b/gettext-tools/src/write-desktop.h
@@ -0,0 +1,44 @@
+/* Reading Desktop Entry files.
+ Copyright (C) 1995-1998, 2000-2003, 2005-2006, 2008-2009, 2014 Free
Software Foundation, Inc.
+ This file was written by Daiki Ueno <address@hidden>.
+
+ 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 3 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, see <http://www.gnu.org/licenses/>. */
+
+#ifndef _WRITE_DESKTOP_H
+#define _WRITE_DESKTOP_H
+
+#include "message.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Write a Desktop Entry file. mlp is a list containing the messages
+ to be output. locale_name is the locale name. template_file_name
+ is the template file. file_name is the output file. Return 0 if
+ ok, nonzero on error. */
+extern int
+ msgdomain_write_desktop (message_list_ty *mlp,
+ const char *canon_encoding,
+ const char *locale_name,
+ const char *template_file_name,
+ hash_table *keywords,
+ const char *file_name);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* _WRITE_DESKTOP_H */
diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am
index eb9367c..26d7dfa 100644
--- a/gettext-tools/tests/Makefile.am
+++ b/gettext-tools/tests/Makefile.am
@@ -48,6 +48,7 @@ TESTS = gettext-1 gettext-2 gettext-3 gettext-4 gettext-5
gettext-6 gettext-7 \
msgfmt-15 msgfmt-16 msgfmt-17 \
msgfmt-properties-1 \
msgfmt-qt-1 msgfmt-qt-2 \
+ msgfmt-desktop-1 \
msggrep-1 msggrep-2 msggrep-3 msggrep-4 msggrep-5 msggrep-6 msggrep-7 \
msggrep-8 msggrep-9 msggrep-10 \
msginit-1 msginit-2 \
diff --git a/gettext-tools/tests/msgfmt-desktop-1
b/gettext-tools/tests/msgfmt-desktop-1
new file mode 100755
index 0000000..d0b92f1
--- /dev/null
+++ b/gettext-tools/tests/msgfmt-desktop-1
@@ -0,0 +1,82 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test basic functioning with Desktop Entry syntax.
+
+cat <<\EOF > mf.desktop
+[Desktop Entry]
+Type=Application
+Name =Foo
+Comment= \sThis is a \nmultiline comment; for testing
+Comment[foo]=Already translated comment
+Keywords=Keyword1;Keyword2;Key\;word3;
+EOF
+
+cat <<\EOF > fr.po
+# 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 <address@hidden>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2014-03-17 07:36+0900\n"
+"PO-Revision-Date: 2014-03-17 08:40+0900\n"
+"Last-Translator: FULL NAME <address@hidden>\n"
+"Language-Team: LANGUAGE <address@hidden>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: xg.desktop:4
+msgid "Foo"
+msgstr ""
+"French\n"
+"foo"
+
+#: xg.desktop:5
+msgid ""
+" This is a \n"
+"multiline comment; for testing"
+msgstr ""
+"French \n"
+"comment"
+
+#: xg.desktop:7
+msgid "Keyword1"
+msgstr "one"
+
+#: xg.desktop:7
+msgid "Keyword2"
+msgstr "two"
+
+#: xg.desktop:7
+msgid "Key;word3"
+msgstr "thr;ee"
+
+EOF
+
+cat <<\EOF > mf.desktop.ok
+[Desktop Entry]
+Type=Application
+Name=Foo
+Name[fr]=French\nfoo
+Comment=\sThis is a \nmultiline comment; for testing
+Comment[fr]=French \ncomment
+Comment[foo]=Already translated comment
+Keywords=Keyword1;Keyword2;Key\;word3;
+Keywords[fr]=one;two;thr\;ee;
+EOF
+
+${MSGFMT} --desktop --template=mf.desktop -l fr fr.po -o mf.desktop.out \
+ || exit 1
+
+: ${DIFF=diff}
+${DIFF} mf.desktop.ok mf.desktop.out
+result=$?
+
+exit $result
--
1.8.4.2