summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDiogo Sousa <diogogsousa@gmail.com>2013-06-08 02:34:01 +0100
committerDiogo Sousa <diogogsousa@gmail.com>2013-06-08 02:34:01 +0100
commit473ab1673921efa011920e7979cc7e9b7a08f304 (patch)
treee3a5d822eda947f9a5104e6160b56f0ba73ab309
parent951292abe1ce97751b80e62b69623d9f712a0914 (diff)
Now the default configuration tries to set the language from the system
locale, and defaults to english if no language match.

This closes #473.
-rw-r--r--src/common/cfgfiles.c65
-rw-r--r--src/common/cfgfiles.h4
-rw-r--r--src/common/hexchat.c12
3 files changed, 70 insertions, 11 deletions
diff --git a/src/common/cfgfiles.c b/src/common/cfgfiles.c
index 219edc85..cb877cfb 100644
--- a/src/common/cfgfiles.c
+++ b/src/common/cfgfiles.c
@@ -41,6 +41,15 @@
 #define DEF_FONT "Monospace 9"
 #define DEF_FONT_ALTER "Arial Unicode MS,Lucida Sans Unicode,MS Gothic,Unifont"
 
+const char const *languages[LANGUAGES_LENGTH] = {
+	"af", "sq", "am", "ast", "az", "eu", "be", "bg", "ca", "zh_CN",   /*  0 ..  9 */
+	"zh_TW", "cs", "da", "nl", "en_GB", "en", "et", "fi", "fr", "gl", /* 10 .. 19 */
+	"de", "el", "gu", "hi", "hu", "id", "it", "ja", "kn", "rw",       /* 20 .. 29 */
+	"ko", "lv", "lt", "mk", "ml", "ms", "nb", "no", "pl", "pt",       /* 30 .. 39 */
+	"pt_BR", "pa", "ru", "sr", "sk", "sl", "es", "sv", "th", "uk",    /* 40 .. 49 */
+	"vi", "wa"                                                        /* 50 .. */
+};
+
 void
 list_addentry (GSList ** list, char *cmd, char *name)
 {
@@ -605,6 +614,60 @@ convert_with_fallback (const char *str, const char *fallback)
 	return utf;
 }
 
+static int
+find_language_number (const char const *lang)
+{
+	int i;
+
+	for (i = 0; i < LANGUAGES_LENGTH; i++)
+		if (!strcmp (lang, languages[i]))
+			return i;
+
+	return -1;
+}
+
+/* Return the number of the system language if found, or english otherwise.
+ */
+static int
+get_default_language (void)
+{
+	const char *locale;
+	char *lang;
+	char *p;
+	int lang_no;
+
+	/* LC_ALL overrides LANG, so we must check it first */
+	locale = g_getenv ("LC_ALL");
+
+	if (!locale)
+		locale = g_getenv ("LANG") ? g_getenv ("LANG") : "en";
+
+	/* we might end up with something like "en_US.UTF-8".  We will try to 
+	 * search for "en_US"; if it fails we search for "en".
+	 */
+	lang = g_strdup (locale);
+
+	if ((p = strchr (lang, '.')))
+		*p='\0';
+
+	lang_no = find_language_number (lang);
+
+	if (lang_no >= 0)
+	{
+		free(lang);
+		return lang_no;
+	}
+
+	if ((p = strchr (lang, '_')))
+		*p='\0';
+
+	lang_no = find_language_number (lang);
+
+	free(lang);
+
+	return lang_no >= 0 ? lang_no : find_language_number ("en");
+}
+
 void
 load_default_config(void)
 {
@@ -704,7 +767,7 @@ load_default_config(void)
 	prefs.hex_gui_dialog_height = 256;
 	prefs.hex_gui_dialog_width = 500;
 	prefs.hex_gui_lagometer = 1;
-	prefs.hex_gui_lang = 15;
+	prefs.hex_gui_lang = get_default_language();
 	prefs.hex_gui_pane_left_size = 128;		/* with treeview icons we need a bit bigger space */
 	prefs.hex_gui_pane_right_size = 100;
 	prefs.hex_gui_pane_right_size_min = 80;
diff --git a/src/common/cfgfiles.h b/src/common/cfgfiles.h
index 3a59d26d..83db9656 100644
--- a/src/common/cfgfiles.h
+++ b/src/common/cfgfiles.h
@@ -24,7 +24,10 @@
 
 #include "hexchat.h"
 
+#define LANGUAGES_LENGTH 52
+
 extern char *xdir;
+extern const char const *languages[LANGUAGES_LENGTH];
 
 char *cfg_get_str (char *cfg, const char *var, char *dest, int dest_len);
 int cfg_get_bool (char *var);
@@ -47,6 +50,7 @@ void list_addentry (GSList ** list, char *cmd, char *name);
 int cmd_set (session *sess, char *tbuf, char *word[], char *word_eol[]);
 int hexchat_open_file (char *file, int flags, int mode, int xof_flags);
 FILE *hexchat_fopen_file (const char *file, const char *mode, int xof_flags);
+
 #define XOF_DOMODE 1
 #define XOF_FULLPATH 2
 
diff --git a/src/common/hexchat.c b/src/common/hexchat.c
index fb9dde5c..aece9ff3 100644
--- a/src/common/hexchat.c
+++ b/src/common/hexchat.c
@@ -1017,20 +1017,12 @@ static void
 set_locale (void)
 {
 #ifdef WIN32
-	const char const *langs[]={
-		"af", "sq", "am", "ast", "az", "eu", "be", "bg", "ca", "zh_CN",   /* 0 .. 9   */
-		"zh_TW", "cs", "da", "nl", "en_GB", "en", "et", "fi", "fr", "gl", /* 10 .. 19 */
-		"de", "el", "gu", "hi", "hu", "id", "it", "ja", "kn", "rw",       /* 20 .. 29 */
-		"ko", "lv", "lt", "mk", "ml", "ms", "nb", "no", "pl", "pt",       /* 30 .. 39 */
-		"pt_BR", "pa", "ru", "sr", "sk", "sl", "es", "sv", "th", "uk",    /* 40 .. 49 */
-		"vi", "wa"                                                        /* 50 .. */
-	};
 	char hexchat_lang[13];	/* LC_ALL= plus 5 chars of hex_gui_lang and trailing \0 */
 
 	strcpy (hexchat_lang, "LC_ALL=");
 
-	if (0 <= prefs.hex_gui_lang && prefs.hex_gui_lang < sizeof(langs)/sizeof(*langs))
-		strcat (hexchat_lang, langs[prefs.hex_gui_lang]);
+	if (0 <= prefs.hex_gui_lang && prefs.hex_gui_lang < LANGUAGES_LENGTH)
+		strcat (hexchat_lang, languages[prefs.hex_gui_lang]);
 	else
 		strcat (hexchat_lang, "en");