i18n improvements

Benedikt Meurer Benedikt.Meurer at unix-ag.uni-siegen.de
Tue Jun 3 20:19:30 CEST 2003


Hello everybody,

As I've mentioned some time ago, I am working on improving the i18n
interface in libxfce4util. The new function xfce_get_path_localized()
was inspired by catopen() and XmGetPixmap(), and gives a very
flexible interface to localize paths.

I've attached a current patch, and would like to have the other developers
(esp. Jasper, since he wanted this functionality for the panel) to
take a look at the code. In addition theres a simple test program, that
shows how to use the functions.

Benedikt

PS: What about getting a STABLE_4 branch to CVS, so we can do further
development without adding new bugs to the release code?

-- 
NetBSD Operating system:                       http://www.NetBSD.org/
pkgsrc "Work in progress":                  http://pkgsrc-wip.sf.net/
XFce desktop environment:                        http://www.xfce.org/
German Unix-AG Association:                   http://www.unix-ag.org/
-------------- next part --------------
Index: i18n.c
===================================================================
RCS file: /cvsroot/xfce/xfce-devel/libxfce4util/libxfce4util/i18n.c,v
retrieving revision 1.1
diff -u -r1.1 i18n.c
--- i18n.c	1 May 2003 10:42:32 -0000	1.1
+++ i18n.c	3 Jun 2003 18:10:11 -0000
@@ -79,6 +79,8 @@
 #define strlcpy(x,y,z)	g_strlcpy(x,y,z)
 #endif
 
+#define DEFAULT_LOCALE	"C"
+
 #if defined(__NetBSD__) && defined(HAVE___UNALIASNAME)
 /*
  * NetBSD has the __unaliasname function in -lc
@@ -239,4 +241,103 @@
 
 	return(__localize_path(buffer, length, directory, G_FILE_TEST_IS_DIR));
 }
+
+/*
+ * paths is a ':'-separated list of pathnames.
+ *
+ *	%F	- The filename
+ *	%L	- The language string, as returned by 
+ *		  setlocale(LC_MESSAGES, NULL)
+ *	%l	- The language component of the language string
+ *	%N	- application name
+ *
+ * Example paths:
+ *
+ *	/usr/local/lib/%L/%F:/usr/local/share/%N/%l/%F
+ */
+gchar *
+xfce_get_path_localized(gchar *dst, gsize size, const gchar *paths,
+		        const gchar *filename, GFileTest test)
+{
+	const gchar *f;
+	gchar *dstlast;
+	gchar *d;
+	const gchar *locale;
+	const gchar *lang;
+	gchar langbuf[PATH_MAX];
+
+	g_return_val_if_fail(dst != NULL, NULL);
+	g_return_val_if_fail(size > 2, NULL);
+	g_return_val_if_fail(paths != NULL, NULL);
+
+	d = dst;
+
+	dstlast = dst + (size - 1);
+
+#ifdef HAVE_SETLOCALE
+	if ((locale = setlocale(LC_MESSAGES, NULL)) == NULL)
+#endif
+		if ((locale = g_getenv("LANG")) == NULL)
+			locale = DEFAULT_LOCALE;
+
+	lang = __unaliasname(NLS_ALIAS_DB, locale, langbuf, sizeof(langbuf));
+
+	for (; d < dst + (size - 1); ) {
+		if (*paths == ':' || *paths == '\0') {
+			*d = '\0';
+
+			if (g_file_test(dst, test))
+				return(dst);
+
+			if (*paths == ':') {
+				d = dst;
+				paths++;
+				continue;
+			}
+			break;
+		}
+
+		if (*paths == '%') {
+			if (*(paths + 1) == 'F') {
+				/* 
+				 * if "filename" is NULL, then simply skip
+				 * the %F.
+				 */
+				if ((f = filename) != NULL)
+					while (*f && d < dstlast)
+						*d++ = *f++;
+
+				paths += 2;
+				continue;
+			}
+			else if (*(paths + 1) == 'L') {
+				for (f = locale; *f && d < dstlast; )
+						*d++ = *f++;
+
+				paths += 2;
+				continue;
+			}
+			else if (*(paths + 1) == 'l') {
+				for (f = lang; *f && d < dstlast; )
+					*d++ = *f++;
+
+				paths += 2;
+				continue;
+			}
+			else if (*(paths + 1) == 'N') {
+				if ((f = g_get_prgname()) != NULL) 
+					while (*f && d < dstlast)
+						*d++ = *f++;
+
+				paths += 2;
+				continue;
+			}
+		}
+
+		*d++ = *paths++;
+	}
+
+	return(NULL);
+}
+
 
Index: i18n.h
===================================================================
RCS file: /cvsroot/xfce/xfce-devel/libxfce4util/libxfce4util/i18n.h,v
retrieving revision 1.4
diff -u -r1.4 i18n.h
--- i18n.h	1 May 2003 10:42:32 -0000	1.4
+++ i18n.h	3 Jun 2003 18:10:11 -0000
@@ -66,5 +66,8 @@
 extern gchar *	xfce_get_file_localized_r(gchar *, gsize, const gchar *);
 extern gchar *	xfce_get_dir_localized_r(gchar *, gsize, const gchar *);
 
+/* */
+extern gchar *	xfce_get_path_localized(gchar *, gsize, const gchar *,
+					const gchar *, GFileTest);
 
 #endif	/* !__LIBXFCE4UTIL_I18N_H__ */
-------------- next part --------------
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

#include <glib.h>

#include <libxfce4util/i18n.h>

int
main(int argc, char **argv)
{
	char buffer[2048];
	char *p;

	setlocale(LC_ALL, "");

	if (argc != 2)
		return(1);

	p = xfce_get_path_localized(buffer, sizeof(buffer), argv[1], NULL,
			G_FILE_TEST_IS_REGULAR);

	if (p != NULL)
		printf("%s\n", p);

	return(0);
}



More information about the Xfce4-dev mailing list