On the user's first visit, default language from HTTP_ACCEPT_LANGUAGE
On the user's first visit, default language from HTTP_ACCEPT_LANGUAGE

On the very first visit when no language preference cookie is set,
attempt to guess what the user wants from their HTTP_ACCEPT_LANGUAGE
setting from their browser. (Their OS language)
This will attempt to match the preferences in the order they're
specified by the browser, which should be the user's order of
preference.
For full language + country code preferences (eg en_US) it
will attempt to match the whole thing. For just language preferences
(eg en) it will match the first locale with that same language code.

--- a/include/Resource.class.php
+++ b/include/Resource.class.php
@@ -158,6 +158,44 @@
 	}
 
 	/**
+	 * FindPreferredLocale
+	 *
+	 * Given a list of preferred locales, try to find a matching supported locale
+	 *
+	 * @access public
+	 * @static
+	 * @param array locales array of locale preferences
+	 * @return string matching locale if found
+	 */
+	public static function FindPreferredLocale($locales)
+	{
+		if (count($locales) < 1) {
+			return '';
+		}
+
+		$supportedLocales = GitPHP_Resource::SupportedLocales();
+
+		for ($i = 0; $i < count($locales); ++$i) {
+			$locale = str_replace('-', '_', $locales[$i]);
+			$loclen = strlen($locale);
+
+			foreach ($supportedLocales as $l => $lang) {
+				/* 
+				 * using strncasecmp with length of the preferred
+				 * locale means we can match both full
+				 * language + country preference specifications
+				 * (en_US) as well as just language specifications
+				 * (en)
+				 */
+				if (strncasecmp($locale, $l, $loclen) === 0) {
+					return $l;
+				}
+			}
+		}
+		return '';
+	}
+
+	/**
 	 * Instantiate
 	 *
 	 * Instantiates the singleton instance

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -38,20 +38,48 @@
 
 date_default_timezone_set('UTC');
 
+
 /*
  * Set the locale based on the user's preference
  */
-if (isset($_GET['l']) && !empty($_GET['l'])) {
+if ((!isset($_COOKIE[GITPHP_LOCALE_COOKIE])) || empty($_COOKIE[GITPHP_LOCALE_COOKIE])) {
+
+	/*
+	 * User's first time here, try by HTTP_ACCEPT_LANGUAGE
+	 */
+	if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+		$httpAcceptLang = explode(',', strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'], ';'));
+		$preferredLocale = GitPHP_Resource::FindPreferredLocale($httpAcceptLang);
+		if (!empty($preferredLocale)) {
+			setcookie(GITPHP_LOCALE_COOKIE, $preferredLocale);
+			GitPHP_Resource::Instantiate($preferredLocale);
+		}
+	}
+
+	if (!GitPHP_Resource::Instantiated()) {
+		/*
+		 * Create a dummy cookie to prevent browser delay
+		 */
+		setcookie(GITPHP_LOCALE_COOKIE, 0);
+	}
+
+} else if (isset($_GET['l']) && !empty($_GET['l'])) {
+
+	/*
+	 * User picked something
+	 */
 	setcookie(GITPHP_LOCALE_COOKIE, $_GET['l']);
 	GitPHP_Resource::Instantiate($_GET['l']);
+
 } else if (isset($_COOKIE[GITPHP_LOCALE_COOKIE]) && !empty($_COOKIE[GITPHP_LOCALE_COOKIE])) {
+
+	/*
+	 * Returning visitor with a preference
+	 */
 	GitPHP_Resource::Instantiate($_COOKIE[GITPHP_LOCALE_COOKIE]);
-} else {
-	/*
-	 * Create the cookie to prevent browser delay
-	 */
-	setcookie(GITPHP_LOCALE_COOKIE, 0);
+
 }
+
 
 try {
 

comments