libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

msgs_i18n.c (3100B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2017 Christian Grothoff, Silvio Clecio (silvioprog)
      4 
      5      This library is free software; you can redistribute it and/or
      6      modify it under the terms of the GNU Lesser General Public
      7      License as published by the Free Software Foundation; either
      8      version 2.1 of the License, or (at your option) any later version.
      9 
     10      This library is distributed in the hope that it will be useful,
     11      but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      Lesser General Public License for more details.
     14 
     15      You should have received a copy of the GNU Lesser General Public
     16      License along with this library; if not, write to the Free Software
     17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     18 */
     19 /**
     20  * @file msgs_i18n.c
     21  * @brief example for how to use translate libmicrohttpd messages
     22  * @author Christian Grothoff
     23  * @author Silvio Clecio (silvioprog)
     24  */
     25 
     26 /*
     27  * supposing you are in Brazil:
     28  *
     29  * # generate the PO file
     30  * $ msginit --input=po/libmicrohttpd.pot --locale=pt_BR --output=libmicrohttpd.po
     31  * # open the generated .po in any program like Poedit and translate the MHD messages; once done, let's go to the test:
     32  * mkdir -p src/examples/locale/pt_BR/LC_MESSAGES
     33  * mv libmicrohttpd.mo libmicrohttpd.po src/examples/locale/pt_BR/LC_MESSAGES
     34  * cd src/examples/
     35  * gcc -o msgs_i18n msgs_i18n.c -lmicrohttpd
     36  * export LANGUAGE=pt_BR
     37  * ./msgs_i18n
     38  * # it may print: Opção inválida 4196490! (Você terminou a lista com MHD_OPTION_END?)
     39  */
     40 #include <stdio.h>
     41 #include <locale.h>
     42 #include <libintl.h>
     43 #include <microhttpd.h>
     44 
     45 
     46 static int
     47 ahc_echo (void *cls,
     48           struct MHD_Connection *cnc,
     49           const char *url,
     50           const char *mt,
     51           const char *ver,
     52           const char *upd,
     53           size_t *upsz,
     54           void **req_cls)
     55 {
     56   return MHD_NO;
     57 }
     58 
     59 
     60 static void
     61 error_handler (void *cls,
     62                const char *fm,
     63                va_list ap)
     64 {
     65   /* Here we do the translation using GNU gettext.
     66      As the error message is from libmicrohttpd, we specify
     67      "libmicrohttpd" as the translation domain here. */
     68   vprintf (dgettext ("libmicrohttpd",
     69                      fm),
     70            ap);
     71 }
     72 
     73 
     74 int
     75 main (int argc,
     76       char **argv)
     77 {
     78   setlocale (LC_ALL, "");
     79 
     80   /* The example uses PO files in the directory
     81      "libmicrohttpd/src/examples/locale".  This
     82      needs to be adapted to match
     83      where the MHD PO files are installed. */
     84   bindtextdomain ("libmicrohttpd",
     85                   "locale");
     86   MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_FEATURE_MESSAGES
     87                     | MHD_USE_ERROR_LOG,
     88                     8080,
     89                     NULL, NULL,
     90                     &ahc_echo, NULL,
     91                     MHD_OPTION_EXTERNAL_LOGGER, &error_handler, NULL,
     92                     99999 /* invalid option, to raise the error
     93            "Invalid option ..." which we are going
     94            to translate */);
     95   return 1; /* This program won't "succeed"... */
     96 }