1 /* xgettext Desktop Entry backend.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 This file was written by Daiki Ueno <ueno@gnu.org>, 2014.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "x-desktop.h"
35 #include "error-progname.h"
37 #include "xvasprintf.h"
40 #include "read-desktop.h"
41 #include "po-charset.h"
44 #define _(s) gettext(s)
46 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
48 /* ====================== Keyword set customization. ====================== */
50 /* The syntax of a Desktop Entry file is defined at
51 http://standards.freedesktop.org/desktop-entry-spec/latest/index.html
53 Basically, values with 'localestring' type can be translated.
55 The type of a value is determined by looking at the key associated
56 with it. The list of available keys are listed on:
57 http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s05.html */
59 static hash_table keywords;
60 static bool default_keywords = true;
63 add_keyword (const char *name, hash_table *keywords, bool is_list)
66 default_keywords = false;
69 if (keywords->table == NULL)
70 hash_init (keywords, 100);
72 desktop_add_keyword (keywords, name, is_list);
77 x_desktop_keyword (const char *name)
79 add_keyword (name, &keywords, false);
87 if (keywords.table == NULL)
88 hash_init (&keywords, 100);
90 desktop_add_default_keywords (&keywords);
91 default_keywords = false;
95 typedef struct extract_desktop_reader_ty extract_desktop_reader_ty;
96 struct extract_desktop_reader_ty
100 message_list_ty *mlp;
104 extract_desktop_handle_group (struct desktop_reader_ty *reader,
107 savable_comment_reset ();
111 extract_desktop_handle_pair (struct desktop_reader_ty *reader,
117 extract_desktop_reader_ty *extract_reader =
118 (extract_desktop_reader_ty *) reader;
121 if (!locale /* Skip already translated entry. */
122 && hash_find_entry (&keywords, key, strlen (key), &keyword_value) == 0)
124 bool is_list = (bool) keyword_value;
126 remember_a_message (extract_reader->mlp, NULL,
127 desktop_unescape_string (value, is_list),
128 null_context, key_pos,
129 NULL, savable_comment);
131 savable_comment_reset ();
135 extract_desktop_handle_comment (struct desktop_reader_ty *reader,
138 size_t buflen = strlen (buffer);
141 while (bufpos < buflen
142 && c_isspace (buffer[bufpos]))
144 while (buflen >= bufpos
145 && c_isspace (buffer[buflen - 1]))
149 char *comment = xstrdup (buffer);
151 savable_comment_add (&comment[bufpos]);
157 extract_desktop_handle_blank (struct desktop_reader_ty *reader,
160 savable_comment_reset ();
163 desktop_reader_class_ty extract_methods =
165 sizeof (extract_desktop_reader_ty),
168 extract_desktop_handle_group,
169 extract_desktop_handle_pair,
170 extract_desktop_handle_comment,
171 extract_desktop_handle_blank
175 extract_desktop (FILE *f,
176 const char *real_filename, const char *logical_filename,
177 flag_context_list_table_ty *flag_table,
178 msgdomain_list_ty *mdlp)
180 desktop_reader_ty *reader = desktop_reader_alloc (&extract_methods);
181 extract_desktop_reader_ty *extract_reader =
182 (extract_desktop_reader_ty *) reader;
185 xgettext_current_source_encoding = po_charset_utf8;
187 extract_reader->mlp = mdlp->item[0]->messages;
189 desktop_parse (reader, f, real_filename, logical_filename);
190 desktop_reader_free (reader);