[Xfce4-commits] <www:nick/gettext> Add language detection.

Nick Schermer noreply at xfce.org
Tue Nov 23 22:08:02 CET 2010


Updating branch refs/heads/nick/gettext
         to cfe46342518cfa950de573c79149119eb7b9de6a (commit)
       from 81667826f0a270d0e919b63c9a59b6b2dcc22cfc (commit)

commit cfe46342518cfa950de573c79149119eb7b9de6a
Author: Nick Schermer <nick at xfce.org>
Date:   Tue Nov 23 20:03:13 2010 +0100

    Add language detection.

 index.php              |   11 +++++
 lib/core.php           |  106 +++++++++++++++++++++++++++++++++++++++++++-----
 lib/locale/locales.php |   17 ++++++++
 3 files changed, 124 insertions(+), 10 deletions(-)

diff --git a/index.php b/index.php
index 81e1fc1..4390ad3 100644
--- a/index.php
+++ b/index.php
@@ -2,6 +2,14 @@
 
 error_reporting(E_ALL | E_STRICT);
 
+function microtime_float ()
+{
+  list ($usec, $sec) = explode(" ", microtime());
+  return ((float)$usec + (float)$sec);
+}
+
+$timer_start = microtime_float ();
+
 include ('lib/core.php');
 
 $uri_a = explode('?', $_SERVER['REQUEST_URI']);
@@ -11,4 +19,7 @@ include ('pages/header.php');
 include (lookup_page($uri));
 include ('pages/footer.php');
 
+$timer_end = microtime_float ();
+echo "<!-- Execution time: ". round ($timer_end - $timer_start, 4) ." second -->";
+
 ?>
diff --git a/lib/core.php b/lib/core.php
index 62cfbde..a8a33de 100644
--- a/lib/core.php
+++ b/lib/core.php
@@ -2,18 +2,13 @@
 
 include ('streams.php');
 include ('gettext.php');
+include ('locale/locales.php');
 
-$gettext_reader = null;
+/* load session variables */
+session_start ();
 
-if (!empty($_GET['lang']))
-{
-	$path = './lib/locale/'. $_GET['lang'] .'/LC_MESSAGES/www.mo';
-	if (is_file ($path))
-	{
-		$file_reader = new FileReader($path);
-		$gettext_reader = new gettext_reader($file_reader);
-	}
-}
+/* shared translation reader, loaded at buttom of this file */
+$gettext_reader = null;
 
 /* return translated string */
 function R_($text)
@@ -31,6 +26,7 @@ function E_($text)
 	echo R_($text);
 }
 
+/* lookup the coresponding page for an uri */
 function lookup_page($uri)
 {
 	$root = 'pages/';
@@ -45,7 +41,97 @@ function lookup_page($uri)
 	if (is_file ($file))
 		return $file;
 
+	header ('HTTP/1.0 404 Not Found');
 	return $root.'404.php';
 }
 
+/* lookup a key from the user in this order: get value, session value or cookie */
+function lookup_key ($key, $valid_values, $default = null)
+{
+	/* try to get value from url */
+	if (isset ($_GET[$key]))
+	{
+		$value = $_GET[$key];
+		if (in_array ($value, $valid_values))
+		{
+			/* store 1 year */
+			$expire = time() + (60*60*24*365);
+			setcookie ($key, $value, $expire, "/");
+
+			/* store in session variable */
+			$_SESSION[$key] = $value;
+
+			return $value;
+		}
+	}
+
+	/* try to read from session */
+	if (isset ($_SESSION[$key]))
+	{
+		$value = $_SESSION[$key];
+		if (in_array ($value, $valid_values))
+			return $value;
+	}
+
+	/* try cookie */
+	if (isset ($_COOKIE[$key]))
+	{
+		$value = $_COOKIE[$key];
+		if (in_array ($value, $valid_values))
+		{
+			/* store in session variable */
+			$_SESSION[$key] = $value;
+
+			return $value;
+		}
+	}
+
+	/* return default value */
+	return $default;
+}
+
+function lookup_user_language ($valid_values, $default = 'en')
+{
+	$accept = strtolower ($_SERVER['HTTP_ACCEPT_LANGUAGE']);
+	if (empty ($accept))
+		return $default;
+
+	foreach (explode (',', $accept) as $lang)
+	{
+		/* check if we have a match for the full language, ie pt-br */
+		$code = explode (';', $lang, 2); /* explode code and quantifier */
+		$primarytag = str_replace ('-', '_', $code[0]);
+		if (in_array (trim ($primarytag), $valid_values))
+			return $primarytag;
+
+		/* check if we have a match for the sub language, ie pt */
+		$subtag = explode ('-', $code[0]);
+		if (in_array (trim ($subtag[0]), $valid_values))
+			return $subtag[0];
+	}
+
+	return $default;
+}
+
+/* if no language if found, lookup the accepted browser languages */
+$lang = lookup_key ('lang', $language_codes);
+if (empty ($lang))
+{
+	$lang = lookup_user_language ($language_codes);
+
+	/* store result in session, so we don't check this all the time */
+	$_SESSION['lang'] = $lang;
+}
+
+/* load the gettext translation */
+if (!empty($lang))
+{
+	$path = './lib/locale/'. $lang .'/LC_MESSAGES/www.mo';
+	if (is_file ($path))
+	{
+		$file_reader = new FileReader($path);
+		$gettext_reader = new gettext_reader($file_reader);
+	}
+}
+
 ?>
diff --git a/lib/locale/locales.php b/lib/locale/locales.php
new file mode 100644
index 0000000..5378f89
--- /dev/null
+++ b/lib/locale/locales.php
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * This file is generated by the compile.sh script.
+ * Do not modify it by hand!
+ **/
+
+/* known languages in this directory */
+$languages = array (
+	'nl' => 'Nederlands',
+	'da' => 'Deens',
+	'pt' => 'Purtugees',
+	'pt_br' => 'Portugees (Braziliaans)' );
+	
+/* used in header and language detection functions */
+$language_codes = array_keys ($languages);
+?>



More information about the Xfce4-commits mailing list