medit/moo/mooutils/mooi18n.c

198 lines
4.7 KiB
C
Raw Normal View History

/*
* mooi18n.c
*
2010-12-21 20:15:45 -08:00
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
*
* This file is part of medit. medit is free software; you can
* redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mooutils/mooi18n.h"
2006-12-10 16:52:08 -06:00
#include "mooutils/mooutils-misc.h"
2010-11-07 22:42:56 -08:00
#include "mooutils/mooutils-tests.h"
#include <glib.h>
2010-12-10 03:10:22 -08:00
/**
2011-01-22 03:21:42 -08:00
* moo_dgettext: (moo.lua 0)
2010-12-10 03:10:22 -08:00
*
* @domain: (type const-utf8):
* @string: (type const-utf8):
2010-12-10 03:10:22 -08:00
*
* Returns: (type const-utf8):
2010-12-10 03:10:22 -08:00
**/
const char *moo_gettext (const char *string) G_GNUC_FORMAT (1);
2007-06-20 13:46:26 -05:00
#ifdef ENABLE_NLS
2007-06-18 05:18:02 -05:00
static void
init_gettext (void)
{
static gboolean been_here = FALSE;
if (!been_here)
{
been_here = TRUE;
bindtextdomain (GETTEXT_PACKAGE, moo_get_locale_dir ());
2007-06-18 05:18:02 -05:00
bindtextdomain (GETTEXT_PACKAGE "-gsv", moo_get_locale_dir ());
2006-12-10 16:52:08 -06:00
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2007-06-18 05:18:02 -05:00
bind_textdomain_codeset (GETTEXT_PACKAGE "-gsv", "UTF-8");
2006-12-10 16:52:08 -06:00
#endif
}
2007-06-18 05:18:02 -05:00
}
2007-06-20 13:46:26 -05:00
#endif /* ENABLE_NLS */
2010-12-10 03:10:22 -08:00
/**
2011-01-22 03:21:42 -08:00
* moo_gettext: (moo.lua 0)
*
* @string: (type const-utf8)
*
* Returns: (type const-utf8)
2010-12-10 03:10:22 -08:00
**/
2007-06-18 05:18:02 -05:00
const char *
moo_gettext (const char *string)
{
#ifdef ENABLE_NLS
g_return_val_if_fail (string != NULL, NULL);
init_gettext ();
return dgettext (GETTEXT_PACKAGE, string);
2006-08-05 02:07:56 -05:00
#else /* !ENABLE_NLS */
return string;
#endif /* !ENABLE_NLS */
}
2007-05-27 15:08:41 -05:00
2008-09-09 22:40:29 -05:00
const char *
2009-11-01 12:44:46 -08:00
moo_pgettext (const char *msgctxtid, G_GNUC_UNUSED gsize msgidoffset)
2008-09-09 22:40:29 -05:00
{
#ifdef ENABLE_NLS
g_return_val_if_fail (msgctxtid != NULL, NULL);
init_gettext ();
return moo_dpgettext (GETTEXT_PACKAGE, msgctxtid, msgidoffset);
#else /* !ENABLE_NLS */
return msgctxtid;
#endif /* !ENABLE_NLS */
}
const char *
2009-11-01 12:44:46 -08:00
moo_pgettext2 (G_GNUC_UNUSED const char *context, const char *msgctxtid)
2008-09-09 22:40:29 -05:00
{
#ifdef ENABLE_NLS
char *tmp;
const char *translation;
g_return_val_if_fail (msgctxtid != NULL, NULL);
init_gettext ();
tmp = g_strjoin (context, "\004", msgctxtid, NULL);
translation = dgettext (GETTEXT_PACKAGE, tmp);
if (translation == tmp)
translation = msgctxtid;
g_free (tmp);
return translation;
#else /* !ENABLE_NLS */
return msgctxtid;
#endif /* !ENABLE_NLS */
}
const char *
moo_dpgettext (const char *domain, const char *msgctxtid, gsize msgidoffset)
{
g_return_val_if_fail (domain != NULL, NULL);
g_return_val_if_fail (msgctxtid != NULL, NULL);
#ifdef ENABLE_NLS
return g_dpgettext (GETTEXT_PACKAGE, msgctxtid, msgidoffset);
#else
return msgctxtid + msgidoffset;
#endif
}
2007-06-18 05:18:02 -05:00
const char *
_moo_gsv_gettext (const char *string)
{
#ifdef ENABLE_NLS
g_return_val_if_fail (string != NULL, NULL);
init_gettext ();
return dgettext (GETTEXT_PACKAGE "-gsv", string);
#else /* !ENABLE_NLS */
return string;
#endif /* !ENABLE_NLS */
}
2007-05-27 15:08:41 -05:00
char *
2009-11-01 12:44:46 -08:00
_moo_gsv_dgettext (G_GNUC_UNUSED const char *domain, const char *string)
2007-05-27 15:08:41 -05:00
{
#ifdef ENABLE_NLS
gchar *tmp;
const gchar *translated;
g_return_val_if_fail (string != NULL, NULL);
2007-06-18 05:18:02 -05:00
init_gettext ();
2007-05-27 15:08:41 -05:00
if (domain == NULL)
2007-06-18 05:18:02 -05:00
return g_strdup (_moo_gsv_gettext (string));
2007-05-27 15:08:41 -05:00
translated = dgettext (domain, string);
if (strcmp (translated, string) == 0)
2007-06-18 05:18:02 -05:00
return g_strdup (_moo_gsv_gettext (string));
2007-05-27 15:08:41 -05:00
if (g_utf8_validate (translated, -1, NULL))
return g_strdup (translated);
tmp = g_locale_to_utf8 (translated, -1, NULL, NULL, NULL);
if (tmp == NULL)
return g_strdup (string);
else
return tmp;
#else
return g_strdup (string);
#endif /* !ENABLE_NLS */
}
2010-11-07 22:42:56 -08:00
static void
test_mooi18n (void)
{
#ifdef ENABLE_NLS
const char *locale_dir;
char *po_file, *po_file2;
locale_dir = moo_get_locale_dir ();
po_file = g_build_filename (locale_dir, "ru", "LC_MESSAGES", GETTEXT_PACKAGE ".mo", NULL);
po_file2 = g_build_filename (locale_dir, "ru", "LC_MESSAGES", GETTEXT_PACKAGE "-gsv.mo", NULL);
TEST_ASSERT_MSG (g_file_test (po_file, G_FILE_TEST_EXISTS), "mo file '%s' does not exist", po_file);
TEST_ASSERT_MSG (g_file_test (po_file2, G_FILE_TEST_EXISTS), "mo file '%s' does not exist", po_file2);
g_free (po_file2);
g_free (po_file);
#endif
}
void
2011-01-02 01:41:49 -08:00
moo_test_i18n (MooTestOptions opts)
2010-11-07 22:42:56 -08:00
{
2011-01-02 01:41:49 -08:00
if (opts & MOO_TEST_INSTALLED)
{
MooTestSuite *suite;
2010-11-07 22:42:56 -08:00
2011-01-02 01:41:49 -08:00
suite = moo_test_suite_new ("mooi18n", "mooutils/mooi18n.c", NULL, NULL, NULL);
2010-11-07 22:42:56 -08:00
2011-01-02 01:41:49 -08:00
moo_test_suite_add_test (suite, "mooi18n", "test of mooi18n",
(MooTestFunc) test_mooi18n, NULL);
}
2010-11-07 22:42:56 -08:00
}