Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function register()
 {
     $this->container->registerSingleton([I18n::class, 'i18n'], function ($container) {
         $app = $container->get('app');
         $cache = $container->get('config')->get('application')['language_cache'];
         $i18n = new I18n(new Loader($container->get('fileSystem'), $app->getPath() . '/resources/i18n'), $app->getLanguage());
         if ($cache !== false) {
             $i18n->setCache($container->get('cache')->instance($cache === true ? null : $cache));
         }
         return $i18n;
     });
 }
Esempio n. 2
0
 /**
  * Returns a human friendly representation of the time.
  *
  * @access  public
  * @param   \DateTimeInterface  $dateTime     DateTime object
  * @param   string              $dateFormat   Default date format
  * @param   string              $clockFormat  Default clock format
  * @return  string
  */
 public function time(DateTimeInterface $dateTime, $dateFormat = 'Y-m-d, H:i', $clockFormat = ', H:i')
 {
     $diff = time() - $dateTime->getTimestamp();
     if ($diff < 0) {
         // Our date is in the future
         $diff = abs($diff);
         if ($diff < 120) {
             return $this->i18n->get('humanizer.in_minute');
         } elseif ($diff < 3600) {
             return $this->i18n->get('humanizer.in_minutes', [$diff / 60]);
         } elseif ($dateTime->format('Y-m-d') === date('Y-m-d')) {
             return $this->i18n->get('humanizer.today') . (!empty($clockFormat) ? $dateTime->format($clockFormat) : '');
         } elseif ($dateTime->format('Y-m-d') === date('Y-m-d', time() + 60 * 60 * 24)) {
             return $this->i18n->get('humanizer.tomorrow') . (!empty($clockFormat) ? $dateTime->format($clockFormat) : '');
         }
     } else {
         // Our date is in the past
         if ($diff < 120) {
             return $this->i18n->get('humanizer.minute_ago');
         } elseif ($diff < 3600) {
             return $this->i18n->get('humanizer.minutes_ago', [$diff / 60]);
         } elseif ($dateTime->format('Y-m-d') === date('Y-m-d')) {
             return $this->i18n->get('humanizer.today') . (!empty($clockFormat) ? $dateTime->format($clockFormat) : '');
         } elseif ($dateTime->format('Y-m-d') === date('Y-m-d', time() - 60 * 60 * 24)) {
             return $this->i18n->get('humanizer.yesterday') . (!empty($clockFormat) ? $dateTime->format($clockFormat) : '');
         }
     }
     // None of the above so just return the default date format
     return $dateTime->format($dateFormat);
 }
Esempio n. 3
0
 /**
  * Returns the error message.
  *
  * @access  protected
  * @param   string     $field       Field name
  * @param   string     $package     Package name
  * @param   string     $validator   Validator name
  * @param   array      $parameters  Validator parameters
  * @return  string
  */
 protected function getErrorMessage($field, $package, $validator, $parameters)
 {
     $package = empty($package) ? '' : $package . '::';
     // We have a i18n instance so we can return a propper error message
     if ($this->i18n->has($package . 'validate.overrides.messages.' . $field . '.' . $validator)) {
         // Return custom field specific error message from the language file
         return $this->i18n->get($package . 'validate.overrides.messages.' . $field . '.' . $validator, array_merge([$field], $parameters));
     } else {
         // Try to translate field name
         $translateFieldName = function ($field) use($package) {
             if ($this->i18n->has($package . 'validate.overrides.fieldnames.' . $field)) {
                 $field = $this->i18n->get($package . 'validate.overrides.fieldnames.' . $field);
             } else {
                 $field = str_replace('_', ' ', $field);
             }
             return $field;
         };
         if (in_array($validator, ['match', 'different'])) {
             $field = [$translateFieldName($field), $translateFieldName(array_shift($parameters))];
         } else {
             $field = $translateFieldName($field);
         }
         // Return default validation error message from the language file
         return $this->i18n->get($package . 'validate.' . $validator, array_merge((array) $field, $parameters));
     }
 }
Esempio n. 4
0
 /**
  *
  */
 public function testCacheSave()
 {
     $loader = $this->getLoader();
     $loader->shouldReceive('loadStrings')->once()->with('en_US', 'foo')->andReturn($this->strings['foo']);
     $cache = $this->getCache();
     $cache->shouldReceive('get')->once()->with('mako.i18n.en_US')->andReturn(false);
     $cache->shouldReceive('put')->once()->with('mako.i18n.en_US', ['foo' => $this->strings['foo']], 3600);
     $i18n = new I18n($loader, 'en_US', $cache);
     $this->assertEquals('foostring', $i18n->get('foo.foo'));
 }