diff options
Diffstat (limited to 'src/examples/msgs_i18n.c')
-rwxr-xr-x | src/examples/msgs_i18n.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/examples/msgs_i18n.c b/src/examples/msgs_i18n.c new file mode 100755 index 00000000..ed23d982 --- /dev/null +++ b/src/examples/msgs_i18n.c | |||
@@ -0,0 +1,91 @@ | |||
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, Silvio Clecio (silvioprog) | ||
23 | */ | ||
24 | |||
25 | /* | ||
26 | * suposing you are in the libmicrohttp root directory and the LANG is `pt_BR`: * | ||
27 | * | ||
28 | * # generate the POT file | ||
29 | * $ xgettext --keyword=_ --language=C --add-comments --sort-output -o libmicrohttpd.pot src/microhttpd/*.c | ||
30 | * | ||
31 | * # generate the PO file | ||
32 | * $ msginit --input=libmicrohttpd.pot --locale=pt_BR --output=libmicrohttpd.po | ||
33 | * | ||
34 | * # open the generated .po in any program like Poedit and translate the MHD messages; once done, let's go to the test: | ||
35 | * mkdir -p src/examples/locale/pt_BR/LC_MESSAGES | ||
36 | * mv libmicrohttpd.mo libmicrohttpd.po src/examples/locale/pt_BR/LC_MESSAGES | ||
37 | * cd src/examples/ | ||
38 | * gcc -o msgs_i18n msgs_i18n.c -lmicrohttpd | ||
39 | * export LANGUAGE=pt_BR | ||
40 | * ./msgs_i18n | ||
41 | * # it may print: Opção inválida 4196490! (Você terminou a lista com MHD_OPTION_END?) | ||
42 | * | ||
43 | * # tip: if you get any problem in your i18n application, you can debug it using `strace` tool, e.g: | ||
44 | * $ strace -e trace=open ./msgs_i18n | ||
45 | * | ||
46 | * That's all! | ||
47 | * | ||
48 | */ | ||
49 | |||
50 | #include <stdio.h> | ||
51 | #include <locale.h> | ||
52 | #include <libintl.h> | ||
53 | #include <microhttpd.h> | ||
54 | |||
55 | #ifndef _ | ||
56 | #define _(fm) dgettext("libmicrohttpd", fm) | ||
57 | #endif | ||
58 | |||
59 | static int | ||
60 | ahc_echo(void *cls, | ||
61 | struct MHD_Connection *cnc, | ||
62 | const char *url, | ||
63 | const char *mt, | ||
64 | const char *ver, | ||
65 | const char *upd, | ||
66 | size_t *upsz, | ||
67 | void **ptr) { | ||
68 | return MHD_NO; | ||
69 | } | ||
70 | |||
71 | static void | ||
72 | error_handler(void *cls, | ||
73 | const char *fm, | ||
74 | va_list ap) { | ||
75 | vprintf(_(fm), ap); | ||
76 | } | ||
77 | |||
78 | int | ||
79 | main() { | ||
80 | setlocale(LC_ALL, ""); | ||
81 | /* notice I'm using PO files in the directory "libmicrohttpd/src/examples/locale", please change it matching | ||
82 | * where the MHD PO files are installed */ | ||
83 | bindtextdomain("libmicrohttpd", "locale"); | ||
84 | MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_FEATURE_MESSAGES | MHD_USE_ERROR_LOG, | ||
85 | 8080, | ||
86 | NULL, NULL, | ||
87 | &ahc_echo, NULL, | ||
88 | MHD_OPTION_EXTERNAL_LOGGER, &error_handler, NULL | ||
89 | /* MHD_OPTION_END - to raise the error "Invalid option ..." we are going to translate */); | ||
90 | return 1; /* purposely */ | ||
91 | } | ||