anastasis-gtk

Demonstrator GUI for Anastasis
Log | Files | Refs | README | LICENSE

nls.c (3748B)


      1 /*
      2      This file is part of GNUnet.
      3      Copyright (C) 2010, 2011, 2021 GNUnet e.V.
      4 
      5      GNUnet is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      GNUnet is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with GNUnet; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19 */
     20 /**
     21  * @file src/lib/nls.c
     22  * @brief natural language support
     23  * @author Christian Grothoff
     24  */
     25 #include "anastasis_gtk_util.h"
     26 
     27 #if ENABLE_NLS
     28 #include <locale.h>
     29 #endif
     30 
     31 
     32 void
     33 ANASTASIS_GTK_setup_nls ()
     34 {
     35 #if ENABLE_NLS
     36   const struct GNUNET_OS_ProjectData *pd;
     37   char *path;
     38 
     39   pd = GNUNET_OS_project_data_get ();
     40   setlocale (LC_ALL, "");
     41   GNUNET_asprintf (&path,
     42                    "%s/%s/locale/",
     43                    ANASTASIS_GTK_get_data_dir (),
     44                    PACKAGE_NAME);
     45   bindtextdomain ("gnunet-gtk",
     46                   path);
     47   if (NULL != pd->gettext_path)
     48     bindtextdomain (pd->gettext_domain,
     49                     pd->gettext_path);
     50   textdomain (pd->gettext_domain);
     51   bind_textdomain_codeset ("GNUnet",
     52                            "UTF-8");
     53   bind_textdomain_codeset ("gnunet-gtk",
     54                            "UTF-8");
     55   bind_textdomain_codeset (pd->gettext_domain,
     56                            "UTF-8");
     57   GNUNET_free (path);
     58 #else
     59   fprintf (
     60     stderr,
     61     "WARNING: gnunet-gtk was compiled without i18n support (did CFLAGS contain -Werror?).\n");
     62 #endif
     63 }
     64 
     65 
     66 /* This is copied from GLib */
     67 /**
     68  * Obtain character set used for filenames on this system.
     69  *
     70  * @param filename_charset set to the character set used for filenames
     71  * @return TRUE if the locale is utf-8
     72  */
     73 static gboolean
     74 get_filename_charset (const gchar **filename_charset)
     75 {
     76   const gchar **charsets;
     77   gboolean is_utf8;
     78 
     79   is_utf8 = g_get_filename_charsets (&charsets);
     80   if (filename_charset)
     81     *filename_charset = charsets[0];
     82   return is_utf8;
     83 }
     84 
     85 
     86 char *
     87 ANASTASIS_GTK_from_loc_to_utf8 (const char *str_loc)
     88 {
     89   char *str_utf8;
     90   const char *loc_charset;
     91   gboolean is_UTF8;
     92 
     93   if (NULL == str_loc)
     94     return NULL;
     95 
     96   is_UTF8 = g_get_charset (&loc_charset);
     97   if (is_UTF8)
     98     str_utf8 = GNUNET_strdup (str_loc);
     99   else
    100     str_utf8 = GNUNET_STRINGS_to_utf8 (str_loc,
    101                                        strlen (str_loc),
    102                                        loc_charset);
    103   return str_utf8;
    104 }
    105 
    106 
    107 /**
    108  * Convert from locale used for filenames to UTF-8.
    109  *
    110  * @param filename filename in locale encoding
    111  * @return filename in utf-8 encoding
    112  */
    113 static char *
    114 from_filename_to_utf8 (gchar *filename)
    115 {
    116   char *str_utf8;
    117   const char *filename_charset;
    118   gboolean is_UTF8;
    119 
    120   if (NULL == filename)
    121     return NULL;
    122 
    123   is_UTF8 = get_filename_charset (&filename_charset);
    124   if (is_UTF8)
    125     str_utf8 = GNUNET_strdup (filename);
    126   else
    127     str_utf8 =
    128       GNUNET_STRINGS_to_utf8 (filename,
    129                               strlen (filename),
    130                               filename_charset);
    131 
    132   return str_utf8;
    133 }
    134 
    135 
    136 char *
    137 ANASTASIS_GTK_filechooser_get_filename_utf8 (GtkFileChooser *fc)
    138 {
    139   char *filename_utf8;
    140   gchar *filename = gtk_file_chooser_get_filename (fc);
    141 
    142   if (NULL == filename)
    143     return NULL;
    144   filename_utf8 = from_filename_to_utf8 (filename);
    145   g_free (filename);
    146   return filename_utf8;
    147 }
    148 
    149 
    150 /* end of nls.c */