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 it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 # include <vlc_charset.h>
31 #endif
32
vlc_bindtextdomain(const char * domain)33 int vlc_bindtextdomain (const char *domain)
34 {
35 #if defined (ENABLE_NLS)
36 /* Specify where to find the locales for current domain */
37 char *datadir = config_GetDataDir();
38 if (unlikely(datadir == NULL))
39 return -1;
40
41 # if !defined (__APPLE__) && !defined (_WIN32) && !defined(__OS2__)
42 const char *fmt = "%s/../locale";
43 # else
44 const char *fmt = "%s"DIR_SEP"locale";
45 # endif
46 char *upath;
47 int ret = asprintf(&upath, fmt, datadir);
48 free (datadir);
49 if (unlikely(ret == -1))
50 return -1;
51
52 char *lpath = ToLocaleDup (upath);
53 if (lpath == NULL || bindtextdomain (domain, lpath) == NULL)
54 {
55 free (lpath);
56 fprintf (stderr, "%s: text domain not found in %s\n", domain, upath);
57 free (upath);
58 return -1;
59 }
60 free (lpath);
61 free (upath);
62
63 /* LibVLC wants all messages in UTF-8.
64 * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
65 * and other functions that are not part of our text domain.
66 */
67 if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
68 {
69 fprintf (stderr, "%s: UTF-8 encoding not available\n", domain);
70 // Unbinds the text domain to avoid broken encoding
71 bindtextdomain (PACKAGE_NAME, "/DOES_NOT_EXIST");
72 return -1;
73 }
74
75 /* LibVLC does NOT set the default textdomain, since it is a library.
76 * This could otherwise break programs using LibVLC (other than VLC).
77 * textdomain (PACKAGE_NAME);
78 */
79
80 #else /* !ENABLE_NLS */
81 (void)domain;
82 #endif
83
84 return 0;
85 }
86
87 /**
88 * In-tree plugins share their gettext domain with LibVLC.
89 */
vlc_gettext(const char * msgid)90 char *vlc_gettext (const char *msgid)
91 {
92 #ifdef ENABLE_NLS
93 if (likely(*msgid))
94 return dgettext (PACKAGE_NAME, msgid);
95 #endif
96 return (char *)msgid;
97 }
98
vlc_ngettext(const char * msgid,const char * plural,unsigned long n)99 char *vlc_ngettext (const char *msgid, const char *plural, unsigned long n)
100 {
101 #ifdef ENABLE_NLS
102 if (likely(*msgid))
103 return dngettext (PACKAGE_NAME, msgid, plural, n);
104 #endif
105 return (char *)((n == 1) ? msgid : plural);
106 }
107