00001 <?php
00002
00007 class spunQ_Locale extends spunQ_StorableObject implements spunQ_IDatabaseInstantiationListener {
00008
00013 const DEFAULT_SPUNQ_LOCALE = 'de-at';
00014
00021 protected static $system = NULL;
00022
00027 private static $allByRfcName = NULL;
00028
00033 private static $allByShortName = NULL;
00034
00043 public static function getSystemLocale() {
00044 if (self::$system === NULL) {
00045 $rfcName = self::DEFAULT_SPUNQ_LOCALE;
00046 if (defined('SPUNQ_SYSTEM_LOCALE')) {
00047 $rfcName = SPUNQ_SYSTEM_LOCALE;
00048 }
00049 self::$system = new self();
00050 self::$system->rfcName = $rfcName;
00051 self::$system->shortName = strtolower(Locale::getRegion($rfcName));
00052 if (self::$system->shortName == '') {
00053 self::$system->shortName = strtolower($rfcName);
00054 }
00055 self::$system->name = Locale::getDisplayLanguage($rfcName, $rfcName);
00056 # We need to set these values here. Otherwise the system will try
00057 # to fetch them from the database, which is a huge problem if an
00058 # exception during the initialization occurs.
00059 self::$system->remoteValues['rfcName'] = self::$system->rfcName;
00060 self::$system->remoteValues['shortName'] = self::$system->shortName;
00061 self::$system->remoteValues['name'] = self::$system->name;
00062 spunQ_db::observeInitializations(self::$system);
00063 self::$allByRfcName[self::$system->rfcName] = self::$system;
00064 self::$allByShortName[self::$system->shortName] = self::$system;
00065 }
00066 return self::$system;
00067 }
00068
00079 public static function getMostAppropriateRfcLocale($locale, array $possibleLocales, $fallbackLocale = NULL) {
00080 $locale = Locale::lookup($possibleLocales, $locale, true, $fallbackLocale);
00081 if ($locale == NULL) {
00082 return NULL;
00083 }
00084 return str_replace('_', '-', $locale);
00085 }
00086
00096 public static function getByRfcName($rfcName, $throwException = true) {
00097 self::init();
00098 if (!isset(self::$allByRfcName[$rfcName])) {
00099 if (!$throwException) {
00100 return NULL;
00101 }
00102 throw new spunQ_InvalidArgumentError('NoLocaleWithGivenShortName', __CLASS__, __FUNCTION__, 'rfcName', $rfcName);
00103 }
00104 return self::$allByRfcName[$rfcName];
00105 }
00106
00116 public static function getByShortName($shortName, $throwException = true) {
00117 self::init();
00118 if (!isset(self::$allByShortName[$shortName])) {
00119 if (!$throwException) {
00120 return NULL;
00121 }
00122 throw new spunQ_InvalidArgumentError('NoLocaleWithGivenShortName', __CLASS__, __FUNCTION__, 'shortName', $shortName);
00123 }
00124 return self::$allByShortName[$shortName];
00125 }
00126
00131 public static function getAll() {
00132 self::init();
00133 return self::$allByRfcName;
00134 }
00135
00140 private static function init() {
00141 self::getSystemLocale();
00142 return NULL;
00143 }
00144
00151 protected $rfcName;
00152
00158 protected $shortName;
00159
00166 protected $name;
00167
00173 protected $possibleFallbacks = array();
00174
00181 public function getName() {
00182 $name = $this->__call('getName', array());
00183 if ($name !== NULL) {
00184 return $name;
00185 }
00186 return Locale::getDisplayLanguage($this->rfcName, $this->rfcName);
00187 }
00188
00192 public function creatingDatabaseInstance($name) {
00193 return NULL;
00194 }
00195
00199 public function createdDatabaseInstance($name, spunQ_IDatabaseConnection $connection) {
00200 if ($name === 'default' && $connection->getTypeStorage('spunQ.language.Locale', false) !== NULL) {
00201 $select = new spunQ_SelectQuery();
00202 $select->setType('spunQ.language.Locale');
00203 $select->setProperties(array('shortName', 'rfcName', 'name'));
00204 self::$allByShortName = array();
00205 self::$allByRfcName = array();
00206 foreach ($select->execute() as $locale) {
00207 self::$allByShortName[$locale->getShortName()] = $locale;
00208 self::$allByRfcName[$locale->getRfcName()] = $locale;
00209 }
00210 if (isset(self::$allByRfcName[$this->rfcName])) {
00211 $connection->removeFromCache(self::$allByRfcName[$this->rfcName]);
00212 $this->id = self::$allByRfcName[$this->rfcName]->getId();
00213 $this->setConnection($connection);
00214 self::$allByShortName[$this->shortName] = $this;
00215 self::$allByRfcName[$this->rfcName] = $this;
00216 } else {
00217 self::$allByShortName[$this->shortName] = $this;
00218 self::$allByRfcName[$this->rfcName] = $this;
00219 $this->store();
00220 }
00221 }
00222 return NULL;
00223 }
00224
00225 }
00226