00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Core_Model_Locale
00033 {
00034
00035
00036
00037 const DEFAULT_LOCALE = 'en_US';
00038 const DEFAULT_TIMEZONE = 'UTC';
00039 const DEFAULT_CURRENCY = 'USD';
00040
00041
00042
00043
00044 const XML_PATH_DEFAULT_LOCALE = 'general/locale/code';
00045 const XML_PATH_DEFAULT_TIMEZONE = 'general/locale/timezone';
00046 const XML_PATH_DEFAULT_COUNTRY = 'general/country/default';
00047 const XML_PATH_ALLOW_CODES = 'global/locale/allow/codes';
00048 const XML_PATH_ALLOW_CURRENCIES = 'global/locale/allow/currencies';
00049 const XML_PATH_ALLOW_CURRENCIES_INSTALLED = 'system/currency/installed';
00050
00051
00052
00053
00054 const FORMAT_TYPE_FULL = 'full';
00055 const FORMAT_TYPE_LONG = 'long';
00056 const FORMAT_TYPE_MEDIUM= 'medium';
00057 const FORMAT_TYPE_SHORT = 'short';
00058
00059
00060
00061
00062
00063
00064 protected $_defaultLocale;
00065
00066
00067
00068
00069
00070
00071 protected $_locale;
00072
00073
00074
00075
00076
00077
00078 protected $_localeCode;
00079
00080
00081
00082
00083
00084
00085 protected $_emulatedLocales = array();
00086
00087 protected static $_currencyCache = array();
00088
00089 public function __construct($locale = null)
00090 {
00091 $this->setLocale($locale);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100 public function setDefaultLocale($locale)
00101 {
00102 $this->_defaultLocale = $locale;
00103 return $this;
00104 }
00105
00106
00107
00108
00109
00110
00111 public function getDefaultLocale()
00112 {
00113 if (!$this->_defaultLocale) {
00114 $locale = Mage::getStoreConfig(self::XML_PATH_DEFAULT_LOCALE);
00115 if (!$locale) {
00116 $locale = self::DEFAULT_LOCALE;
00117 }
00118 $this->_defaultLocale = $locale;
00119 }
00120 return $this->_defaultLocale;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 public function setLocale($locale = null)
00130 {
00131 if (($locale !== null) && is_string($locale)) {
00132 $this->_localeCode = $locale;
00133 } else {
00134 $this->_localeCode = $this->getDefaultLocale();
00135 }
00136 Mage::dispatchEvent('core_locale_set_locale', array('locale'=>$this));
00137 return $this;
00138 }
00139
00140
00141
00142
00143
00144
00145 public function getTimezone()
00146 {
00147 return self::DEFAULT_TIMEZONE;
00148 }
00149
00150
00151
00152
00153
00154
00155 public function getCurrency()
00156 {
00157 return self::DEFAULT_CURRENCY;
00158 }
00159
00160
00161
00162
00163
00164
00165 public function getLocale()
00166 {
00167 if (!$this->_locale) {
00168 Zend_Locale_Data::setCache(Mage::app()->getCache());
00169 $this->_locale = new Zend_Locale($this->getLocaleCode());
00170 } elseif ($this->_locale->__toString() != $this->_localeCode) {
00171 $this->setLocale($this->_localeCode);
00172 }
00173
00174 return $this->_locale;
00175 }
00176
00177
00178
00179
00180
00181
00182 public function getLocaleCode()
00183 {
00184 if ($this->_localeCode === null) {
00185 $this->setLocale();
00186 }
00187 return $this->_localeCode;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 public function setLocaleCode($code)
00197 {
00198 $this->_localeCode = $code;
00199 return $this;
00200 }
00201
00202
00203
00204
00205
00206
00207 public function getOptionLocales()
00208 {
00209 return $this->_getOptionLocales();
00210 }
00211
00212
00213
00214
00215
00216
00217 public function getTranslatedOptionLocales()
00218 {
00219 return $this->_getOptionLocales(true);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 protected function _getOptionLocales($translatedName=false)
00229 {
00230 $options = array();
00231 $locales = $this->getLocale()->getLocaleList();
00232 $languages = $this->getLocale()->getLanguageTranslationList($this->getLocale());
00233 $countries = $this->getCountryTranslationList();
00234
00235 $allowed = $this->getAllowLocales();
00236 foreach ($locales as $code=>$active) {
00237 if (strstr($code, '_')) {
00238 if (!in_array($code, $allowed)) {
00239 continue;
00240 }
00241 $data = explode('_', $code);
00242 if (!isset($languages[$data[0]]) || !isset($countries[$data[1]])) {
00243 continue;
00244 }
00245 if ($translatedName) {
00246 $label = ucwords($this->getLocale()->getLanguageTranslation($data[0], $code))
00247 . ' (' . $this->getLocale()->getCountryTranslation($data[1], $code) . ') / '
00248 . $languages[$data[0]] . ' (' . $countries[$data[1]] . ')';
00249 } else {
00250 $label = $languages[$data[0]] . ' (' . $countries[$data[1]] . ')';
00251 }
00252 $options[] = array(
00253 'value' => $code,
00254 'label' => $label
00255 );
00256 }
00257 }
00258 return $this->_sortOptionArray($options);
00259 }
00260
00261
00262
00263
00264
00265
00266 public function getOptionTimezones()
00267 {
00268 $options= array();
00269 $zones = $this->getTranslationList('windowstotimezone');
00270 ksort($zones);
00271 foreach ($zones as $code=>$name) {
00272 $name = trim($name);
00273 $options[] = array(
00274 'label' => empty($name) ? $code : $name . ' (' . $code . ')',
00275 'value' => $code,
00276 );
00277 }
00278 return $this->_sortOptionArray($options);
00279 }
00280
00281
00282
00283
00284
00285
00286 public function getOptionWeekdays()
00287 {
00288 $options= array();
00289 $days = $this->getTranslationList('days');
00290 foreach (array_values($days['format']['wide']) as $code => $name) {
00291 $options[] = array(
00292 'label' => $name,
00293 'value' => $code,
00294 );
00295 }
00296 return $options;
00297 }
00298
00299
00300
00301
00302
00303
00304 public function getOptionCountries()
00305 {
00306 $options = array();
00307 $countries = $this->getCountryTranslationList();
00308
00309 foreach ($countries as $code=>$name) {
00310 $options[] = array(
00311 'label' => $name,
00312 'value' => $code,
00313 );
00314 }
00315 return $this->_sortOptionArray($options);
00316 }
00317
00318
00319
00320
00321
00322
00323 public function getOptionCurrencies()
00324 {
00325 $currencies = $this->getTranslationList('currencytoname');
00326 $options = array();
00327 $allowed = $this->getAllowCurrencies();
00328
00329 foreach ($currencies as $name=>$code) {
00330 if (!in_array($code, $allowed)) {
00331 continue;
00332 }
00333
00334 $options[] = array(
00335 'label' => $name,
00336 'value' => $code,
00337 );
00338 }
00339 return $this->_sortOptionArray($options);
00340 }
00341
00342
00343
00344
00345
00346
00347 public function getOptionAllCurrencies()
00348 {
00349 $currencies = $this->getTranslationList('currencytoname');
00350 $options = array();
00351 foreach ($currencies as $name=>$code) {
00352 $options[] = array(
00353 'label' => $name,
00354 'value' => $code,
00355 );
00356 }
00357 return $this->_sortOptionArray($options);
00358 }
00359
00360 protected function _sortOptionArray($option)
00361 {
00362 $data = array();
00363 foreach ($option as $item) {
00364 $data[$item['value']] = $item['label'];
00365 }
00366 asort($data);
00367 $option = array();
00368 foreach ($data as $key => $label) {
00369 $option[] = array(
00370 'value' => $key,
00371 'label' => $label
00372 );
00373 }
00374 return $option;
00375 }
00376
00377
00378
00379
00380
00381
00382 public function getAllowLocales()
00383 {
00384 $data = Mage::getConfig()->getNode(self::XML_PATH_ALLOW_CODES)->asArray();
00385 if ($data) {
00386 return array_keys($data);
00387 }
00388 return array();
00389 }
00390
00391
00392
00393
00394
00395
00396 public function getAllowCurrencies()
00397 {
00398 $data = array();
00399 if (Mage::isInstalled()) {
00400 $data = Mage::app()->getStore()->getConfig(self::XML_PATH_ALLOW_CURRENCIES_INSTALLED);
00401 return explode(',', $data);
00402 }
00403 else {
00404 $data = Mage::getConfig()->getNode(self::XML_PATH_ALLOW_CURRENCIES)->asArray();
00405 if ($data) {
00406 return array_keys($data);
00407 }
00408 }
00409 return $data;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418 public function getDateFormat($type=null)
00419 {
00420 return $this->getTranslation($type, 'date');
00421 }
00422
00423
00424
00425
00426
00427
00428
00429 public function getTimeFormat($type=null)
00430 {
00431 return $this->getTranslation($type, 'time');
00432 }
00433
00434
00435
00436
00437
00438
00439
00440 public function getDateTimeFormat($type)
00441 {
00442 return $this->getDateFormat($type) . ' ' . $this->getTimeFormat($type);
00443 }
00444
00445
00446
00447
00448
00449
00450
00451 public function getDateStrFormat($type)
00452 {
00453 return Varien_Date::convertZendToStrftime($this->getDateFormat($type), true, false);
00454 }
00455
00456
00457
00458
00459
00460
00461
00462 public function getTimeStrFormat($type)
00463 {
00464 return Varien_Date::convertZendToStrftime($this->getTimeFormat($type), false, true);
00465 }
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475 public function date($date=null, $part=null, $locale=null, $useTimezone=true)
00476 {
00477 if (is_null($locale)) {
00478 $locale = $this->getLocale();
00479 }
00480
00481
00482 $date = new Zend_Date($date, $part, $locale);
00483 if ($useTimezone) {
00484 if ($timezone = Mage::app()->getStore()->getConfig(self::XML_PATH_DEFAULT_TIMEZONE)) {
00485 $date->setTimezone($timezone);
00486 }
00487 }
00488
00489
00490 return $date;
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 public function storeDate($store=null, $date=null, $includeTime=false)
00502 {
00503 $timezone = Mage::app()->getStore($store)->getConfig(self::XML_PATH_DEFAULT_TIMEZONE);
00504 $date = new Zend_Date($date, null, $this->getLocale());
00505 $date->setTimezone($timezone);
00506 if (!$includeTime) {
00507 $date->setHour(0)
00508 ->setMinute(0)
00509 ->setSecond(0);
00510 }
00511 return $date;
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521 public function storeTimeStamp($store=null)
00522 {
00523 $timezone = Mage::app()->getStore($store)->getConfig(self::XML_PATH_DEFAULT_TIMEZONE);
00524 $currentTimezone = @date_default_timezone_get();
00525 @date_default_timezone_set($timezone);
00526 $date = date('Y-m-d H:i:s');
00527 @date_default_timezone_set($currentTimezone);
00528 return strtotime($date);
00529 }
00530
00531
00532
00533
00534
00535
00536
00537 public function currency($currency)
00538 {
00539 Varien_Profiler::start('locale/currency');
00540 if (!isset(self::$_currencyCache[$this->getLocaleCode()][$currency])) {
00541 try {
00542 $currencyObject = new Mage_Core_Model_Locale_Currency($currency, $this->getLocale());
00543 } catch (Exception $e) {
00544 $currencyObject = new Mage_Core_Model_Locale_Currency($this->getCurrency(), $this->getLocale());
00545 $options = array(
00546 'name' => $currency,
00547 'currency' => $currency,
00548 'symbol' => $currency
00549 );
00550 $currencyObject->setFormat($options);
00551 }
00552
00553 self::$_currencyCache[$this->getLocaleCode()][$currency] = $currencyObject;
00554 }
00555 Varien_Profiler::stop('locale/currency');
00556 return self::$_currencyCache[$this->getLocaleCode()][$currency];
00557 }
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 public function getNumber($value)
00577 {
00578 if (is_null($value)) {
00579 return null;
00580 }
00581
00582 if (!is_string($value)) {
00583 return floatval($value);
00584 }
00585
00586
00587 $value = str_replace('\'', '', $value);
00588 $value = str_replace(' ', '', $value);
00589
00590 $separatorComa = strpos($value, ',');
00591 $separatorDot = strpos($value, '.');
00592
00593 if ($separatorComa !== false && $separatorDot !== false) {
00594 if ($separatorComa > $separatorDot) {
00595 $value = str_replace('.', '', $value);
00596 $value = str_replace(',', '.', $value);
00597 }
00598 else {
00599 $value = str_replace(',', '', $value);
00600 }
00601 }
00602 elseif ($separatorComa !== false) {
00603 $value = str_replace(',', '.', $value);
00604 }
00605
00606 return floatval($value);
00607
00608 }
00609
00610
00611
00612
00613
00614
00615
00616 public function getJsPriceFormat()
00617 {
00618 $format = Zend_Locale_Data::getContent($this->getLocaleCode(), 'currencynumber');
00619 $symbols = Zend_Locale_Data::getList($this->getLocaleCode(), 'symbols');
00620
00621 $pos = strpos($format, ';');
00622 if ($pos !== false){
00623 $format = substr($format, 0, $pos);
00624 }
00625 $format = preg_replace("/[^0\#\.,]/", "", $format);
00626 $totalPrecision = 0;
00627 $decimalPoint = strpos($format, '.');
00628 if ($decimalPoint !== false) {
00629 $totalPrecision = (strlen($format) - (strrpos($format, '.')+1));
00630 } else {
00631 $decimalPoint = strlen($format);
00632 }
00633 $requiredPrecision = $totalPrecision;
00634 $t = substr($format, $decimalPoint);
00635 $pos = strpos($t, '#');
00636 if ($pos !== false){
00637 $requiredPrecision = strlen($t) - $pos - $totalPrecision;
00638 }
00639 $group = 0;
00640 if (strrpos($format, ',') !== false) {
00641 $group = ($decimalPoint - strrpos($format, ',') - 1);
00642 } else {
00643 $group = strrpos($format, '.');
00644 }
00645 $integerRequired = (strpos($format, '.') - strpos($format, '0'));
00646
00647 $result = array(
00648 'pattern' => Mage::app()->getStore()->getCurrentCurrency()->getOutputFormat(),
00649 'precision' => $totalPrecision,
00650 'requiredPrecision' => $requiredPrecision,
00651 'decimalSymbol' => $symbols['decimal'],
00652 'groupSymbol' => $symbols['group'],
00653 'groupLength' => $group,
00654 'integerRequired' => $integerRequired
00655 );
00656
00657 return $result;
00658 }
00659
00660
00661
00662
00663
00664
00665
00666 public function emulate($storeId)
00667 {
00668 if ($storeId) {
00669 $this->_emulatedLocales[] = clone $this->getLocale();
00670 $this->_locale = new Zend_Locale(Mage::getStoreConfig(self::XML_PATH_DEFAULT_LOCALE, $storeId));
00671 $this->_localeCode = $this->_locale->toString();
00672 Mage::getSingleton('core/translate')->setLocale($this->_locale)->init('frontend', true);
00673 }
00674 else {
00675 $this->_emulatedLocales[] = false;
00676 }
00677 }
00678
00679
00680
00681
00682
00683 public function revert()
00684 {
00685 if ($locale = array_pop($this->_emulatedLocales)) {
00686 $this->_locale = $locale;
00687 $this->_localeCode = $this->_locale->toString();
00688 Mage::getSingleton('core/translate')->setLocale($this->_locale)->init('adminhtml', true);
00689 }
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701 public function getTranslationList($path = null, $value = null)
00702 {
00703 return $this->getLocale()->getTranslationList($path, $this->getLocale(), $value);
00704 }
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714 public function getTranslation($value = null, $path = null)
00715 {
00716 return $this->getLocale()->getTranslation($value, $path, $this->getLocale());
00717 }
00718
00719
00720
00721
00722
00723
00724
00725 public function getCountryTranslation($value)
00726 {
00727 return $this->getLocale()->getCountryTranslation($value, $this->getLocale());
00728 }
00729
00730
00731
00732
00733
00734
00735 public function getCountryTranslationList()
00736 {
00737 return $this->getLocale()->getCountryTranslationList($this->getLocale());
00738 }
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 public function IsStoreDateInInterval($store, $dateFrom = null, $dateTo = null)
00749 {
00750 if (!$store instanceof Mage_Core_Model_Store) {
00751 $store = Mage::app()->getStore($store);
00752 }
00753
00754 $storeTimeStamp = $this->storeTimeStamp($store);
00755 $fromTimeStamp = strtotime($dateFrom);
00756 $toTimeStamp = strtotime($dateTo);
00757 if ($dateTo) {
00758
00759 $toTimeStamp += 86400;
00760 }
00761
00762 $result = false;
00763 if (!is_empty_date($dateFrom) && $storeTimeStamp < $fromTimeStamp) {
00764 }
00765 elseif (!is_empty_date($dateTo) && $storeTimeStamp > $toTimeStamp) {
00766 }
00767 else {
00768 $result = true;
00769 }
00770
00771 return $result;
00772 }
00773 }