* @package Mammut\Locale */ class Locale extends \Mammut\StdObject { const SHORT = 1; const LONG = 2; const DETAIL = 3; const LOC_DE = 'de'; const LOC_EN = 'en'; const LOC_JP = 'jp'; private $id = 'undefined'; private $format = array(); private static $instances = array(); private $symbol = array(); public function __construct($localefile, $id = 'undefined') { $basedir = __DIR__ . DIRECTORY_SEPARATOR . '_locales_' . DIRECTORY_SEPARATOR; require ($basedir . $localefile); $this->format = $format; $this->text = $text; $this->symbol = $symbol; $this->id = $id; } /** * Creates a new locale instance based on an identifier * @param string $locale a locale identifier * @return Locale the new locale instance */ public static function getInstance($locale) { $locale = strtolower($locale); if(!key_exists($locale, self::$instances)) self::$instances[$locale] = new Locale('i18n-' . strtolower(str_replace('_', '-', $locale)) . '.php', $locale); return self::$instances[$locale]; } public function getId() { return $this->id; } /** * returns the type string for the date format of the locale * * @param int $type * format type, can be on of the MFLOCALE constancs SHORT, LONG or DETAIL * @return string type string */ public function getDateFormat($type = self::LONG) { switch($type) { case self::SHORT: return $this->format['date_short']; case self::LONG: return $this->format['date_long']; ; case self::DETAIL: return 'd.m.Y'; default: throw new \InvalidArgumentException('type unknown'); } } /** * returns the type string for the time format of the locale * * @param int $type * format type, can be on of the MFLOCALE constancs SHORT, LONG or DETAIL * @param boolean $seconds * to true if the format should contain seconds * @return string type string */ public function getTimeFormat($type = self::LONG, $seconds = true) { switch($type) { case self::SHORT: return $seconds ? $this->format['time_short_s'] : $this->format['time_short']; case self::LONG: case self::DETAIL: return $seconds ? $this->format['time_long_s'] : $this->format['time_long']; default: throw new \InvalidArgumentException('type unknown'); } } /** * returns the type string for the datetime format of the locale * * @param int $type * format type, can be on of the MFLOCALE constancs SHORT, LONG or DETAIL * @param boolean $seconds * to true if the format should contain seconds * @return string type string */ public function getDateTimeFormat($type = self::LONG, $seconds = true) { switch($type) { case self::SHORT: return $seconds ? $this->format['datetime_short_s'] : $this->format['datetime_short']; case self::LONG: case self::DETAIL: return $seconds ? $this->format['datetime_long_s'] : $this->format['datetime_long']; default: throw new \InvalidArgumentException('type unknown'); } } /** * * @param int $type * format type, can be on of the MFLOCALE constancs SHORT, LONG or DETAIL * @return string currency symbol as string or html code (short version) */ public function getCurrencySymbol($type = self::LONG) { return '€'; } /** * * @param int $type * format type, can be on of the MFLOCALE constancs SHORT, LONG or DETAIL * @return array weekday names as numbered array, starting with 0 for monday */ public function getWeekdays($type = self::LONG) { switch($type) { case self::SHORT: return $this->text['day_short']; case self::LONG: case self::DETAIL: return $this->text['day_long']; default: throw new \InvalidArgumentException('type unknown'); } } /** * * @param int $type * format type, can be on of the MFLOCALE constancs SHORT, LONG or DETAIL * @return array month names as numbered array, starting with 0 for january */ public function getMonths($type = self::LONG) { switch($type) { case self::SHORT: return $this->text['month_short']; case self::LONG: case self::DETAIL: return $this->text['month_long']; default: throw new \InvalidArgumentException('type unknown'); } } /** * converts an user agent locale priority string to an sortet locale string array * * @return array an sorted string array with the highest priority on the 1st position */ public static function getLocaleList($agentAccept) { $langu = explode(',', $agentAccept); $langPrioTable = array(); foreach($langu as $l) { if(strpos($l, ';') !== false) { list($st, $prio) = explode(';', $l); $langPrioTable[str_replace('q=', '', $prio)] = str_replace('-', '_', $st); } else $langPrioTable['1.0'] = strtolower(str_replace('-', '_', $l)); } krsort($langPrioTable); return $langPrioTable; } public static function findBestLocale($agentAccepted, array $allowed, $default = false) { $langPrioTable = self::getLocaleList($agentAccepted); $lang = $default; foreach($langPrioTable as $l) { if(in_array($l, $allowed)) { $lang = $l; break; } } return $lang; } }