On the user's first visit, default language from HTTP_ACCEPT_LANGUAGE
[gitphp.git] / include / Resource.class.php
blob:a/include/Resource.class.php -> blob:b/include/Resource.class.php
--- a/include/Resource.class.php
+++ b/include/Resource.class.php
@@ -12,13 +12,15 @@
 require_once(GITPHP_BASEDIR . 'lib/php-gettext/streams.php');
 require_once(GITPHP_BASEDIR . 'lib/php-gettext/gettext.php');
 
+/**
+ * Constant of the locale cookie in the user's browser
+ */
 define('GITPHP_LOCALE_COOKIE', 'GitPHPLocale');
 
 /**
- * Resource
+ * Resource factory class
  *
  * @package GitPHP
- * @subpackage Resource
  */
 class GitPHP_Resource
 {
@@ -164,31 +166,49 @@
 	 *
 	 * @access public
 	 * @static
-	 * @param array locales array of locale preferences
+	 * @param string $httpAcceptLang HTTP Accept-Language string
 	 * @return string matching locale if found
 	 */
-	public static function FindPreferredLocale($locales)
-	{
-		if (count($locales) < 1) {
+	public static function FindPreferredLocale($httpAcceptLang)
+	{
+		if (empty($httpAcceptLang))
 			return '';
-		}
+
+		$locales = explode(',', $httpAcceptLang);
+
+		$localePref = array();
+
+		foreach ($locales as $locale) {
+			$quality = '1.0';
+			$localeData = explode(';', trim($locale));
+			if (count($localeData) > 1) {
+				$q = trim($localeData[1]);
+				if (substr($q, 0, 2) == 'q=') {
+					$quality = substr($q, 2);
+				}
+			}
+			$localePref[$quality][] = trim($localeData[0]);
+		}
+		krsort($localePref);
 
 		$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;
+		foreach ($localePref as $quality => $qualityArray) {
+			foreach ($qualityArray as $browserLocale) {
+				$locale = str_replace('-', '_', $browserLocale);
+				$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;
+					}
 				}
 			}
 		}
@@ -224,3 +244,35 @@
 
 }
 
+
+/**
+ * Gettext wrapper function for readability, single string
+ *
+ * @param string $str string to translate
+ * @return string translated string
+ */
+function __($str)
+{
+	if (GitPHP_Resource::Instantiated())
+		return GitPHP_Resource::GetInstance()->translate($str);
+	return $str;
+}
+
+/**
+ * Gettext wrapper function for readability, plural form
+ *
+ * @param string $singular singular form of string
+ * @param string $plural plural form of string
+ * @param int $count number of items
+ * @return string translated string
+ */
+function __n($singular, $plural, $count)
+{
+	if (GitPHP_Resource::Instantiated())
+		return GitPHP_Resource::GetInstance()->ngettext($singular, $plural, $count);
+	if ($count > 1)
+		return $plural;
+	return $singular;
+}
+
+

comments