diff --git a/.gitignore b/.gitignore index 5ebd21a..2345597 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,4 @@ pip-log.txt # Mac crap .DS_Store +.idea \ No newline at end of file diff --git a/Dater/Dater.php b/Dater/Dater.php index 734dbe2..0b45d37 100644 --- a/Dater/Dater.php +++ b/Dater/Dater.php @@ -17,6 +17,12 @@ class Dater { const SERVER_TIME_FORMAT = 'server_time'; const SERVER_DATETIME_FORMAT = 'server_datetime'; + protected static $localesCodes = array( + 'en' => 'English', + 'ru' => 'Russian', + 'ua' => 'Ukrainian', + ); + protected $formats = array( self::USER_DATE_FORMAT => 'm/d/Y', self::USER_TIME_FORMAT => 'g:i A', @@ -50,6 +56,21 @@ public function getLocale() { return $this->locale; } + /** + * Get locale by 2-chars code: en, ru, ua + * @param $code + * @throws Exception + * @return + */ + public static function getLocaleByCode($code) { + $code = strtolower($code); + if(!isset(static::$localesCodes[$code])) { + throw new Exception('Unknown locale code "' . $code . '". See available codes in Dater::$localeCodes.'); + } + $class = 'Dater_Locale_' . static::$localesCodes[$code]; + return new $class(); + } + public function setLocale(Dater_Locale $locale) { foreach($locale::$formats as $alias => $format) { $this->setFormat($alias, $format); diff --git a/example/index.php b/example/index.php index c4d9862..4f5e3ba 100644 --- a/example/index.php +++ b/example/index.php @@ -39,7 +39,7 @@ echo 'User date: ' . $dater->date() . PHP_EOL; echo 'User formatted date: ' . $dater->now('j F Y') . PHP_EOL . PHP_EOL; -$dater->setLocale(new Dater_Locale_Russian()); +$dater->setLocale(Dater::getLocaleByCode('ru')); echo 'Set locale: : ' . get_class($dater->getLocale()) . PHP_EOL; echo 'User date: ' . $dater->date() . PHP_EOL; echo 'User formatted date: ' . $dater->now('j F Y') . PHP_EOL . PHP_EOL; diff --git a/readme.md b/readme.md index 37c40df..b529616 100644 --- a/readme.md +++ b/readme.md @@ -30,7 +30,7 @@ All [date()](http://php.net/date) format options available and can be overrided ### Locales support - $dater->setLocale(new Dater_Locale_English()); + $dater->setLocale(new Dater_Locale_English()); // or you can use Dater::getLocaleByCode('ru') echo $dater->date(); // 03/21/2013 echo $dater->now('j F Y'); // 21 March 2013