]> git.sesse.net Git - vlc/blob - src/modules/textdomain.c
130f1aef9e8602d2b0679076b2f34150a5bd4b60
[vlc] / src / modules / textdomain.c
1 /*****************************************************************************
2  * textdomain.c : Modules text domain management
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include "modules/modules.h"
27
28 #ifdef ENABLE_NLS
29 # include <libintl.h>
30 # if defined (__APPLE__) || defined (WIN32) || defined(__OS2__)
31 #  include "config/configuration.h"
32 #  include <vlc_charset.h>
33 # endif
34 #endif
35
36 int vlc_bindtextdomain (const char *domain)
37 {
38 #if defined (ENABLE_NLS)
39     /* Specify where to find the locales for current domain */
40 # if !defined (__APPLE__) && !defined (WIN32) && !defined(__OS2__)
41     static const char path[] = LOCALEDIR;
42
43     if (bindtextdomain (domain, path) == NULL)
44     {
45         fprintf (stderr, "%s: text domain not found in %s\n", domain, path);
46         return -1;
47     }
48 # else
49     char *datadir = config_GetDataDirDefault();
50     if (unlikely(datadir == NULL))
51         return -1;
52
53     char *upath;
54     int ret = asprintf (&upath, "%s" DIR_SEP "locale", datadir);
55     free (datadir);
56     if (unlikely(ret == -1))
57         return -1;
58
59     char *lpath = ToLocaleDup (upath);
60     if (lpath == NULL || bindtextdomain (domain, lpath) == NULL)
61     {
62         free (lpath);
63         fprintf (stderr, "%s: text domain not found in %s\n", domain, upath);
64         free (upath);
65         return -1;
66     }
67     free (lpath);
68     free (upath);
69 # endif
70
71     /* LibVLC wants all messages in UTF-8.
72      * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
73      * and other functions that are not part of our text domain.
74      */
75     if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
76     {
77         fprintf (stderr, "%s: UTF-8 encoding bot available\n", domain);
78         // Unbinds the text domain to avoid broken encoding
79         bindtextdomain (PACKAGE_NAME, "/DOES_NOT_EXIST");
80         return -1;
81     }
82
83     /* LibVLC does NOT set the default textdomain, since it is a library.
84      * This could otherwise break programs using LibVLC (other than VLC).
85      * textdomain (PACKAGE_NAME);
86      */
87
88 #else /* !ENABLE_NLS */
89     (void)domain;
90 #endif
91
92     return 0;
93 }
94
95 /**
96  * In-tree plugins share their gettext domain with LibVLC.
97  */
98 char *vlc_gettext (const char *msgid)
99 {
100 #ifdef ENABLE_NLS
101     if (unlikely(!*msgid))
102         return (char *)"";
103     return dgettext (PACKAGE_NAME, msgid);
104 #else
105     return (char *)msgid;
106 #endif
107 }