Fix resource header, add version header
[gitphp.git] / include / Resource.class.php
blob:a/include/Resource.class.php -> blob:b/include/Resource.class.php
<?php <?php
/** /**
* GitPHP Resource * GitPHP Resource
* *
* Resource factory * Resource factory
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
*/ */
   
require_once(GITPHP_BASEDIR . 'lib/php-gettext/streams.php'); require_once(GITPHP_BASEDIR . 'lib/php-gettext/streams.php');
require_once(GITPHP_BASEDIR . 'lib/php-gettext/gettext.php'); require_once(GITPHP_BASEDIR . 'lib/php-gettext/gettext.php');
   
/** /**
* Constant of the locale cookie in the user's browser * Constant of the locale cookie in the user's browser
*/ */
define('GITPHP_LOCALE_COOKIE', 'GitPHPLocale'); define('GITPHP_LOCALE_COOKIE', 'GitPHPLocale');
   
/** /**
* Resource * Resource factory class
* *
* @package GitPHP * @package GitPHP
* @subpackage Resource  
*/ */
class GitPHP_Resource class GitPHP_Resource
{ {
/** /**
* instance * instance
* *
* Stores the singleton instance of the resource provider * Stores the singleton instance of the resource provider
* *
* @access protected * @access protected
* @static * @static
*/ */
protected static $instance = null; protected static $instance = null;
   
/** /**
* currentLocale * currentLocale
* *
* Stores the currently instantiated locale identifier * Stores the currently instantiated locale identifier
* *
* @access protected * @access protected
* @static * @static
*/ */
protected static $currentLocale = ''; protected static $currentLocale = '';
   
/** /**
* GetInstance * GetInstance
* *
* Returns the singleton instance * Returns the singleton instance
* *
* @access public * @access public
* @static * @static
* @return mixed instance of resource class * @return mixed instance of resource class
*/ */
public static function GetInstance() public static function GetInstance()
{ {
return self::$instance; return self::$instance;
} }
   
/** /**
* Instantiated * Instantiated
* *
* Tests if the resource provider has been instantiated * Tests if the resource provider has been instantiated
* *
* @access public * @access public
* @static * @static
* @return boolean true if resource provider is instantiated * @return boolean true if resource provider is instantiated
*/ */
public static function Instantiated() public static function Instantiated()
{ {
return (self::$instance !== null); return (self::$instance !== null);
} }
   
/** /**
* GetLocale * GetLocale
* *
* Gets the currently instantiated locale * Gets the currently instantiated locale
* *
* @access public * @access public
* @static * @static
* @return string locale identifier * @return string locale identifier
*/ */
public static function GetLocale() public static function GetLocale()
{ {
return self::$currentLocale; return self::$currentLocale;
} }
   
/** /**
* GetLocaleName * GetLocaleName
* *
* Gets the current instantiated locale's name * Gets the current instantiated locale's name
* *
* @access public * @access public
* @static * @static
* @return string locale name * @return string locale name
*/ */
public static function GetLocaleName() public static function GetLocaleName()
{ {
return GitPHP_Resource::LocaleToName(self::$currentLocale); return GitPHP_Resource::LocaleToName(self::$currentLocale);
} }
   
/** /**
* LocaleToName * LocaleToName
* *
* Given a locale, returns a human readable name * Given a locale, returns a human readable name
* *
* @access public * @access public
* @static * @static
* @param string $locale locale * @param string $locale locale
* return string name * return string name
*/ */
public static function LocaleToName($locale) public static function LocaleToName($locale)
{ {
switch ($locale) { switch ($locale) {
case 'en_US': case 'en_US':
return 'English'; return 'English';
case 'fr_FR': case 'fr_FR':
return 'Français'; return 'Français';
case 'zz_Debug': case 'zz_Debug':
return 'Gibberish'; return 'Gibberish';
} }
return ''; return '';
} }
   
/** /**
* SupportedLocales * SupportedLocales
* *
* Gets the list of supported locales and their languages * Gets the list of supported locales and their languages
* *
* @access public * @access public
* @static * @static
* @return array list of locales mapped to languages * @return array list of locales mapped to languages
*/ */
public static function SupportedLocales() public static function SupportedLocales()
{ {
$locales = array(); $locales = array();
   
$locales['en_US'] = GitPHP_Resource::LocaleToName('en_US'); $locales['en_US'] = GitPHP_Resource::LocaleToName('en_US');
   
if ($dh = opendir(GITPHP_LOCALEDIR)) { if ($dh = opendir(GITPHP_LOCALEDIR)) {
while (($file = readdir($dh)) !== false) { while (($file = readdir($dh)) !== false) {
$fullPath = GITPHP_LOCALEDIR . '/' . $file; $fullPath = GITPHP_LOCALEDIR . '/' . $file;
if ((strpos($file, '.') !== 0) && is_dir($fullPath) && is_file($fullPath . '/gitphp.mo')) { if ((strpos($file, '.') !== 0) && is_dir($fullPath) && is_file($fullPath . '/gitphp.mo')) {
if ($file == 'zz_Debug') { if ($file == 'zz_Debug') {
$conf = GitPHP_Config::GetInstance(); $conf = GitPHP_Config::GetInstance();
if ($conf) { if ($conf) {
if (!$conf->GetValue('debug', false)) { if (!$conf->GetValue('debug', false)) {
continue; continue;
} }
} }
} }
$locales[$file] = GitPHP_Resource::LocaleToName($file); $locales[$file] = GitPHP_Resource::LocaleToName($file);
} }
} }
} }
return $locales; return $locales;
} }
   
/** /**
* FindPreferredLocale * FindPreferredLocale
* *
* Given a list of preferred locales, try to find a matching supported locale * Given a list of preferred locales, try to find a matching supported locale
* *
* @access public * @access public
* @static * @static
* @param string $httpAcceptLang HTTP Accept-Language string * @param string $httpAcceptLang HTTP Accept-Language string
* @return string matching locale if found * @return string matching locale if found
*/ */
public static function FindPreferredLocale($httpAcceptLang) public static function FindPreferredLocale($httpAcceptLang)
{ {
if (empty($httpAcceptLang)) if (empty($httpAcceptLang))
return ''; return '';
   
$locales = explode(',', $httpAcceptLang); $locales = explode(',', $httpAcceptLang);
   
$localePref = array(); $localePref = array();
   
foreach ($locales as $locale) { foreach ($locales as $locale) {
$quality = '1.0'; $quality = '1.0';
$localeData = explode(';', trim($locale)); $localeData = explode(';', trim($locale));
if (count($localeData) > 1) { if (count($localeData) > 1) {
$q = trim($localeData[1]); $q = trim($localeData[1]);
if (substr($q, 0, 2) == 'q=') { if (substr($q, 0, 2) == 'q=') {
$quality = substr($q, 2); $quality = substr($q, 2);
} }
} }
$localePref[$quality][] = trim($localeData[0]); $localePref[$quality][] = trim($localeData[0]);
} }
krsort($localePref); krsort($localePref);
   
$supportedLocales = GitPHP_Resource::SupportedLocales(); $supportedLocales = GitPHP_Resource::SupportedLocales();
   
foreach ($localePref as $quality => $qualityArray) { foreach ($localePref as $quality => $qualityArray) {
foreach ($qualityArray as $browserLocale) { foreach ($qualityArray as $browserLocale) {
$locale = str_replace('-', '_', $browserLocale); $locale = str_replace('-', '_', $browserLocale);
$loclen = strlen($locale); $loclen = strlen($locale);
   
foreach ($supportedLocales as $l => $lang) { foreach ($supportedLocales as $l => $lang) {
/* /*
* using strncasecmp with length of the preferred * using strncasecmp with length of the preferred
* locale means we can match both full * locale means we can match both full
* language + country preference specifications * language + country preference specifications
* (en_US) as well as just language specifications * (en_US) as well as just language specifications
* (en) * (en)
*/ */
if (strncasecmp($locale, $l, $loclen) === 0) { if (strncasecmp($locale, $l, $loclen) === 0) {
return $l; return $l;
} }
} }
} }
} }
return ''; return '';
} }
   
/** /**
* Instantiate * Instantiate
* *
* Instantiates the singleton instance * Instantiates the singleton instance
* *
* @access public * @access public
* @static * @static
* @param string $locale locale to instantiate * @param string $locale locale to instantiate
* @return boolean true if resource provider was instantiated successfully * @return boolean true if resource provider was instantiated successfully
*/ */
public static function Instantiate($locale) public static function Instantiate($locale)
{ {
self::$instance = null; self::$instance = null;
self::$currentLocale = ''; self::$currentLocale = '';
   
$reader = null; $reader = null;
if (!(($locale == 'en_US') || ($locale == 'en'))) { if (!(($locale == 'en_US') || ($locale == 'en'))) {
$reader = new FileReader(GITPHP_LOCALEDIR . $locale . '/gitphp.mo'); $reader = new FileReader(GITPHP_LOCALEDIR . $locale . '/gitphp.mo');
if (!$reader) if (!$reader)
return false; return false;
} }
   
self::$instance = new gettext_reader($reader); self::$instance = new gettext_reader($reader);
self::$currentLocale = $locale; self::$currentLocale = $locale;
return true; return true;
} }
   
} }
   
   
/** /**
* Gettext wrapper function for readability, single string * Gettext wrapper function for readability, single string
* *
* @param string $str string to translate * @param string $str string to translate
* @return string translated string * @return string translated string
*/ */
function __($str) function __($str)
{ {
if (GitPHP_Resource::Instantiated()) if (GitPHP_Resource::Instantiated())
return GitPHP_Resource::GetInstance()->translate($str); return GitPHP_Resource::GetInstance()->translate($str);
return $str; return $str;
} }
   
/** /**
* Gettext wrapper function for readability, plural form * Gettext wrapper function for readability, plural form
* *
* @param string $singular singular form of string * @param string $singular singular form of string
* @param string $plural plural form of string * @param string $plural plural form of string
* @param int $count number of items * @param int $count number of items
* @return string translated string * @return string translated string
*/ */
function __n($singular, $plural, $count) function __n($singular, $plural, $count)
{ {
if (GitPHP_Resource::Instantiated()) if (GitPHP_Resource::Instantiated())
return GitPHP_Resource::GetInstance()->ngettext($singular, $plural, $count); return GitPHP_Resource::GetInstance()->ngettext($singular, $plural, $count);
if ($count > 1) if ($count > 1)
return $plural; return $plural;
return $singular; return $singular;
} }
   
   
comments