]> git.sesse.net Git - vlc/blob - src/modules/textdomain.c
Factor out LoadMessages to support other text domains
[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 #endif
31
32 int vlc_bindtextdomain (const char *domain)
33 {
34     int ret = 0;
35
36 #if defined (ENABLE_NLS)
37     /* Specify where to find the locales for current domain */
38 # if !defined (__APPLE__) && !defined ( IN32)
39     static const char path[] = LOCALEDIR;
40 # else
41     char *datadir = config_GetDataDirDefault();
42     char *path;
43     int ret;
44
45     if (unlikely(datadir == NULL))
46         return -1;
47     int ret = asprintf (&path, "%s" DIR_SEP "locale", datadir);
48     free (datadir);
49 # endif
50
51     if (bindtextdomain (domain, path) == NULL)
52     {
53         fprintf (stderr, "%s: text domain not found in %s\n", domain, path);
54         ret = -1;
55         goto out;
56     }
57
58     /* LibVLC wants all messages in UTF-8.
59      * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
60      * and other functions that are not part of our text domain.
61      */
62     if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
63     {
64         fprintf (stderr, "%s: UTF-8 encoding bot available\n", domain);
65         // Unbinds the text domain to avoid broken encoding
66         bindtextdomain (PACKAGE_NAME, "/DOES_NOT_EXIST");
67         ret = -1;
68         goto out;
69     }
70
71     /* LibVLC does NOT set the default textdomain, since it is a library.
72      * This could otherwise break programs using LibVLC (other than VLC).
73      * textdomain (PACKAGE_NAME);
74      */
75 out:
76 # if defined (__APPLE__) || defined (WIN32)
77     free (path);
78 # endif
79
80 #else /* !ENABLE_NLS */
81     (void)domain;
82 #endif
83
84     return ret;
85 }
86
87