예제 #1
0
 private static function create(Locale $locale)
 {
     $formatter = \NumberFormatter::create($locale->getLocale(), \NumberFormatter::DEFAULT_STYLE);
     $zeroDigit = $formatter->getSymbol(\NumberFormatter::ZERO_DIGIT_SYMBOL);
     $positiveSign = $formatter->getSymbol(\NumberFormatter::PLUS_SIGN_SYMBOL);
     $negativeSign = $formatter->getSymbol(\NumberFormatter::MINUS_SIGN_SYMBOL);
     $decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
     if ($zeroDigit === '0' && $negativeSign === '-' && $decimalSeparator === '.') {
         return self::STANDARD();
     }
     return new DecimalStyle($zeroDigit, $positiveSign, $negativeSign, $decimalSeparator);
 }
예제 #2
0
 /**
  * Obtains an instance of {@code WeekFields} appropriate for a locale.
  * <p>
  * This will look up appropriate values from the provider of localization data.
  *
  * @param Locale $locale the locale to use, not null
  * @return WeekFields the week-definition, not null
  */
 public static function ofLocale(Locale $locale)
 {
     // TODO $locale = Locale::of($locale->getLanguage(), $locale->getCountry());  // elminate variants
     $cal = IntlCalendar::createInstance(null, $locale->getLocale());
     $calDow = $cal->getFirstDayOfWeek();
     $dow = DayOfWeek::SUNDAY()->plus($calDow - 1);
     $minDays = $cal->getMinimalDaysInFirstWeek();
     return WeekFields::of($dow, $minDays);
 }
 /**
  * Gets the formatting pattern for date and time styles for a locale and chronology.
  * The locale and chronology are used to lookup the locale specific format
  * for the requested dateStyle and/or timeStyle.
  *
  * @param FormatStyle|null $dateStyle the FormatStyle for the date, null for time-only pattern
  * @param FormatStyle|null $timeStyle the FormatStyle for the time, null for date-only pattern
  * @param Chronology $chrono the Chronology, non-null
  * @param Locale $locale the locale, non-null
  * @return string the locale and Chronology specific formatting pattern
  * @throws IllegalArgumentException if both dateStyle and timeStyle are null
  */
 public static function getLocalizedDateTimePattern($dateStyle, $timeStyle, Chronology $chrono, Locale $locale)
 {
     if ($dateStyle === null && $timeStyle === null) {
         throw new IllegalArgumentException("Either dateStyle or timeStyle must be non-null");
     }
     // TODO use calendar from chonology
     $formatter = \IntlDateFormatter::create($locale->getLocale(), self::convertStyle($dateStyle), self::convertStyle($timeStyle));
     return $formatter->getPattern();
 }
 /**
  * @param $field
  * @param TextStyle $style
  * @param Locale $locale
  * @param callable $transformer
  * @return array|null
  */
 public static function tryFetchStyleValues($field, $style, Locale $locale, callable $transformer)
 {
     $bundle = new ResourceBundle($locale->getLocale(), null);
     if ($style === null) {
         $tmp = [];
         foreach (['stand-alone', 'format'] as $id) {
             foreach (['wide', 'abbreviated', 'narrow'] as $style) {
                 $values = $bundle['calendar']['gregorian'][$field][$id][$style];
                 if ($values === null) {
                     continue;
                 }
                 foreach ($values as $key => $value) {
                     $tmp[$value] = $transformer($key);
                 }
             }
         }
         return $tmp;
     }
     $id = $style->isStandalone() ? 'stand-alone' : 'format';
     $styles = [\IntlDateFormatter::FULL => 'wide', \IntlDateFormatter::MEDIUM => 'abbreviated', \IntlDateFormatter::SHORT => 'narrow'];
     $values = $bundle['calendar']['gregorian'][$field][$id][$styles[$style->toCalendarStyle()]];
     // fallback to stand alone if not found
     if ($values === null && $id === 'format') {
         $values = $bundle['calendar']['gregorian'][$field]['stand-alone'][$styles[$style->toCalendarStyle()]];
     }
     // ERA
     if ($values === null) {
         $values = $bundle['calendar']['gregorian'][$field][$styles[$style->toCalendarStyle()]];
     }
     // AMPM
     if ($values === null) {
         $values = $bundle['calendar']['gregorian'][$field];
     }
     if (!$values) {
         return null;
     }
     $tmp = [];
     foreach ($values as $key => $value) {
         $tmp[$value] = $transformer($key);
     }
     return $tmp;
 }
 /**
  * @dataProvider data_time
  */
 public function test_time_parse(LocalTime $time, FormatStyle $timeStyle, $timeStyleOld, Locale $locale)
 {
     $old = \IntlDateFormatter::create($locale->getLocale(), \IntlDateFormatter::NONE, $timeStyleOld, new \DateTimeZone('UTC'));
     $oldDate = new \DateTime('1970-0-0T' . $time->getHour() . ':' . $time->getMinute() . ':' . $time->getSecond(), new \DateTimeZone('UTC'));
     $text = $old->format($oldDate);
     $f = $this->builder->appendLocalized(null, $timeStyle)->toFormatter2($locale);
     $parsed = $f->parsePos($text, $this->pos);
     $this->assertEquals($this->pos->getIndex(), strlen($text));
     $this->assertEquals($this->pos->getErrorIndex(), -1);
     $this->assertEquals(LocalTime::from($parsed), $time);
 }