Internationalisation

This commit is contained in:
Přemysl Eric Janouch 2013-05-19 02:46:05 +02:00
parent c430b6d7f8
commit 46083b1e7d
6 changed files with 196 additions and 34 deletions

View File

@ -23,6 +23,13 @@ pkg_check_modules (dependencies REQUIRED ncursesw glib-2.0 gio-2.0 pango)
include_directories (${dependencies_INCLUDE_DIRS}) include_directories (${dependencies_INCLUDE_DIRS})
# Localization
find_package (Gettext REQUIRED)
file (GLOB project_PO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/po/*.po)
GETTEXT_CREATE_TRANSLATIONS (
${CMAKE_CURRENT_SOURCE_DIR}/po/${CMAKE_PROJECT_NAME}.pot
ALL ${project_PO_FILES})
# Project source files # Project source files
set (project_common_sources set (project_common_sources
src/generator.c src/generator.c

View File

@ -5,5 +5,8 @@
#define PROJECT_VERSION "${project_VERSION}" #define PROJECT_VERSION "${project_VERSION}"
#define PROJECT_URL "${project_URL}" #define PROJECT_URL "${project_URL}"
#define GETTEXT_PACKAGE PROJECT_NAME
#define GETTEXT_DIRNAME "${CMAKE_INSTALL_PREFIX}/share/locale"
#endif /* ! CONFIG_H */ #endif /* ! CONFIG_H */

47
po/make-template.sh Executable file
View File

@ -0,0 +1,47 @@
#!/bin/sh
# This shell script generates the translation template.
#
# The reason for this not being inside CMakeLists.txt
# is that the translator should not need to run the whole
# configuration process just to get this single stupid file.
# Get the directory this script resides in so that the user
# doesn't have to run the script from there
DIR=$(dirname $0)
# Collect source files
SOURCES=$(echo $DIR/../src/*.c)
# Get the package name from CMakeLists.txt
PACKAGE=$(sed -n '/^[ \t]*project[ \t]*([ \t]*\([^ \t)]\{1,\}\).*).*/{s//\1/p;q}' \
$DIR/../CMakeLists.txt)
# Get the package version from CMakeLists.txt
EXP_BEG='/^[ \t]*set[ \t]*([ \t]*project_VERSION_'
EXP_END='[ \t]\{1,\}"\{0,1\}\([^)"]\{1,\}\)"\{0,1\}).*/{s//\1/p;q}'
MAJOR=$(sed -n "${EXP_BEG}MAJOR${EXP_END}" $DIR/../CMakeLists.txt)
MINOR=$(sed -n "${EXP_BEG}MINOR${EXP_END}" $DIR/../CMakeLists.txt)
PATCH=$(sed -n "${EXP_BEG}PATCH${EXP_END}" $DIR/../CMakeLists.txt)
if [ "$MAJOR" != "" ]; then
VERSION=$MAJOR
if [ "$MINOR" != "" ]; then
VERSION=$VERSION.$MINOR
if [ "$PATCH" != "" ]; then
VERSION=$VERSION.$PATCH
fi
fi
fi
if [ -z "$PACKAGE" -o -z "$VERSION" ]; then
echo "Failed to get information from CMakeLists.txt"
exit 1
fi
# Finally make the template
xgettext -LC -k_ -kN_ $SOURCES -o "$DIR/$PACKAGE.pot" \
--package-name="$PACKAGE" --package-version="$VERSION" \
--copyright-holder="Přemysl Janouch" \
--msgid-bugs-address="https://github.com/pjanouch/$PACKAGE/issues"

110
po/sdtui.pot Normal file
View File

@ -0,0 +1,110 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2013 Přemysl Janouch
# This file is distributed under the same license as the sdtui package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: sdtui 0.1.0\n"
"Report-Msgid-Bugs-To: https://github.com/pjanouch/sdtui/issues\n"
"POT-Creation-Date: 2013-05-19 04:58+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: ../src/sdtui.c:230
msgid "error in entry"
msgstr ""
#: ../src/sdtui.c:249
msgid "no usable field found"
msgstr ""
#: ../src/sdtui.c:295
msgid "Error loading dictionary"
msgstr ""
#: ../src/sdtui.c:308
msgid "Search"
msgstr ""
#: ../src/sdtui.c:440
msgid "Terminal UI for StarDict dictionaries"
msgstr ""
#: ../src/sdtui.c:443
msgid "Type to search"
msgstr ""
#: ../src/sdtui.c:1054
msgid "Output version information and exit"
msgstr ""
#: ../src/sdtui.c:1067
msgid "dictionary.ifo - StarDict terminal UI"
msgstr ""
#: ../src/sdtui.c:1072
msgid "Error"
msgstr ""
#: ../src/sdtui.c:1072
msgid "option parsing failed"
msgstr ""
#: ../src/stardict.c:136
msgid "invalid header format"
msgstr ""
#: ../src/stardict.c:268
msgid "version not specified"
msgstr ""
#: ../src/stardict.c:279
msgid "invalid version"
msgstr ""
#: ../src/stardict.c:294
msgid "unknown key, ignoring"
msgstr ""
#: ../src/stardict.c:311
msgid "invalid integer"
msgstr ""
#: ../src/stardict.c:321
msgid "option format error"
msgstr ""
#: ../src/stardict.c:331
msgid "no book name specified"
msgstr ""
#: ../src/stardict.c:337
msgid "word count not specified"
msgstr ""
#: ../src/stardict.c:343
msgid "index file size not specified"
msgstr ""
#: ../src/stardict.c:352
msgid "invalid index offset bits"
msgstr ""
#: ../src/stardict.c:679
msgid "cannot find .idx file"
msgstr ""
#: ../src/stardict.c:702
msgid "cannot find .dict file"
msgstr ""
#: ../src/stardict.c:861 ../src/stardict.c:886
msgid "invalid data entry"
msgstr ""

View File

@ -31,6 +31,7 @@
#include <gio/gio.h> #include <gio/gio.h>
#include <pango/pango.h> #include <pango/pango.h>
#include <ncurses.h> #include <ncurses.h>
#include <glib/gi18n.h>
#include <unistd.h> #include <unistd.h>
#include <poll.h> #include <poll.h>
@ -75,8 +76,6 @@ typedef enum {
KEY_ALT_RIGHT //!< Alt + Right arrow KEY_ALT_RIGHT //!< Alt + Right arrow
} ExtraKeyCode; //!< Translated key codes above KEY_MAX } ExtraKeyCode; //!< Translated key codes above KEY_MAX
#define _(x) x /**< Fake gettext, for now. */
// --- Utilities --------------------------------------------------------------- // --- Utilities ---------------------------------------------------------------
static int static int
@ -293,7 +292,7 @@ app_init (Application *self, const gchar *filename)
self->dict = stardict_dict_new (filename, &error); self->dict = stardict_dict_new (filename, &error);
if (!self->dict) if (!self->dict)
{ {
g_printerr ("Error loading dictionary: %s\n", error->message); g_printerr ("%s: %s\n", _("Error loading dictionary"), error->message);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
@ -306,11 +305,7 @@ app_init (Application *self, const gchar *filename)
self->entries = g_ptr_array_new_with_free_func self->entries = g_ptr_array_new_with_free_func
((GDestroyNotify) view_entry_free); ((GDestroyNotify) view_entry_free);
gchar *search_label = g_strdup_printf ("%s: ", _("Search")); self->search_label = g_strdup_printf ("%s: ", _("Search"));
self->search_label = g_locale_to_utf8 (search_label, -1, NULL, NULL, NULL);
g_free (search_label);
g_assert (self->search_label != NULL);
self->input = g_array_new (TRUE, FALSE, sizeof (gunichar)); self->input = g_array_new (TRUE, FALSE, sizeof (gunichar));
self->input_pos = 0; self->input_pos = 0;
@ -439,23 +434,16 @@ app_show_message (Application *self, const gchar *lines[], gsize len)
static void static void
app_show_help (Application *self) app_show_help (Application *self)
{ {
gchar *description = g_locale_to_utf8
(_("Terminal UI for StarDict dictionaries"), -1, NULL, NULL, NULL);
gchar *type_to_search = g_locale_to_utf8
(_("Type to search"), -1, NULL, NULL, NULL);
const gchar *lines[] = const gchar *lines[] =
{ {
PROJECT_NAME " " PROJECT_VERSION, PROJECT_NAME " " PROJECT_VERSION,
description, _("Terminal UI for StarDict dictionaries"),
"Copyright (c) 2013, Přemysl Janouch", "Copyright (c) 2013, Přemysl Janouch",
"", "",
type_to_search _("Type to search")
}; };
app_show_message (self, lines, G_N_ELEMENTS (lines)); app_show_message (self, lines, G_N_ELEMENTS (lines));
g_free (description);
g_free (type_to_search);
} }
/** Redraw the dictionary view. */ /** Redraw the dictionary view. */
@ -1063,17 +1051,22 @@ G_GNUC_END_IGNORE_DEPRECATIONS
{ {
{ "version", 0, G_OPTION_FLAG_IN_MAIN, { "version", 0, G_OPTION_FLAG_IN_MAIN,
G_OPTION_ARG_NONE, &show_version, G_OPTION_ARG_NONE, &show_version,
"Output version information and exit", NULL }, N_("Output version information and exit"), NULL },
{ NULL } { NULL }
}; };
if (!setlocale (LC_ALL, "")) if (!setlocale (LC_ALL, ""))
abort (); abort ();
bindtextdomain (GETTEXT_PACKAGE, GETTEXT_DIRNAME);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
GError *error = NULL; GError *error = NULL;
GOptionContext *ctx = g_option_context_new GOptionContext *ctx = g_option_context_new
("dictionary.ifo - StarDict terminal UI"); (N_("dictionary.ifo - StarDict terminal UI"));
g_option_context_add_main_entries (ctx, entries, NULL); g_option_context_add_main_entries (ctx, entries, GETTEXT_PACKAGE);
g_option_context_set_translation_domain (ctx, GETTEXT_PACKAGE);
if (!g_option_context_parse (ctx, &argc, &argv, &error)) if (!g_option_context_parse (ctx, &argc, &argv, &error))
{ {
g_printerr ("%s: %s: %s\n", _("Error"), _("option parsing failed"), g_printerr ("%s: %s: %s\n", _("Error"), _("option parsing failed"),

View File

@ -25,6 +25,7 @@
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <glib/gi18n.h>
#include "stardict.h" #include "stardict.h"
#include "stardict-private.h" #include "stardict-private.h"
@ -132,7 +133,7 @@ ifo_reader_init (IfoReader *ir, const gchar *path, GError **error)
|| strncmp (contents, first_line, sizeof first_line - 1)) || strncmp (contents, first_line, sizeof first_line - 1))
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: invalid header format", path); "%s: %s", path, _("invalid header format"));
return FALSE; return FALSE;
} }
@ -264,7 +265,7 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
if (ifo_reader_read (&ir) != 1 || strcmp (ir.key, "version")) if (ifo_reader_read (&ir) != 1 || strcmp (ir.key, "version"))
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: version not specified", path); "%s: %s", path, _("version not specified"));
goto error; goto error;
} }
@ -275,7 +276,7 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
else else
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: invalid version: %s", path, ir.value); "%s: %s: %s", path, _("invalid version"), ir.value);
goto error; goto error;
} }
@ -290,7 +291,7 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
if (i == _stardict_ifo_keys_length) if (i == _stardict_ifo_keys_length)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: unknown key, ignoring: %s", path, ir.key); "%s: %s: %s", path, _("unknown key, ignoring"), ir.key);
continue; continue;
} }
@ -307,7 +308,7 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
if (*end) if (*end)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: invalid integer", path); "%s: %s", path, _("invalid integer"));
goto error; goto error;
} }
@ -317,7 +318,7 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
if (ret == -1) if (ret == -1)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: option format error", path); "%s: %s", path, _("option format error"));
goto error; goto error;
} }
@ -327,19 +328,19 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
if (!sti->book_name || !*sti->book_name) if (!sti->book_name || !*sti->book_name)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: no book name specified\n", path); "%s: %s", path, _("no book name specified"));
ret_val = FALSE; ret_val = FALSE;
} }
if (!sti->word_count) if (!sti->word_count)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: word count not specified\n", path); "%s: %s", path, _("word count not specified"));
ret_val = FALSE; ret_val = FALSE;
} }
if (!sti->idx_filesize) if (!sti->idx_filesize)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: .idx file size not specified\n", path); "%s: %s", path, _("index file size not specified"));
ret_val = FALSE; ret_val = FALSE;
} }
@ -348,7 +349,8 @@ load_ifo (StardictInfo *sti, const gchar *path, GError **error)
else if (sti->idx_offset_bits != 32 && sti->idx_offset_bits != 64) else if (sti->idx_offset_bits != 32 && sti->idx_offset_bits != 64)
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"%s: wrong index offset bits: %lu\n", path, sti->idx_offset_bits); "%s: %s: %lu", path, _("invalid index offset bits"),
sti->idx_offset_bits);
ret_val = FALSE; ret_val = FALSE;
} }
@ -674,7 +676,7 @@ stardict_dict_new_from_info (StardictInfo *sdi, GError **error)
else else
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_FILE_NOT_FOUND, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_FILE_NOT_FOUND,
"%s: cannot find index file", sdi->path); "%s: %s", sdi->path, _("cannot find .idx file"));
} }
} }
g_free (base_idx); g_free (base_idx);
@ -697,7 +699,7 @@ stardict_dict_new_from_info (StardictInfo *sdi, GError **error)
else else
{ {
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_FILE_NOT_FOUND, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_FILE_NOT_FOUND,
"%s: cannot find dict file", sdi->path); "%s: %s", sdi->path, _("cannot find .dict file"));
} }
} }
g_free (base_dict); g_free (base_dict);
@ -856,7 +858,7 @@ read_entries (const gchar *entry, gsize entry_size, GError **error)
error: error:
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"invalid data entry"); _("invalid data entry"));
g_list_free_full (result, (GDestroyNotify) stardict_entry_field_free); g_list_free_full (result, (GDestroyNotify) stardict_entry_field_free);
return NULL; return NULL;
} }
@ -881,7 +883,7 @@ read_entries_sts (const gchar *entry, gsize entry_size,
error: error:
g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA, g_set_error (error, STARDICT_ERROR, STARDICT_ERROR_INVALID_DATA,
"invalid data entry"); _("invalid data entry"));
g_list_free_full (result, (GDestroyNotify) stardict_entry_field_free); g_list_free_full (result, (GDestroyNotify) stardict_entry_field_free);
return NULL; return NULL;
} }