Exemple #1
0
 public static function getInstance()
 {
     if (self::$instance == NULL) {
         self::$instance = new MKrono();
         self::$instance->baseDate = '02/01/00';
         // For day/month names
         self::$instance->localeConv = localeConv();
         self::$instance->separator = Manager::getOptions('separatorDate');
         self::$instance->formatDate = Manager::getOptions('formatDate');
         self::$instance->formatTimestamp = Manager::getOptions('formatTimestamp');
     }
     return self::$instance;
 }
Exemple #2
0
 public static function encode($value, $toMap = true, $encodeEmptyStrings = false)
 {
     if (is_string($value)) {
         return self::quoteString($value);
     } else {
         if (is_bool($value)) {
             return $value ? 'true' : 'false';
         } else {
             if (is_array($value)) {
                 $results = '';
                 if (($n = count($value)) > 0 && array_keys($value) !== range(0, $n - 1)) {
                     foreach ($value as $k => $v) {
                         if ($v !== '' || $encodeEmptyStrings) {
                             if ($results !== '') {
                                 $results .= ',';
                             }
                             $results .= "'{$k}':" . self::encode($v, $toMap, $encodeEmptyStrings);
                         }
                     }
                     return '{' . $results . '}';
                 } else {
                     foreach ($value as $v) {
                         if ($v !== '' || $encodeEmptyStrings) {
                             if ($results !== '') {
                                 $results .= ',';
                             }
                             $results .= self::encode($v, $toMap, $encodeEmptyStrings);
                         }
                     }
                     return '[' . $results . ']';
                 }
             } else {
                 if (is_integer($value)) {
                     return "{$value}";
                 } else {
                     if (is_float($value)) {
                         switch ($value) {
                             case -INF:
                                 return 'Number.NEGATIVE_INFINITY';
                                 break;
                             case INF:
                                 return 'Number.POSITIVE_INFINITY';
                                 break;
                             default:
                                 $locale = localeConv();
                                 if ($locale['decimal_point'] == '.') {
                                     return "{$value}";
                                 } else {
                                     return str_replace($locale['decimal_point'], '.', "{$value}");
                                 }
                                 break;
                         }
                     } else {
                         if (is_object($value)) {
                             if ($value instanceof TJavaScriptLiteral) {
                                 return $value->toJavaScriptLiteral();
                             } else {
                                 return self::encode(get_object_vars($value), $toMap);
                             }
                         } else {
                             if ($value === null) {
                                 return 'null';
                             } else {
                                 return '';
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #3
0
 /**
  * Set Locale
  * 
  * Example:
  * <code>
  * I18Nv2::setLocale('en_GB');
  * </code>
  * 
  * @static
  * @access  public
  * @return  mixed   &type.string; used locale or false on failure
  * @param   string  $locale     a valid locale like en_US or de_DE
  * @param   int     $cat        the locale category - usually LC_ALL
  */
 static function setLocale($locale = null, $cat = LC_ALL)
 {
     if (!strlen($locale)) {
         return setLocale($cat, null);
     }
     $locales = I18Nv2::getStaticProperty('locales');
     // get complete standard locale code (en => en_US)
     if (isset($locales[$locale])) {
         $locale = $locales[$locale];
     }
     // get Win32 locale code (en_US => enu)
     if (I18Nv2_WIN) {
         $windows = I18Nv2::getStaticProperty('windows');
         $setlocale = isset($windows[$locale]) ? $windows[$locale] : $locale;
     } else {
         $setlocale = $locale;
     }
     $syslocale = setLocale($cat, $setlocale);
     // if the locale is not recognized by the system, check if there
     // is a fallback locale and try that, otherwise return false
     if (!$syslocale) {
         $fallbacks =& I18Nv2::getStaticProperty('fallbacks');
         if (isset($fallbacks[$locale])) {
             // avoid endless recursion with circular fallbacks
             $trylocale = $fallbacks[$locale];
             unset($fallbacks[$locale]);
             if ($retlocale = I18Nv2::setLocale($trylocale, $cat)) {
                 $fallbacks[$locale] = $trylocale;
                 return $retlocale;
             }
         }
         return false;
     }
     $language = substr($locale, 0, 2);
     if (I18Nv2_WIN) {
         @putEnv('LANG=' . $language);
         @putEnv('LANGUAGE=' . $language);
     } else {
         @putEnv('LANG=' . $locale);
         @putEnv('LANGUAGE=' . $locale);
     }
     // unshift locale stack
     $last =& I18Nv2::getStaticProperty('last');
     array_unshift($last, array(0 => $locale, 1 => $language, 2 => $syslocale, 'locale' => $locale, 'language' => $language, 'syslocale' => $syslocale));
     // fetch locale specific information
     $info =& I18Nv2::getStaticProperty('info');
     $info = localeConv();
     return $syslocale;
 }
Exemple #4
0
/**
 * Convert a localized number string into a floating point number
 *
 * @param      string $sNumber The localized number string to convert.
 * @return     float The converted number.
 */
function str2num($sNumber)
{
    $aConventions = localeConv();
    $sNumber = trim((string) $sNumber);
    $bIsNegative = 0 === $aConventions['n_sign_posn'] && '(' === $sNumber[0] && ')' === $sNumber[strlen($sNumber) - 1];
    $sCharacters = $aConventions['decimal_point'] . $aConventions['mon_decimal_point'] . $aConventions['negative_sign'];
    $sNumber = preg_replace('/[^' . preg_quote($sCharacters) . '\\d]+/', '', trim((string) $sNumber));
    $iLength = strlen($sNumber);
    if (strlen($aConventions['decimal_point'])) {
        $sNumber = str_replace($aConventions['decimal_point'], '.', $sNumber);
    }
    if (strlen($aConventions['mon_decimal_point'])) {
        $sNumber = str_replace($aConventions['mon_decimal_point'], '.', $sNumber);
    }
    $sNegativeSign = $aConventions['negative_sign'];
    if (strlen($sNegativeSign) && 0 !== $aConventions['n_sign_posn']) {
        $bIsNegative = $sNegativeSign === $sNumber[0] || $sNegativeSign === $sNumber[$iLength - 1];
        if ($bIsNegative) {
            $sNumber = str_replace($aConventions['negative_sign'], '', $sNumber);
        }
    }
    $fNumber = (double) $sNumber;
    if ($bIsNegative) {
        $fNumber = -$fNumber;
    }
    return $fNumber;
}
Exemple #5
0
 /**
  * Retorna o valor float da variável, com base no locale atual (definido via setlocale)
  * @param $value (string) valor a ser convertido
  * @returns (float) value
  *
  */
 public static function getFloatValue($value)
 {
     $l = localeConv();
     $sign = strpos($value, $l['negative_sign'] ?: '-') !== false ? -1 : 1;
     $value = strtr($value, $l['positive_sign'] . $l['negative_sign'] . '()', '    ');
     $value = str_replace(' ', '', $value);
     $value = str_replace($l['currency_symbol'] ?: '$', '', $value);
     $value = str_replace($l['mon_thousands_sep'] ?: ',', '', $value);
     $value = str_replace($l['mon_decimal_point'] ?: '.', '.', $value);
     return (double) ($value * $sign);
 }
Exemple #6
0
 /**
  * Set Locale
  *
  * Example:
  * <code>
  * I18Nv2::setLocale('en_GB');
  * </code>
  *
  * @static
  * @access  public
  * @return  mixed   &type.string; used locale or false on failure
  * @param   string  $locale     a valid locale like en_US or de_DE
  * @param   int     $cat        the locale category - usually LC_ALL
  */
 static function setLocale($locale = null, $cat = LC_ALL)
 {
     if (!strlen($locale)) {
         return setLocale($cat, null);
     }
     $locales = I18Nv2::getStaticProperty('locales');
     // get complete standard locale code (en => en_US)
     if (isset($locales[$locale])) {
         $locale = $locales[$locale];
     }
     // get Win32 locale code (en_US => enu)
     if (I18Nv2_WIN) {
         $windows = I18Nv2::getStaticProperty('windows');
         $setlocale = isset($windows[$locale]) ? $windows[$locale] : $locale;
     } else {
         $setlocale = $locale;
     }
     $syslocale = setLocale($cat, $setlocale);
     // if the locale is not recognized by the system, check if there
     // is a fallback locale and try that, otherwise return false
     if (!$syslocale) {
         $fallbacks =& I18Nv2::getStaticProperty('fallbacks');
         if (isset($fallbacks[$locale])) {
             // avoid endless recursion with circular fallbacks
             $trylocale = $fallbacks[$locale];
             unset($fallbacks[$locale]);
             if ($retlocale = I18Nv2::setLocale($trylocale, $cat)) {
                 $fallbacks[$locale] = $trylocale;
                 return $retlocale;
             }
         }
         return false;
     }
     $language = substr($locale, 0, 2);
     if (I18Nv2_WIN) {
         @putEnv('LANG=' . $language);
         @putEnv('LANGUAGE=' . $language);
     } else {
         @putEnv('LANG=' . $locale);
         @putEnv('LANGUAGE=' . $locale);
     }
     // unshift locale stack
     $last =& I18Nv2::getStaticProperty('last');
     array_unshift($last, array(0 => $locale, 1 => $language, 2 => $syslocale, 'locale' => $locale, 'language' => $language, 'syslocale' => $syslocale));
     // fetch locale specific information
     $info =& I18Nv2::getStaticProperty('info');
     $info = localeConv();
     //For some reason Windows can return bogus locale data where frac_digits is 127, in that case just fall back to default english values
     if (isset($info['frac_digits']) and $info['frac_digits'] == 127) {
         $info = array('decimal_point' => '.', 'thousands_sep' => ',', 'int_curr_symbol' => '', 'currency_symbol' => '$', 'mon_decimal_point' => '.', 'mon_thousands_sep' => ',', 'positive_sign' => '', 'negative_sign' => '-', 'int_frac_digits' => 2, 'frac_digits' => 2, 'p_cs_precedes' => 1, 'p_sep_by_space' => 0, 'n_cs_precedes' => 1, 'n_sep_by_space' => 0, 'p_sign_posn' => 1, 'n_sign_posn' => 1, 'grouping' => array(3, 3), 'mon_grouping' => array(3, 3));
     }
     return $syslocale;
 }
Exemple #7
0
 public function __construct($language, $langstr = null)
 {
     if ($langstr == null) {
         $langstr = array();
     }
     $this->_name = $language;
     $systemLocale = $this->getIsoName();
     $options = array($systemLocale . ".UTF8", $systemLocale . ".UTF-8", $systemLocale . ".utf8", $systemLocale . ".utf-8", $systemLocale, $language, $this->_name);
     $this->_cultureActive = setlocale(LC_ALL, $options);
     // Try to load in Windows if failed before
     if (!$this->_cultureActive) {
         $row = LocaleFactory::getInfoLocaleDB('shortname', $this->getName());
         $langstr = $row->getFieldArray("langstr");
         $iLang = 0;
         while (!$this->_cultureActive && $iLang < count($langstr)) {
             $this->_cultureActive = setlocale(LC_ALL, $langstr[$iLang]);
             $iLang++;
         }
     }
     if (!$this->_cultureActive) {
         $firstPart = substr($language, 0, 2);
         if (FileUtil::isWindowsOS()) {
             $complement = "";
         } else {
             $complement = "Only the languages available in 'locale -a' can be setted. On debian try execute 'apt-get install language-pack-{$firstPart}'.";
         }
         echo "<br/>\n<b>Warning: </b> The culture language '{$language}' was not found. {$complement}\n<br/>";
         // I cant call the context here because it's call CultureInfo but the context is not complete yet.
         //$context = Context::getInstance();
         //$context->WriteWarningMessage ("");
     }
     $this->_localeConv = localeConv();
     #if (stripos(PHP_OS, 'win') !== false)
     #{
     #	$this->_localeConv["currency_symbol"] = utf8_encode($this->_localeConv["currency_symbol"]);
     #}
     #Debug::PrintValue($this->_localeConv, $langstr, $this->getRegionalMonthNames());
 }
Exemple #8
0
 public function formatValue()
 {
     $l = localeConv();
     // Sign specifications:
     if ($this->value >= 0) {
         $sign = $l['positive_sign'];
         $sign_posn = $l['p_sign_posn'];
     } else {
         $sign = $l['negative_sign'];
         $sign_posn = $l['n_sign_posn'];
     }
     // Currency format:
     $m = number_format(abs($this->value), $l['frac_digits'], $l['mon_decimal_point'], $l['mon_thousands_sep']);
     switch ($sign_posn) {
         case 0:
             $m = "({$m})";
             break;
         case 1:
             $m = "{$sign}{$m}";
             break;
         case 2:
             $m = "{$m}{$sign}";
             break;
         case 3:
             $m = "{$sign}{$m}";
             break;
         case 4:
             $m = "{$m}{$sign}";
             break;
         default:
             $m = "{$m} [error sign_posn={$sign_posn}&nbsp;!]";
     }
     return $m;
 }