[vlc-devel] commit: Factor out LoadMessages to support other text domains ( Rémi Denis-Courmont )

git version control git at videolan.org
Thu Jan 28 19:00:03 CET 2010


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Jan 28 19:48:06 2010 +0200| [46c51df557d7905ab3fbb77a65b5696514cb68d3] | committer: Rémi Denis-Courmont 

Factor out LoadMessages to support other text domains

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=46c51df557d7905ab3fbb77a65b5696514cb68d3
---

 src/Makefile.am          |    1 +
 src/libvlc.c             |   60 +-------------------------------
 src/modules/modules.h    |    2 +
 src/modules/textdomain.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 91 insertions(+), 59 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 53be9c3..5ea0af3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -416,6 +416,7 @@ SOURCES_libvlc_common = \
 	modules/cache.c \
 	modules/entry.c \
 	modules/os.c \
+	modules/textdomain.c \
 	misc/threads.c \
 	misc/stats.c \
 	misc/cpu.c \
diff --git a/src/libvlc.c b/src/libvlc.c
index 1020384..23687ca 100644
--- a/src/libvlc.c
+++ b/src/libvlc.c
@@ -65,10 +65,6 @@
 #   include <locale.h>
 #endif
 
-#ifdef ENABLE_NLS
-# include <libintl.h> /* bindtextdomain */
-#endif
-
 #ifdef HAVE_DBUS
 /* used for one-instance mode */
 #   include <dbus/dbus.h>
@@ -205,11 +201,6 @@ void vlc_release (gc_object_t *p_gc)
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-#if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
-    ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
-static void SetLanguage   ( char const * );
-#endif
-static inline int LoadMessages (void);
 static int  GetFilenames  ( libvlc_int_t *, int, const char *[] );
 static void Help          ( libvlc_int_t *, char const *psz_help_name );
 static void Usage         ( libvlc_int_t *, char const *psz_search );
@@ -313,7 +304,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     /*
      * Support for gettext
      */
-    LoadMessages ();
+    vlc_bindtextdomain (PACKAGE_NAME);
 
     /* Initialize the module bank and load the configuration of the
      * main module. We need to do this at this stage to be able to display
@@ -1198,55 +1189,6 @@ static void SetLanguage ( const char *psz_lang )
 }
 #endif
 
-
-static inline int LoadMessages (void)
-{
-#if defined( ENABLE_NLS ) \
-     && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
-    /* Specify where to find the locales for current domain */
-#if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
-    static const char psz_path[] = LOCALEDIR;
-#else
-    char psz_path[1024];
-    char *datadir = config_GetDataDirDefault();
-    int ret;
-
-    if (unlikely(datadir == NULL))
-        return -1;
-    ret = snprintf (psz_path, sizeof (psz_path), "%s" DIR_SEP "locale",
-                    datadir);
-    free (datadir);
-    if (ret >= (int)sizeof (psz_path))
-        return -1;
-#endif
-    if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
-    {
-        fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
-                         " to directory %s\n", psz_path);
-        return -1;
-    }
-
-    /* LibVLC wants all messages in UTF-8.
-     * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
-     * and other functions that are not part of our text domain.
-     */
-    if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
-    {
-        fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
-                         PACKAGE_NAME"\n");
-        // Unbinds the text domain to avoid broken encoding
-        bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
-        return -1;
-    }
-
-    /* LibVLC does NOT set the default textdomain, since it is a library.
-     * This could otherwise break programs using LibVLC (other than VLC).
-     * textdomain (PACKAGE_NAME);
-     */
-#endif
-    return 0;
-}
-
 /*****************************************************************************
  * GetFilenames: parse command line options which are not flags
  *****************************************************************************
diff --git a/src/modules/modules.h b/src/modules/modules.h
index 88e7624..b9d0b3a 100644
--- a/src/modules/modules.h
+++ b/src/modules/modules.h
@@ -148,6 +148,8 @@ void module_LoadPlugins( vlc_object_t *, bool );
 void module_EndBank( vlc_object_t *, bool );
 #define module_EndBank(a,b) module_EndBank(VLC_OBJECT(a), b)
 
+int vlc_bindtextdomain (const char *);
+
 /* Low-level OS-dependent handler */
 int  module_Load   (vlc_object_t *, const char *, module_handle_t *);
 int  module_Call   (vlc_object_t *obj, module_t *);
diff --git a/src/modules/textdomain.c b/src/modules/textdomain.c
new file mode 100644
index 0000000..847ba2e
--- /dev/null
+++ b/src/modules/textdomain.c
@@ -0,0 +1,87 @@
+/*****************************************************************************
+ * textdomain.c : Modules text domain management
+ *****************************************************************************
+ * Copyright (C) 2010 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include "modules/modules.h"
+
+#ifdef ENABLE_NLS
+# include <libintl.h>
+#endif
+
+int vlc_bindtextdomain (const char *domain)
+{
+    int ret = 0;
+
+#if defined (ENABLE_NLS)
+    /* Specify where to find the locales for current domain */
+# if !defined (__APPLE__) && !defined ( IN32)
+    static const char path[] = LOCALEDIR;
+# else
+    char *datadir = config_GetDataDirDefault();
+    char *path;
+    int ret;
+
+    if (unlikely(datadir == NULL))
+        return -1;
+    int ret = asprintf (&path, "%s" DIR_SEP "locale", datadir);
+    free (datadir);
+# endif
+
+    if (bindtextdomain (domain, path) == NULL)
+    {
+        fprintf (stderr, "%s: text domain not found in %s\n", domain, path);
+        ret = -1;
+        goto out;
+    }
+
+    /* LibVLC wants all messages in UTF-8.
+     * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
+     * and other functions that are not part of our text domain.
+     */
+    if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
+    {
+        fprintf (stderr, "%s: UTF-8 encoding bot available\n", domain);
+        // Unbinds the text domain to avoid broken encoding
+        bindtextdomain (PACKAGE_NAME, "/DOES_NOT_EXIST");
+        ret = -1;
+        goto out;
+    }
+
+    /* LibVLC does NOT set the default textdomain, since it is a library.
+     * This could otherwise break programs using LibVLC (other than VLC).
+     * textdomain (PACKAGE_NAME);
+     */
+out:
+# if defined (__APPLE__) || defined (WIN32)
+    free (path);
+# endif
+
+#else /* !ENABLE_NLS */
+    (void)domain;
+#endif
+
+    return ret;
+}
+
+




More information about the vlc-devel mailing list