aboutsummaryrefslogtreecommitdiff
path: root/src/examples/msgs_i18n.c
blob: 5d563e4f2998b8c6360eda3afbeaf28f3a72d7d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
     This file is part of libmicrohttpd
     Copyright (C) 2017 Christian Grothoff, Silvio Clecio (silvioprog)

     This library 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.

     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.

     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * @file msgs_i18n.c
 * @brief example for how to use translate libmicrohttpd messages
 * @author Christian Grothoff
 * @author Silvio Clecio (silvioprog)
 */

/*
 * supposing you are in Brazil:
 *
 * # generate the PO file
 * $ msginit --input=po/libmicrohttpd.pot --locale=pt_BR --output=libmicrohttpd.po
 * # open the generated .po in any program like Poedit and translate the MHD messages; once done, let's go to the test:
 * mkdir -p src/examples/locale/pt_BR/LC_MESSAGES
 * mv libmicrohttpd.mo libmicrohttpd.po src/examples/locale/pt_BR/LC_MESSAGES
 * cd src/examples/
 * gcc -o msgs_i18n msgs_i18n.c -lmicrohttpd
 * export LANGUAGE=pt_BR
 * ./msgs_i18n
 * # it may print: Opção inválida 4196490! (Você terminou a lista com MHD_OPTION_END?)
 */
#include <stdio.h>
#include <locale.h>
#include <libintl.h>
#include <microhttpd.h>


static int
ahc_echo (void *cls,
          struct MHD_Connection *cnc,
          const char *url,
          const char *mt,
          const char *ver,
          const char *upd,
          size_t *upsz,
          void **req_cls)
{
  return MHD_NO;
}


static void
error_handler (void *cls,
               const char *fm,
               va_list ap)
{
  /* Here we do the translation using GNU gettext.
     As the error message is from libmicrohttpd, we specify
     "libmicrohttpd" as the translation domain here. */
  vprintf (dgettext ("libmicrohttpd",
                     fm),
           ap);
}


int
main (int argc,
      char **argv)
{
  setlocale (LC_ALL, "");

  /* The example uses PO files in the directory
     "libmicrohttpd/src/examples/locale".  This
     needs to be adapted to match
     where the MHD PO files are installed. */
  bindtextdomain ("libmicrohttpd",
                  "locale");
  MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_FEATURE_MESSAGES
                    | MHD_USE_ERROR_LOG,
                    8080,
                    NULL, NULL,
                    &ahc_echo, NULL,
                    MHD_OPTION_EXTERNAL_LOGGER, &error_handler, NULL,
                    99999 /* invalid option, to raise the error
           "Invalid option ..." which we are going
           to translate */);
  return 1; /* This program won't "succeed"... */
}